Class: AtomicCache::LastModTimeKeyManager
- Inherits:
-
Object
- Object
- AtomicCache::LastModTimeKeyManager
- 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
-
#current_key(keyspace) ⇒ String
get the key at the given keyspace, suffixed by the current timestamp.
-
#initialize(keyspace: nil, storage: nil, timestamp_formatter: nil) ⇒ LastModTimeKeyManager
constructor
A new instance of LastModTimeKeyManager.
- #last_known_key(keyspace) ⇒ Object
- #last_modified_time ⇒ Object
- #last_modified_time=(timestamp = Time.now) ⇒ Object
- #last_modified_time_key ⇒ Object
-
#lock(keyspace, ttl, options = {}) ⇒ Object
prevent other processes from modifying the given keyspace.
-
#lock_present?(keyspace) ⇒ Boolean
check if the keyspace is locked.
-
#next_key(keyspace, timestamp) ⇒ String
get a key at the given keyspace, suffixed by given timestamp.
-
#promote(keyspace, last_known_key:, timestamp:) ⇒ Object
promote a key and timestamp after a successful re-generation of a cache keyspace.
-
#unlock(keyspace) ⇒ Object
remove existing lock to allow other processes to update keyspace.
Constructor Details
#initialize(keyspace: nil, storage: nil, timestamp_formatter: nil) ⇒ LastModTimeKeyManager
Returns a new instance of LastModTimeKeyManager.
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) = keyspace @storage = storage || DefaultConfig.instance.key_storage = || DefaultConfig.instance. 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_time ⇒ Object
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=(=Time.now) @storage.set(last_modified_time_key, self.format()) end |
#last_modified_time_key ⇒ Object
79 80 81 |
# File 'lib/atomic_cache/key/last_mod_time_key_manager.rb', line 79 def last_modified_time_key @lmtk ||= .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, ={}) # returns false if the key already exists @storage.add(keyspace.lock_key, LOCK_VALUE, ttl, ) 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, ) keyspace.key(self.format()) 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()) 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 |