Class: Mistri::Locks::RailsCache

Inherits:
Object
  • Object
show all
Defined in:
lib/mistri/locks/rails_cache.rb

Overview

Leases and flags over an ActiveSupport::Cache::Store (Redis-backed in any real deployment), so every process in the fleet reads the same truth. Pass any cache store duck; defaults to Rails.cache.

Instance Method Summary collapse

Constructor Details

#initialize(cache: nil, namespace: "mistri:locks") ⇒ RailsCache

Returns a new instance of RailsCache.



11
12
13
14
15
# File 'lib/mistri/locks/rails_cache.rb', line 11

def initialize(cache: nil, namespace: "mistri:locks")
  @cache = cache || (defined?(Rails) && Rails.cache) ||
           raise(ConfigurationError, "no cache store; pass cache: or configure Rails.cache")
  @namespace = namespace
end

Instance Method Details

#acquire(key, ttl:) ⇒ Object



17
18
19
# File 'lib/mistri/locks/rails_cache.rb', line 17

def acquire(key, ttl:)
  @cache.write(scoped(key), true, unless_exist: true, expires_in: ttl)
end

#clear_flag(key) ⇒ Object



41
# File 'lib/mistri/locks/rails_cache.rb', line 41

def clear_flag(key) = release(key)

#flag?(key) ⇒ Boolean

Returns:

  • (Boolean)


39
# File 'lib/mistri/locks/rails_cache.rb', line 39

def flag?(key) = held?(key)

#held?(key) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/mistri/locks/rails_cache.rb', line 31

def held?(key)
  @cache.exist?(scoped(key))
end

#release(key) ⇒ Object



26
27
28
29
# File 'lib/mistri/locks/rails_cache.rb', line 26

def release(key)
  @cache.delete(scoped(key))
  nil
end

#renew(key, ttl:) ⇒ Object



21
22
23
24
# File 'lib/mistri/locks/rails_cache.rb', line 21

def renew(key, ttl:)
  @cache.write(scoped(key), true, expires_in: ttl)
  nil
end

#set_flag(key, ttl: 300) ⇒ Object



35
36
37
# File 'lib/mistri/locks/rails_cache.rb', line 35

def set_flag(key, ttl: 300)
  renew(key, ttl: ttl)
end