Class: Bizside::Cache::Store
- Inherits:
-
Object
- Object
- Bizside::Cache::Store
- Defined in:
- lib/bizside/cache/store.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #cleanup(options = nil) ⇒ Object
- #clear(options = nil) ⇒ Object
- #decrement(name, amount = 1, options = nil) ⇒ Object
- #delete(name, options = nil) ⇒ Object
- #delete_matched(matcher, options = nil) ⇒ Object
- #exist?(name, options = nil) ⇒ Boolean
- #fetch(name, options = nil) ⇒ Object
- #fetch_multi(*names) ⇒ Object
- #increment(name, amount = 1, options = nil) ⇒ Object
-
#initialize(options = nil) ⇒ Store
constructor
A new instance of Store.
- #read(name, options = nil) ⇒ Object
- #read_multi(*names) ⇒ Object
- #write(name, value, options = nil) ⇒ Object
Constructor Details
#initialize(options = nil) ⇒ Store
Returns a new instance of Store.
9 10 11 |
# File 'lib/bizside/cache/store.rb', line 9 def initialize( = nil) @options = ? .dup : {} end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/bizside/cache/store.rb', line 7 def @options end |
Instance Method Details
#cleanup(options = nil) ⇒ Object
122 123 124 |
# File 'lib/bizside/cache/store.rb', line 122 def cleanup( = nil) raise NotImplementedError.new("does not support cleanup") end |
#clear(options = nil) ⇒ Object
126 127 128 |
# File 'lib/bizside/cache/store.rb', line 126 def clear( = nil) raise NotImplementedError.new("does not support clear") end |
#decrement(name, amount = 1, options = nil) ⇒ Object
118 119 120 |
# File 'lib/bizside/cache/store.rb', line 118 def decrement(name, amount = 1, = nil) raise NotImplementedError.new("does not support decrement") end |
#delete(name, options = nil) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/bizside/cache/store.rb', line 93 def delete(name, = nil) = () instrument(:delete, name) do delete_entry(namespaced_key(name, ), ) end end |
#delete_matched(matcher, options = nil) ⇒ Object
110 111 112 |
# File 'lib/bizside/cache/store.rb', line 110 def delete_matched(matcher, = nil) raise NotImplementedError.new("does not support delete_matched") end |
#exist?(name, options = nil) ⇒ Boolean
101 102 103 104 105 106 107 108 |
# File 'lib/bizside/cache/store.rb', line 101 def exist?(name, = nil) = () instrument(:exist?, name) do entry = read_entry(namespaced_key(name, ), ) (entry && !entry.expired?) || false end end |
#fetch(name, options = nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/bizside/cache/store.rb', line 13 def fetch(name, = nil) if block_given? = () key = namespaced_key(name, ) cached_entry = find_cached_entry(key, name, ) unless [:force] entry = handle_expired_entry(cached_entry, key, ) if entry get_entry_value(entry, name, ) else save_block_result_to_cache(name, ) { |_name| yield _name } end else read(name, ) end end |
#fetch_multi(*names) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/bizside/cache/store.rb', line 70 def fetch_multi(*names) = names. = () results = read_multi(*names, ) names.each_with_object({}) do |name, memo| memo[name] = results.fetch(name) do value = yield name write(name, value, ) value end end end |
#increment(name, amount = 1, options = nil) ⇒ Object
114 115 116 |
# File 'lib/bizside/cache/store.rb', line 114 def increment(name, amount = 1, = nil) raise NotImplementedError.new("does not support increment") end |
#read(name, options = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/bizside/cache/store.rb', line 31 def read(name, = nil) = () key = namespaced_key(name, ) instrument(:read, name, ) do |payload| entry = read_entry(key, ) if entry if entry.expired? delete_entry(key, ) payload[:hit] = false if payload nil else payload[:hit] = true if payload entry.value end else payload[:hit] = false if payload nil end end end |
#read_multi(*names) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/bizside/cache/store.rb', line 52 def read_multi(*names) = names. = () results = {} names.each do |name| key = namespaced_key(name, ) entry = read_entry(key, ) if entry if entry.expired? delete_entry(key, ) else results[name] = entry.value end end end results end |