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



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

Parameters:

  • id_key_map

    is a map with => cache_key

Returns:

  • a map with => current_version



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, options = {})
  new_version = (Time.current.to_f * 10000).to_i
  seconds = options[:ttl] ? options[:ttl] + (rand(options[: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, options = {})
  renew(key, false, options)
end