Class: AtomicCache::LastModTimeKeyManager

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/atomic_cache/key/last_mod_time_key_manager.rb

Constant Summary collapse

LOCK_VALUE =
1

Instance Method Summary collapse

Constructor Details

#initialize(keyspace: nil, storage: nil, timestamp_formatter: nil) ⇒ LastModTimeKeyManager

Returns a new instance of LastModTimeKeyManager.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
# File 'lib/atomic_cache/key/last_mod_time_key_manager.rb', line 15

def initialize(keyspace: nil, storage: nil, timestamp_formatter: nil)
  @timestamp_keyspace = keyspace
  @storage = storage || DefaultConfig.instance.key_storage
  @timestamp_formatter = timestamp_formatter || DefaultConfig.instance.timestamp_formatter

  raise ArgumentError.new("`storage` required but none given") unless @storage.present?
  raise ArgumentError.new("`root_keyspace` required but none given") unless @storage.present?
end

Instance Method Details

#current_key(keyspace) ⇒ String

get the key at the given keyspace, suffixed by the current timestamp



28
29
30
# File 'lib/atomic_cache/key/last_mod_time_key_manager.rb', line 28

def current_key(keyspace)
  keyspace.key(last_modified_time)
end

#last_known_key(keyspace) ⇒ Object



75
76
77
# File 'lib/atomic_cache/key/last_mod_time_key_manager.rb', line 75

def last_known_key(keyspace)
  @storage.read(keyspace.last_known_key_key)
end

#last_modified_timeObject



83
84
85
# File 'lib/atomic_cache/key/last_mod_time_key_manager.rb', line 83

def last_modified_time
  @storage.read(last_modified_time_key)
end

#last_modified_time=(timestamp = Time.now) ⇒ Object



87
88
89
# File 'lib/atomic_cache/key/last_mod_time_key_manager.rb', line 87

def last_modified_time=(timestamp=Time.now)
  @storage.set(last_modified_time_key, self.format(timestamp))
end

#last_modified_time_keyObject



79
80
81
# File 'lib/atomic_cache/key/last_mod_time_key_manager.rb', line 79

def last_modified_time_key
  @lmtk ||= @timestamp_keyspace.key('lmt')
end

#lock(keyspace, ttl, options = {}) ⇒ Object

prevent other processes from modifying the given keyspace



56
57
58
59
# File 'lib/atomic_cache/key/last_mod_time_key_manager.rb', line 56

def lock(keyspace, ttl, options={})
  # returns false if the key already exists
  @storage.add(keyspace.lock_key, LOCK_VALUE, ttl, options)
end

#lock_present?(keyspace) ⇒ Boolean

check if the keyspace is locked



64
65
66
# File 'lib/atomic_cache/key/last_mod_time_key_manager.rb', line 64

def lock_present?(keyspace)
  @storage.read(keyspace.lock_key) == LOCK_VALUE
end

#next_key(keyspace, timestamp) ⇒ String

get a key at the given keyspace, suffixed by given timestamp



37
38
39
# File 'lib/atomic_cache/key/last_mod_time_key_manager.rb', line 37

def next_key(keyspace, timestamp)
  keyspace.key(self.format(timestamp))
end

#promote(keyspace, last_known_key:, timestamp:) ⇒ Object

promote a key and timestamp after a successful re-generation of a cache keyspace



46
47
48
49
# File 'lib/atomic_cache/key/last_mod_time_key_manager.rb', line 46

def promote(keyspace, last_known_key:, timestamp:)
  @storage.set(keyspace.last_known_key_key, last_known_key)
  @storage.set(last_modified_time_key, self.format(timestamp))
end

#unlock(keyspace) ⇒ Object

remove existing lock to allow other processes to update keyspace



71
72
73
# File 'lib/atomic_cache/key/last_mod_time_key_manager.rb', line 71

def unlock(keyspace)
  @storage.delete(keyspace.lock_key)
end