Class: Shallow::Cache

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/shallow.rb

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



36
37
38
# File 'lib/shallow.rb', line 36

def initialize
  @data_store = {}
end

Instance Method Details

#delete(key) ⇒ Object



50
51
52
# File 'lib/shallow.rb', line 50

def delete(key)
  @data_store.delete(key)
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/shallow.rb', line 40

def exist?(key)
  @data_store.key?(key)
end

#fetch(key) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/shallow.rb', line 43

def fetch(key)
  if @data_store.key?(key)
    @data_store.fetch(key)
  else
    @data_store[key] = yield
  end
end

#read(key) ⇒ Object



53
54
55
# File 'lib/shallow.rb', line 53

def read(key)
  @data_store.fetch(key)
end