Class: RecordCache::VersionStore

Inherits:
Object
  • Object
show all
Defined in:
lib/record_cache/version_store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#storeObject

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

Returns:

  • nil in case the key is not known in the version store



14
15
16
# File 'lib/record_cache/version_store.rb', line 14

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

Parameters:

  • id_key_map

    is a map with => cache_key

Returns:

  • a map with => current_version



22
23
24
25
# File 'lib/record_cache/version_store.rb', line 22

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)



54
55
56
57
58
# File 'lib/record_cache/version_store.rb', line 54

def delete(key)
  deleted = @store.delete(key)
  RecordCache::Base.logger.debug{ "Version Store: deleted #{key}" }
  deleted
end

#increment(key) ⇒ Object

@deprecated: use renew instead



48
49
50
51
# File 'lib/record_cache/version_store.rb', line 48

def increment(key)
  RecordCache::Base.logger.debug{ "increment is deprecated, use renew instead. Called from: #{caller[0]}" }
  renew(key)
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.


39
40
41
42
43
44
45
# File 'lib/record_cache/version_store.rb', line 39

def multi(&block)
  if @store.respond_to?(:multi)
    @store.multi(&block)
  else
    yield
  end
end

#renew(key, options = {}) ⇒ Object

Call this method to reset the key to a new (unique) version



28
29
30
31
32
33
34
# File 'lib/record_cache/version_store.rb', line 28

def renew(key, options = {})
  new_version = (Time.current.to_f * 10000).to_i
  seconds = options[:ttl] ? options[:ttl] + (rand(options[:ttl] / 2) * [1, -1].sample) : nil
  @store.write(key, new_version, {:expires_in => seconds})
  RecordCache::Base.logger.debug{ "Version Store: renew #{key}: nil => #{new_version}" }
  new_version
end