Class: Valise::Cache::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/valise/cache.rb

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



6
7
8
# File 'lib/valise/cache.rb', line 6

def initialize
  @hash = {}
end

Instance Method Details

#[](key) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/valise/cache.rb', line 14

def [](key)
  ref = @hash.fetch(key) do
    return nil
  end
  return live_ref(key, ref)
rescue WeakRef::RefError
  nil
end

#[]=(key, object) ⇒ Object



10
11
12
# File 'lib/valise/cache.rb', line 10

def []=(key, object)
  @hash[key] = WeakRef.new(object)
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
# File 'lib/valise/cache.rb', line 30

def has_key?(key)
  ref = @hash.fetch(key) do
    false
  end
  live_ref(key, ref)
  true
rescue WeakRef::RefError
  false
end

#live_ref(key, ref) ⇒ Object



23
24
25
26
27
28
# File 'lib/valise/cache.rb', line 23

def live_ref(key, ref)
  ref.__getobj__
rescue WeakRef::RefError
  @hash.delete(key)
  raise
end