Class: Fino::Cache::Memory

Inherits:
Object
  • Object
show all
Includes:
Fino::Cache
Defined in:
lib/fino/cache/memory.rb

Instance Method Summary collapse

Constructor Details

#initialize(expires_in:) ⇒ Memory

Returns a new instance of Memory.



6
7
8
9
# File 'lib/fino/cache/memory.rb', line 6

def initialize(expires_in:)
  @hash = {}
  @expirator = Fino::Expirator.new(ttl: expires_in) if expires_in
end

Instance Method Details

#clearObject



59
60
61
# File 'lib/fino/cache/memory.rb', line 59

def clear
  hash.clear
end

#delete(key) ⇒ Object



55
56
57
# File 'lib/fino/cache/memory.rb', line 55

def delete(key)
  hash.delete(key)
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
# File 'lib/fino/cache/memory.rb', line 11

def exist?(key)
  expire_if_ready

  hash.key?(key)
end

#fetch(key, &block) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
# File 'lib/fino/cache/memory.rb', line 27

def fetch(key, &block)
  raise ArgumentError, "no block provided to #{self.class.name}#fetch" unless block

  expire_if_ready

  hash.fetch(key) do
    write(key, block.call)
  end
end

#fetch_multi(*keys, &block) ⇒ Object

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fino/cache/memory.rb', line 37

def fetch_multi(*keys, &block)
  raise ArgumentError, "no block provided to #{self.class.name}#fetch_multi" unless block

  expire_if_ready

  missing_keys = keys - hash.keys

  if missing_keys.any?
    results = block.call(missing_keys)

    results.each do |key, value|
      write(key, value)
    end
  end

  hash.values_at(*keys)
end

#read(key) ⇒ Object



17
18
19
20
21
# File 'lib/fino/cache/memory.rb', line 17

def read(key)
  expire_if_ready

  hash[key]
end

#write(key, value) ⇒ Object



23
24
25
# File 'lib/fino/cache/memory.rb', line 23

def write(key, value)
  hash[key] = value
end