Class: PrefixedCacheStore
- Inherits:
-
Object
- Object
- PrefixedCacheStore
- Extended by:
- Forwardable
- Defined in:
- lib/prefixed_cache_store.rb
Overview
A caching store that will expire only the keys saved through it, by using a common prefix for the namespace and the version number that can be ratched up to “unlink” all the related keys. It assumes that the keys are being evicted automatically if they do not get used often.
Constant Summary collapse
- VERSION =
'0.2.2'
- RETAIN_PREFIX_FOR_SECONDS =
10
Instance Attribute Summary collapse
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
-
#clear(options = nil) ⇒ Object
Bump the version prefix making all keys obsolete.
-
#current_version_number ⇒ Object
Returns the current version that has been set for this store.
-
#decrement(name, amount = 1, options = nil) ⇒ Object
Decrement a cached value.
- #delete(name, options = nil) ⇒ Object
- #exist?(name, options = nil) ⇒ Boolean
- #fetch(name, options = nil) ⇒ Object
- #fetch_multi(*names) ⇒ Object
-
#increment(name, amount = 1, options = nil) ⇒ Object
Increment a cached value.
-
#initialize(store, prefix = 'pfx') ⇒ PrefixedCacheStore
constructor
A new instance of PrefixedCacheStore.
- #read(name, options = nil) ⇒ Object
-
#read_multi(*names) ⇒ Object
Reads multiple keys from the cache using a single call to the servers for all keys.
- #write(name, value, options = nil) ⇒ Object
Constructor Details
#initialize(store, prefix = 'pfx') ⇒ PrefixedCacheStore
Returns a new instance of PrefixedCacheStore.
22 23 24 25 |
# File 'lib/prefixed_cache_store.rb', line 22 def initialize(store, prefix = 'pfx') @store = store @prefix = prefix end |
Instance Attribute Details
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
14 15 16 |
# File 'lib/prefixed_cache_store.rb', line 14 def prefix @prefix end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
14 15 16 |
# File 'lib/prefixed_cache_store.rb', line 14 def store @store end |
Instance Method Details
#clear(options = nil) ⇒ Object
Bump the version prefix making all keys obsolete.
94 95 96 97 98 |
# File 'lib/prefixed_cache_store.rb', line 94 def clear(=nil) bump_version! # First bump the version @last_prefix = nil # Then make sure the cached version number will not be used get_and_set_current_version end |
#current_version_number ⇒ Object
Returns the current version that has been set for this store
101 102 103 |
# File 'lib/prefixed_cache_store.rb', line 101 def current_version_number get_and_set_current_version end |
#decrement(name, amount = 1, options = nil) ⇒ Object
Decrement a cached value.
85 86 87 88 89 90 91 |
# File 'lib/prefixed_cache_store.rb', line 85 def decrement(name, amount = 1, =nil) if @store.method(:decrement).parameters.length > 2 @store.decrement(prefix_key(name), amount, ) else @store.increment(prefix_key(name), amount) # Toss the options end end |
#delete(name, options = nil) ⇒ Object
59 60 61 |
# File 'lib/prefixed_cache_store.rb', line 59 def delete(name, =nil) @store.delete(prefix_key(name), ) end |
#exist?(name, options = nil) ⇒ Boolean
55 56 57 |
# File 'lib/prefixed_cache_store.rb', line 55 def exist?(name, =nil) @store.exist?(prefix_key(name), ) end |
#fetch(name, options = nil) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/prefixed_cache_store.rb', line 27 def fetch(name, =nil) if block_given? @store.fetch(prefix_key(name), ) { yield } else @store.fetch(prefix_key(name), ) end end |
#fetch_multi(*names) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/prefixed_cache_store.rb', line 35 def fetch_multi(*names) if !@store.respond_to?(:fetch_multi) raise NoMethodError, "#{@store.inspect} does not support fetch_multi()" end = names. prefixed_keys = names.map{|e| prefix_key(e) } @store.fetch_multi(*prefixed_keys, ) do | prefixed_key | yield(unprefix_key(prefixed_key)) end end |
#increment(name, amount = 1, options = nil) ⇒ Object
Increment a cached value.
76 77 78 79 80 81 82 |
# File 'lib/prefixed_cache_store.rb', line 76 def increment(name, amount = 1, =nil) if @store.method(:increment).parameters.length > 2 @store.increment(prefix_key(name), amount, ) else @store.increment(prefix_key(name), amount) # Toss the options end end |
#read(name, options = nil) ⇒ Object
47 48 49 |
# File 'lib/prefixed_cache_store.rb', line 47 def read(name, =nil) @store.read(prefix_key(name), ) end |
#read_multi(*names) ⇒ Object
Reads multiple keys from the cache using a single call to the servers for all keys. Keys must be Strings.
65 66 67 68 69 70 71 72 73 |
# File 'lib/prefixed_cache_store.rb', line 65 def read_multi(*names) names. prefixed_names = names.map{|e| prefix_key(e) } result = @store.read_multi(*prefixed_names) # Unprefix the keys received result.inject({}) do |memo, (prefixed_key, value)| memo.merge(unprefix_key(prefixed_key) => value) end end |
#write(name, value, options = nil) ⇒ Object
51 52 53 |
# File 'lib/prefixed_cache_store.rb', line 51 def write(name, value, =nil) @store.write(prefix_key(name), value, ) end |