Module: WebCache::CacheOperations
Instance Attribute Summary collapse
- #dir ⇒ Object
-
#last_error ⇒ Object
readonly
Returns the value of attribute last_error.
Instance Method Summary collapse
- #cached?(url) ⇒ Boolean
- #clear(url) ⇒ Object
- #disable ⇒ Object
- #enable ⇒ Object
- #enabled? ⇒ Boolean
- #flush ⇒ Object
- #get(url, force: false) ⇒ Object
- #initialize(dir: 'cache', life: '1h') ⇒ Object
- #life ⇒ Object
- #life=(new_life) ⇒ Object
- #options ⇒ Object
Instance Attribute Details
#dir ⇒ Object
34 35 36 |
# File 'lib/webcache/cache_operations.rb', line 34 def dir @dir ||= 'cache' end |
#last_error ⇒ Object (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 |
Instance Method Details
#cached?(url) ⇒ 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 |
#disable ⇒ Object
51 52 53 |
# File 'lib/webcache/cache_operations.rb', line 51 def disable @enabled = false end |
#enable ⇒ Object
47 48 49 |
# File 'lib/webcache/cache_operations.rb', line 47 def enable @enabled = true end |
#enabled? ⇒ Boolean
43 44 45 |
# File 'lib/webcache/cache_operations.rb', line 43 def enabled? @enabled ||= (@enabled.nil? ? true : @enabled) end |
#flush ⇒ Object
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') ⇒ Object
11 12 13 14 15 |
# File 'lib/webcache/cache_operations.rb', line 11 def initialize(dir: 'cache', life: '1h') @dir = dir @life = life_to_seconds life @enabled = true end |
#life ⇒ Object
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 |
#options ⇒ Object
64 65 66 |
# File 'lib/webcache/cache_operations.rb', line 64 def @options ||= end |