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.



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

def auth
  @auth
end

#dirObject



36
37
38
# File 'lib/webcache/cache_operations.rb', line 36

def dir
  @dir ||= 'cache'
end

#last_errorObject (readonly)

Returns the value of attribute last_error.



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

def last_error
  @last_error
end

#passObject (readonly)

Returns the value of attribute pass.



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

def pass
  @pass
end

#permissionsObject

Returns the value of attribute permissions.



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

def permissions
  @permissions
end

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

Instance Method Details

#cached?(url) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/webcache/cache_operations.rb', line 40

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

#clear(url) ⇒ Object



57
58
59
60
# File 'lib/webcache/cache_operations.rb', line 57

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

#disableObject



53
54
55
# File 'lib/webcache/cache_operations.rb', line 53

def disable
  @enabled = false
end

#enableObject



49
50
51
# File 'lib/webcache/cache_operations.rb', line 49

def enable
  @enabled = true
end

#enabled?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/webcache/cache_operations.rb', line 45

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

#flushObject



62
63
64
# File 'lib/webcache/cache_operations.rb', line 62

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

#get(url, force: false) ⇒ Object



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

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

  path = get_path url
  clear url if force || stale?(path)

  get! path, url
end

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



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

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

#lifeObject



28
29
30
# File 'lib/webcache/cache_operations.rb', line 28

def life
  @life ||= 3600
end

#life=(new_life) ⇒ Object



32
33
34
# File 'lib/webcache/cache_operations.rb', line 32

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