Class: Marty::CacheAdapters::Redis
- Inherits:
-
Delorean::Cache::Adapters::Base
- Object
- Delorean::Cache::Adapters::Base
- Marty::CacheAdapters::Redis
- Defined in:
- lib/marty/cache_adapters/redis.rb
Constant Summary collapse
- POST =
'__RedisCache'
Instance Method Summary collapse
- #cache_item(klass:, cache_key:, item:) ⇒ Object
- #cache_item?(klass:, method_name:, args:) ⇒ Boolean
- #cache_key(klass:, method_name:, args:) ⇒ Object
- #clear!(klass:) ⇒ Object
- #clear_all! ⇒ Object
- #fetch_item(klass:, cache_key:, default: nil) ⇒ Object
-
#initialize(size_per_class: 1000, redis_url: Rails.application.config.marty.redis_url, expires_in: 48.hours) ⇒ Redis
constructor
A new instance of Redis.
Constructor Details
#initialize(size_per_class: 1000, redis_url: Rails.application.config.marty.redis_url, expires_in: 48.hours) ⇒ Redis
Returns a new instance of Redis.
10 11 12 13 14 15 16 17 |
# File 'lib/marty/cache_adapters/redis.rb', line 10 def initialize( size_per_class: 1000, redis_url: Rails.application.config.marty.redis_url, expires_in: 48.hours ) @redis = ::Redis.new(url: "redis://#{redis_url}") @expires_in = expires_in end |
Instance Method Details
#cache_item(klass:, cache_key:, item:) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/marty/cache_adapters/redis.rb', line 19 def cache_item(klass:, cache_key:, item:) @redis.set( cache_key, Marshal.dump(item), ex: @expires_in.seconds.to_i ) end |
#cache_item?(klass:, method_name:, args:) ⇒ Boolean
58 59 60 |
# File 'lib/marty/cache_adapters/redis.rb', line 58 def cache_item?(klass:, method_name:, args:) !Mcfly.is_infinity(args&.first) end |
#cache_key(klass:, method_name:, args:) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/marty/cache_adapters/redis.rb', line 35 def cache_key(klass:, method_name:, args:) r = ["#{klass.name}#{POST}", method_name] + args.map do |arg| next arg.id if arg.respond_to?(:id) arg end.freeze Marshal.dump r end |
#clear!(klass:) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/marty/cache_adapters/redis.rb', line 45 def clear!(klass:) keys = @redis.keys("*#{klass.name}#{POST}*") @redis.pipelined do keys.each do |key| @redis.del key end end end |
#clear_all! ⇒ Object
54 55 56 |
# File 'lib/marty/cache_adapters/redis.rb', line 54 def clear_all! @redis.flushall end |
#fetch_item(klass:, cache_key:, default: nil) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/marty/cache_adapters/redis.rb', line 27 def fetch_item(klass:, cache_key:, default: nil) r = @redis.get(cache_key) return default if r.nil? Marshal.load(r) end |