Class: Marty::CacheAdapters::MemoryAndRedis

Inherits:
Delorean::Cache::Adapters::Base
  • Object
show all
Defined in:
lib/marty/cache_adapters/memory_and_redis.rb

Constant Summary collapse

POST =
'__RedisCache'

Instance Method Summary collapse

Constructor Details

#initialize(size_per_class: 1000, redis_url: Rails.application.config.marty.redis_url, redis_expires_in: 48.hours) ⇒ MemoryAndRedis

Returns a new instance of MemoryAndRedis.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/marty/cache_adapters/memory_and_redis.rb', line 8

def initialize(
  size_per_class: 1000,
  redis_url: Rails.application.config.marty.redis_url,
  redis_expires_in: 48.hours
)
  @size_per_class = size_per_class

  @redis_adapter = ::Marty::CacheAdapters::Redis.new(
    redis_url: redis_url,
    expires_in: redis_expires_in
  )

  @memory_adapter = ::Marty::CacheAdapters::McflyRubyCache.new(
    size_per_class: size_per_class
  )
end

Instance Method Details

#cache_item(klass:, cache_key:, item:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/marty/cache_adapters/memory_and_redis.rb', line 25

def cache_item(klass:, cache_key:, item:)
  @redis_adapter.cache_item(
    klass: klass,
    cache_key: cache_key,
    item: item
  )

  @memory_adapter.cache_item(
    klass: klass,
    cache_key: cache_key,
    item: item
  )
end

#cache_item?(klass:, method_name:, args:) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/marty/cache_adapters/memory_and_redis.rb', line 88

def cache_item?(klass:, method_name:, args:)
  !Mcfly.is_infinity(args&.first)
end

#cache_key(klass:, method_name:, args:) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/marty/cache_adapters/memory_and_redis.rb', line 68

def cache_key(klass:, method_name:, args:)
  r = ["#{klass.name}#{POST}", method_name] + args.map do |arg|
    arg.respond_to?(:id) ? arg.id : arg

    arg
  end.freeze

  Marshal.dump r
end

#clear!(klass:) ⇒ Object



78
79
80
81
# File 'lib/marty/cache_adapters/memory_and_redis.rb', line 78

def clear!(klass:)
  @redis_adapter.clear!(klass: klass)
  @memory_adapter.clear!(klass: klass)
end

#clear_all!Object



83
84
85
86
# File 'lib/marty/cache_adapters/memory_and_redis.rb', line 83

def clear_all!
  @redis_adapter.clear_all!
  @memory_adapter.clear_all!
end

#fetch_item(klass:, cache_key:, default: nil) ⇒ Object

When cache is found in local memory, we simply return the cached item. Otherwise we look into Redis, if item is cached there, we copy it to local memory to speed up future lookups.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/marty/cache_adapters/memory_and_redis.rb', line 42

def fetch_item(klass:, cache_key:, default: nil)
  memory_item = @memory_adapter.fetch_item(
    klass: klass,
    cache_key: cache_key,
    default: default
  )

  return memory_item if memory_item != default

  redis_item = @redis_adapter.fetch_item(
    klass: klass,
    cache_key: cache_key,
    default: default
  )

  return default if redis_item == default

  @memory_adapter.cache_item(
    klass: klass,
    cache_key: cache_key,
    item: redis_item
  )

  redis_item
end