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
# File 'lib/record_cache/version_store.rb', line 5

def initialize(store)
  raise "Must be an ActiveSupport::Cache::Store" unless store.is_a?(ActiveSupport::Cache::Store)
  @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



12
13
14
# File 'lib/record_cache/version_store.rb', line 12

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



20
21
22
23
# File 'lib/record_cache/version_store.rb', line 20

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)



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

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

#increment(key) ⇒ Object

Increment the current version for the given key, in case of record updates



35
36
37
38
39
40
41
42
43
44
# File 'lib/record_cache/version_store.rb', line 35

def increment(key)
  version = @store.increment(key, 1)
  # renew key in case the version store already purged the key
  if version.nil? || version == 1
    version = renew(key)
  else
    RecordCache::Base.logger.debug("Version Store: incremented #{key}: #{version - 1} => #{version}") if RecordCache::Base.logger.debug?
  end
  version
end

#renew(key) ⇒ Object

In case the version store did not have a key anymore, call this methods to reset the key with a (unique) new version



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

def renew(key)
  new_version = (Time.current.to_f * 10000).to_i
  @store.write(key, new_version)
  RecordCache::Base.logger.debug("Version Store: renew #{key}: nil => #{new_version}") if RecordCache::Base.logger.debug?
  new_version
end