Module: Lightly::CacheOperations
Instance Attribute Summary collapse
- #dir ⇒ Object
-
#hash ⇒ Object
writeonly
Sets the attribute hash.
-
#permissions ⇒ Object
Returns the value of attribute permissions.
Instance Method Summary collapse
- #cached?(key) ⇒ Boolean
- #clear(key) ⇒ Object
- #disable ⇒ Object
- #enable ⇒ Object
- #enabled? ⇒ Boolean
- #flush ⇒ Object
- #get(key) ⇒ Object
- #get_path(key) ⇒ Object
- #hash? ⇒ Boolean
- #initialize(dir: 'cache', life: '1h', hash: true, enabled: true, permissions: nil) ⇒ Object
- #life ⇒ Object
- #life=(new_life) ⇒ Object
- #prune ⇒ Object
- #save(key, content) ⇒ Object
Instance Attribute Details
#dir ⇒ Object
33 34 35 |
# File 'lib/lightly/cache_operations.rb', line 33 def dir @dir ||= 'cache' end |
#hash=(value) ⇒ Object (writeonly)
Sets the attribute hash
7 8 9 |
# File 'lib/lightly/cache_operations.rb', line 7 def hash=(value) @hash = value end |
#permissions ⇒ Object
Returns the value of attribute permissions.
6 7 8 |
# File 'lib/lightly/cache_operations.rb', line 6 def @permissions end |
Instance Method Details
#cached?(key) ⇒ Boolean
62 63 64 65 |
# File 'lib/lightly/cache_operations.rb', line 62 def cached?(key) path = get_path key File.exist?(path) and File.size(path).positive? and !expired?(path) end |
#clear(key) ⇒ Object
45 46 47 48 |
# File 'lib/lightly/cache_operations.rb', line 45 def clear(key) path = get_path key FileUtils.rm path if File.exist? path end |
#disable ⇒ Object
71 72 73 |
# File 'lib/lightly/cache_operations.rb', line 71 def disable @enabled = false end |
#enable ⇒ Object
67 68 69 |
# File 'lib/lightly/cache_operations.rb', line 67 def enable @enabled = true end |
#enabled? ⇒ Boolean
41 42 43 |
# File 'lib/lightly/cache_operations.rb', line 41 def enabled? @enabled ||= (@enabled.nil? ? true : @enabled) end |
#flush ⇒ Object
50 51 52 53 54 |
# File 'lib/lightly/cache_operations.rb', line 50 def flush return false if dir == '/' || dir.empty? FileUtils.rm_rf dir end |
#get(key) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/lightly/cache_operations.rb', line 17 def get(key) return load key if cached?(key) && enabled? content = yield save key, content if content && enabled? content end |
#get_path(key) ⇒ Object
75 76 77 78 |
# File 'lib/lightly/cache_operations.rb', line 75 def get_path(key) key = Digest::MD5.hexdigest(key) if hash? File.join dir, key end |
#hash? ⇒ Boolean
37 38 39 |
# File 'lib/lightly/cache_operations.rb', line 37 def hash? @hash ||= (@hash.nil? ? true : @hash) end |
#initialize(dir: 'cache', life: '1h', hash: true, enabled: true, permissions: nil) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/lightly/cache_operations.rb', line 9 def initialize(dir: 'cache', life: '1h', hash: true, enabled: true, permissions: nil) @dir = dir @life = life_to_seconds life @hash = hash @enabled = enabled @permissions = end |
#life ⇒ Object
25 26 27 |
# File 'lib/lightly/cache_operations.rb', line 25 def life @life ||= 3600 end |
#life=(new_life) ⇒ Object
29 30 31 |
# File 'lib/lightly/cache_operations.rb', line 29 def life=(new_life) @life = life_to_seconds new_life end |
#prune ⇒ Object
56 57 58 59 60 |
# File 'lib/lightly/cache_operations.rb', line 56 def prune return false if dir == '/' || dir.empty? Dir["#{dir}/*"].each { |file| expired? file } end |
#save(key, content) ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/lightly/cache_operations.rb', line 80 def save(key, content) FileUtils.mkdir_p dir path = get_path key File.open path, 'wb', do |file| file.write Marshal.dump(content) end end |