Class: RecordCache::VersionStore
- Inherits:
-
Object
- Object
- RecordCache::VersionStore
- Defined in:
- lib/record_cache/version_store.rb
Instance Attribute Summary collapse
-
#store ⇒ Object
Returns the value of attribute store.
Instance Method Summary collapse
-
#current(key) ⇒ Object
Retrieve the current versions for the given key.
-
#current_multi(id_key_map) ⇒ Object
Retrieve the current versions for the given keys version nil for all keys unknown to the version store.
-
#delete(key) ⇒ Object
Delete key from the version store (records cached in the Record Store belonging to this key will become unreachable).
-
#increment(key) ⇒ Object
@deprecated: use renew instead.
-
#initialize(store) ⇒ VersionStore
constructor
A new instance of VersionStore.
-
#multi(&block) ⇒ Object
perform several actions on the version store in one go Dalli: Turn on quiet aka noreply support.
- #on_write_failure(&blk) ⇒ Object
-
#renew(key, write = true, options = {}) ⇒ Object
Call this method to reset the key to a new (unique) version.
- #renew_for_read(key, options = {}) ⇒ Object
Constructor Details
#initialize(store) ⇒ VersionStore
Returns a new instance of VersionStore.
5 6 7 8 9 10 |
# File 'lib/record_cache/version_store.rb', line 5 def initialize(store) [:write, :read, :read_multi, :delete].each do |method| raise "Store #{store.class.name} must respond to #{method}" unless store.respond_to?(method) end @store = store end |
Instance Attribute Details
#store ⇒ Object
Returns the value of attribute store.
3 4 5 |
# File 'lib/record_cache/version_store.rb', line 3 def store @store end |
Instance Method Details
#current(key) ⇒ Object
Retrieve the current versions for the given key
18 19 20 |
# File 'lib/record_cache/version_store.rb', line 18 def current(key) @store.read(key) end |
#current_multi(id_key_map) ⇒ Object
Retrieve the current versions for the given keys version nil for all keys unknown to the version store
26 27 28 29 |
# File 'lib/record_cache/version_store.rb', line 26 def current_multi(id_key_map) current_versions = @store.read_multi(*(id_key_map.values)) Hash[id_key_map.map{ |id, key| [id, current_versions[key]] }] end |
#delete(key) ⇒ Object
Delete key from the version store (records cached in the Record Store belonging to this key will become unreachable)
63 64 65 66 67 68 |
# File 'lib/record_cache/version_store.rb', line 63 def delete(key) deleted = @store.delete(key) @on_write_failure.call(key) if !deleted && @on_write_failure RecordCache::Base.logger.debug{ "Version Store: deleted #{key}" } deleted end |
#increment(key) ⇒ Object
@deprecated: use renew instead
57 58 59 60 |
# File 'lib/record_cache/version_store.rb', line 57 def increment(key) RecordCache::Base.logger.debug{ "increment is deprecated, use renew instead. Called from: #{caller[0]}" } renew(key, true) end |
#multi(&block) ⇒ Object
perform several actions on the version store in one go Dalli: Turn on quiet aka noreply support. All relevant operations within this block will be effectively pipelined using ‘quiet’ operations where possible.
Currently supports the set, add, replace and delete operations for Dalli cache.
48 49 50 51 52 53 54 |
# File 'lib/record_cache/version_store.rb', line 48 def multi(&block) if @store.respond_to?(:multi) @store.multi(&block) else yield end end |
#on_write_failure(&blk) ⇒ Object
12 13 14 |
# File 'lib/record_cache/version_store.rb', line 12 def on_write_failure(&blk) @on_write_failure = blk end |
#renew(key, write = true, options = {}) ⇒ Object
Call this method to reset the key to a new (unique) version
36 37 38 39 40 41 42 43 |
# File 'lib/record_cache/version_store.rb', line 36 def renew(key, write = true, = {}) new_version = (Time.current.to_f * 10000).to_i seconds = [:ttl] ? [:ttl] + (rand([:ttl] / 2) * [1, -1].sample) : nil written = @store.write(key, new_version, {:expires_in => seconds}) @on_write_failure.call(key) if !written && write && @on_write_failure RecordCache::Base.logger.debug{ "Version Store: renew #{key}: nil => #{new_version}" } new_version end |
#renew_for_read(key, options = {}) ⇒ Object
31 32 33 |
# File 'lib/record_cache/version_store.rb', line 31 def renew_for_read(key, = {}) renew(key, false, ) end |