Module: WebCache::CacheOperations

Included in:
WebCache, WebCache
Defined in:
lib/webcache/cache_operations.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#authObject

Returns the value of attribute auth.



7
8
9
# File 'lib/webcache/cache_operations.rb', line 7

def auth
  @auth
end

#dirObject



34
35
36
# File 'lib/webcache/cache_operations.rb', line 34

def dir
  @dir ||= 'cache'
end

#last_errorObject (readonly)

Returns the value of attribute last_error.



7
8
9
# File 'lib/webcache/cache_operations.rb', line 7

def last_error
  @last_error
end

#passObject (readonly)

Returns the value of attribute pass.



7
8
9
# File 'lib/webcache/cache_operations.rb', line 7

def pass
  @pass
end

#userObject (readonly)

Returns the value of attribute user.



7
8
9
# File 'lib/webcache/cache_operations.rb', line 7

def user
  @user
end

Instance Method Details

#cached?(url) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
# File 'lib/webcache/cache_operations.rb', line 38

def cached?(url)
  path = get_path url
  File.exist?(path) and !stale?(path)
end

#clear(url) ⇒ Object



55
56
57
58
# File 'lib/webcache/cache_operations.rb', line 55

def clear(url)
  path = get_path url
  FileUtils.rm path if File.exist? path
end

#disableObject



51
52
53
# File 'lib/webcache/cache_operations.rb', line 51

def disable
  @enabled = false
end

#enableObject



47
48
49
# File 'lib/webcache/cache_operations.rb', line 47

def enable
  @enabled = true
end

#enabled?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/webcache/cache_operations.rb', line 43

def enabled?
  @enabled ||= (@enabled.nil? ? true : @enabled)
end

#flushObject



60
61
62
# File 'lib/webcache/cache_operations.rb', line 60

def flush
  FileUtils.rm_rf dir if Dir.exist? dir
end

#get(url, force: false) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/webcache/cache_operations.rb', line 17

def get(url, force: false)
  return http_get url unless enabled?

  path = get_path url
  clear url if force or stale? path

  get! path, url
end

#initialize(dir: 'cache', life: '1h', auth: nil) ⇒ Object



10
11
12
13
14
15
# File 'lib/webcache/cache_operations.rb', line 10

def initialize(dir: 'cache', life: '1h', auth: nil)
  @dir = dir
  @life = life_to_seconds life
  @enabled = true
  @auth = convert_auth auth
end

#lifeObject



26
27
28
# File 'lib/webcache/cache_operations.rb', line 26

def life
  @life ||= 3600
end

#life=(new_life) ⇒ Object



30
31
32
# File 'lib/webcache/cache_operations.rb', line 30

def life=(new_life)
  @life = life_to_seconds new_life
end