Class: Delorean::Cache::Adapters::RubyCache

Inherits:
Base
  • Object
show all
Defined in:
lib/delorean/cache/adapters/ruby_cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#cache_item?

Constructor Details

#initialize(size_per_class: 1000) ⇒ RubyCache

Returns a new instance of RubyCache.



11
12
13
14
# File 'lib/delorean/cache/adapters/ruby_cache.rb', line 11

def initialize(size_per_class: 1000)
  @lookup_cache = {}
  @size_per_class = size_per_class
end

Instance Attribute Details

#lookup_cacheObject (readonly)

Returns the value of attribute lookup_cache.



9
10
11
# File 'lib/delorean/cache/adapters/ruby_cache.rb', line 9

def lookup_cache
  @lookup_cache
end

#size_per_classObject (readonly)

Returns the value of attribute size_per_class.



9
10
11
# File 'lib/delorean/cache/adapters/ruby_cache.rb', line 9

def size_per_class
  @size_per_class
end

Instance Method Details

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



16
17
18
19
20
# File 'lib/delorean/cache/adapters/ruby_cache.rb', line 16

def cache_item(klass:, cache_key:, item:)
  lookup_cache[klass] ||= {}
  clear_outdated_items(klass: klass)
  lookup_cache[klass][cache_key] = item
end

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



32
33
34
35
36
37
38
# File 'lib/delorean/cache/adapters/ruby_cache.rb', line 32

def cache_key(klass:, method_name:, args:)
  [method_name] + args.map do |arg|
    next arg.id if arg.respond_to?(:id)

    arg
  end.freeze
end

#clear!(klass:) ⇒ Object



40
41
42
# File 'lib/delorean/cache/adapters/ruby_cache.rb', line 40

def clear!(klass:)
  lookup_cache[klass] = {}
end

#clear_all!Object



44
45
46
# File 'lib/delorean/cache/adapters/ruby_cache.rb', line 44

def clear_all!
  @lookup_cache = {}
end

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



22
23
24
25
26
27
28
29
30
# File 'lib/delorean/cache/adapters/ruby_cache.rb', line 22

def fetch_item(klass:, cache_key:, default: nil)
  subh = lookup_cache.fetch(klass, default)
  return default if subh == default

  v = subh.fetch(cache_key, default)
  return default if v == default

  v
end