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.



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

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.



7
8
9
# File 'lib/delorean/cache/adapters/ruby_cache.rb', line 7

def lookup_cache
  @lookup_cache
end

#size_per_classObject (readonly)

Returns the value of attribute size_per_class.



7
8
9
# File 'lib/delorean/cache/adapters/ruby_cache.rb', line 7

def size_per_class
  @size_per_class
end

Instance Method Details

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



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

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



28
29
30
31
32
33
# File 'lib/delorean/cache/adapters/ruby_cache.rb', line 28

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



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

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

#clear_all!Object



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

def clear_all!
  @lookup_cache = {}
end

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



20
21
22
23
24
25
26
# File 'lib/delorean/cache/adapters/ruby_cache.rb', line 20

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