Module: Lightly::CacheOperations
Instance Attribute Summary collapse
- #dir ⇒ Object
-
#hash ⇒ Object
writeonly
Sets the attribute hash.
Instance Method Summary collapse
- #cached?(key) ⇒ Boolean
- #clear(key) ⇒ Object
- #disable ⇒ Object
- #enable ⇒ Object
- #enabled? ⇒ Boolean
- #flush ⇒ Object
- #get(key, &block) ⇒ Object
- #get_path(key) ⇒ Object
- #hash? ⇒ Boolean
- #initialize(dir: 'cache', life: '1h', hash: true, enabled: true) ⇒ Object
- #life ⇒ Object
- #life=(new_life) ⇒ Object
- #prune ⇒ Object
- #save(key, content) ⇒ Object
Instance Attribute Details
#dir ⇒ Object
31 32 33 |
# File 'lib/lightly/cache_operations.rb', line 31 def dir @dir ||= 'cache' end |
#hash=(value) ⇒ Object (writeonly)
Sets the attribute hash
6 7 8 |
# File 'lib/lightly/cache_operations.rb', line 6 def hash=(value) @hash = value end |
Instance Method Details
#cached?(key) ⇒ Boolean
58 59 60 61 |
# File 'lib/lightly/cache_operations.rb', line 58 def cached?(key) path = get_path key File.exist?(path) and File.size(path) > 0 and !expired?(path) end |
#clear(key) ⇒ Object
43 44 45 46 |
# File 'lib/lightly/cache_operations.rb', line 43 def clear(key) path = get_path key FileUtils.rm path if File.exist? path end |
#disable ⇒ Object
67 68 69 |
# File 'lib/lightly/cache_operations.rb', line 67 def disable @enabled = false end |
#enable ⇒ Object
63 64 65 |
# File 'lib/lightly/cache_operations.rb', line 63 def enable @enabled = true end |
#enabled? ⇒ Boolean
39 40 41 |
# File 'lib/lightly/cache_operations.rb', line 39 def enabled? @enabled ||= (@enabled.nil? ? true : @enabled) end |
#flush ⇒ Object
48 49 50 51 |
# File 'lib/lightly/cache_operations.rb', line 48 def flush return false if dir == '/' || dir.empty? FileUtils.rm_rf dir end |
#get(key, &block) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/lightly/cache_operations.rb', line 15 def get(key, &block) return load key if cached?(key) && enabled? content = block.call save key, content if content && enabled? content end |
#get_path(key) ⇒ Object
71 72 73 74 |
# File 'lib/lightly/cache_operations.rb', line 71 def get_path(key) key = Digest::MD5.hexdigest(key) if hash? File.join dir, key end |
#hash? ⇒ Boolean
35 36 37 |
# File 'lib/lightly/cache_operations.rb', line 35 def hash? @hash ||= (@hash.nil? ? true : @hash) end |
#initialize(dir: 'cache', life: '1h', hash: true, enabled: true) ⇒ Object
8 9 10 11 12 13 |
# File 'lib/lightly/cache_operations.rb', line 8 def initialize(dir: 'cache', life: '1h', hash: true, enabled: true) @dir = dir @life = life_to_seconds life @hash = hash @enabled = enabled end |
#life ⇒ Object
23 24 25 |
# File 'lib/lightly/cache_operations.rb', line 23 def life @life ||= 3600 end |
#life=(new_life) ⇒ Object
27 28 29 |
# File 'lib/lightly/cache_operations.rb', line 27 def life=(new_life) @life = life_to_seconds new_life end |
#prune ⇒ Object
53 54 55 56 |
# File 'lib/lightly/cache_operations.rb', line 53 def prune return false if dir == '/' || dir.empty? Dir["#{dir}/*"].each { |file| expired? file } end |
#save(key, content) ⇒ Object
76 77 78 79 80 81 82 |
# File 'lib/lightly/cache_operations.rb', line 76 def save(key, content) FileUtils.mkdir_p dir path = get_path key File.open path, 'wb' do |f| f.write Marshal.dump content end end |