Class: Redis::Stream::DataCache
- Inherits:
-
Object
- Object
- Redis::Stream::DataCache
- Defined in:
- lib/redis/stream/data_cache.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #build_key(data) ⇒ Object
- #delete(key) ⇒ Object
- #include?(key) ⇒ Boolean
-
#initialize(logger = Logger.new(STDOUT)) ⇒ DataCache
constructor
A new instance of DataCache.
- #key?(key) ⇒ Boolean
- #key_prefix ⇒ Object
- #key_prefix=(prefix) ⇒ Object
- #resolve_by_message(pid, payload, &block) ⇒ Object
Constructor Details
#initialize(logger = Logger.new(STDOUT)) ⇒ DataCache
Returns a new instance of DataCache.
8 9 10 11 |
# File 'lib/redis/stream/data_cache.rb', line 8 def initialize(logger = Logger.new(STDOUT)) @logger = logger @cache = Moneta.new(:HashFile, dir: Redis::Stream::Config[:data_cache] || "/tmp/cache", serializer: :json) end |
Instance Method Details
#[](key) ⇒ Object
18 19 20 |
# File 'lib/redis/stream/data_cache.rb', line 18 def [](key) @cache.fetch(key) end |
#[]=(key, value) ⇒ Object
13 14 15 16 |
# File 'lib/redis/stream/data_cache.rb', line 13 def []=(key, value) @logger.info("CACHE - #{File.basename(__FILE__)}:#{__LINE__} - caching with key #{key}") @cache.store(key, value) end |
#build_key(data) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/redis/stream/data_cache.rb', line 42 def build_key(data) key = "" if data && data.include?('payload') payload_data = data['payload'] else payload_data = data end if payload_data.include?("id") id = payload_data["id"].downcase key = "#{id}" key = "#{@key_prefix}_#{key}" unless @key_prefix.nil? || @key_prefix&.empty? end raise "Empty cache key" if key.nil? || key&.empty? key end |
#delete(key) ⇒ Object
30 31 32 |
# File 'lib/redis/stream/data_cache.rb', line 30 def delete(key) @cache.delete(key) end |
#include?(key) ⇒ Boolean
22 23 24 |
# File 'lib/redis/stream/data_cache.rb', line 22 def include?(key) key?(key) end |
#key?(key) ⇒ Boolean
26 27 28 |
# File 'lib/redis/stream/data_cache.rb', line 26 def key?(key) @cache.key?(key) end |
#key_prefix ⇒ Object
38 39 40 |
# File 'lib/redis/stream/data_cache.rb', line 38 def key_prefix @key_prefix end |
#key_prefix=(prefix) ⇒ Object
34 35 36 |
# File 'lib/redis/stream/data_cache.rb', line 34 def key_prefix=(prefix) @key_prefix = prefix.downcase end |
#resolve_by_message(pid, payload, &block) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/redis/stream/data_cache.rb', line 59 def (pid, payload, &block) data = nil cache_key = build_key(payload) invalidate_cache(cache_key, payload) data = load_from_cache(cache_key) data = load_from_service(pid, cache_key, &block) if data.nil? data end |