Class: MiniCache::Store

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mini_cache/store.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Store

Public: Initializes a new MiniCache object.

data - A Hash of key-value pairs (optional).

The values can be String or MiniCache::Data.

Returns nothing.



16
17
18
19
# File 'lib/mini_cache/store.rb', line 16

def initialize(data = {})
  @data = {}
  load(data)
end

Instance Attribute Details

#dataObject (readonly)

Public: Returns the hash of key-value pairs.



8
9
10
# File 'lib/mini_cache/store.rb', line 8

def data
  @data
end

Instance Method Details

#eachObject

Public: Iterates over all key-value pairs.

block - A block of code that will be send the key

and value of each pair.

Yields the String key and value.



140
141
142
# File 'lib/mini_cache/store.rb', line 140

def each
  @data.each { |k, v| yield(k, v) }
end

#get(key) ⇒ Object

Public: Retrieves the value for a given key.

key - A String or Symbol representing the key.

Returns the value set for the key; if nothing is

set, returns nil.


27
28
29
30
31
# File 'lib/mini_cache/store.rb', line 27

def get(key)
  check_key!(key)
  expires!(key)
  @data[key.to_s]&.value
end

#get_or_set(key, value = nil, expires_in: nil) ⇒ Object

Public: Retrieves the value for a given key if it has already been set; otherwise, sets the value either as an argument or block.

key - A String or Symbol representing the key. value - Any object that represents the value (optional).

The value can be a MiniCache::Data.
Not used if a block is given.

block - A block of code that returns the value to set (optional).

Can be set a MiniCache::Data in the block.

expires_in - Time, in seconds, to expire the cache (optional).

If not set, the cache never expires.

Examples

cache.set("name", "Derrick")
=> "Derrick"

cache.get_or_set("name", "Joe")
=> "Derrick"

cache.get_or_set("occupation") { "Engineer" }
=> "Engineer"

cache.get_or_set("occupation") { "Pilot" }
=> "Engineer"

Returns the value.



111
112
113
114
# File 'lib/mini_cache/store.rb', line 111

def get_or_set(key, value = nil, expires_in: nil)
  return get(key) if set?(key)
  set(key, block_given? ? yield : value, expires_in: expires_in)
end

#load(data) ⇒ Object

Public: Loads a hash of data into the cache.

data - A Hash of data with either String or Symbol keys.

Returns nothing.



149
150
151
152
153
154
# File 'lib/mini_cache/store.rb', line 149

def load(data)
  data.each do |key, value|
    check_key!(key)
    set(key, value)
  end
end

#resetObject

Public: Clears all key-value pairs.

Returns nothing.



130
131
132
# File 'lib/mini_cache/store.rb', line 130

def reset
  @data = {}
end

#set(key, value = nil, expires_in: nil) ⇒ Object

Public: Sets a value for a given key either as an argument or block.

key - A String or Symbol representing the key. value - Any object that represents the value (optional).

The value can be a MiniCache::Data.
Not used if a block is given.

block - A block of code that returns the value to set (optional).

Can be set a MiniCache::Data in the block.

expires_in - Time, in seconds, to expire the cache (optional).

If not set, the cache never expires.

Examples

cache.set("name", "Derrick")
=> "Derrick"

cache.set("name", "Derrick", expires_in: 60)
=> "Derrick"

cache.set("name") { "Joe" }
=> "Joe"

cache.set("name") { MiniCache::Data.new("Joe", 60) }
=> "Joe"

Returns the value given.



60
61
62
63
64
65
66
67
68
69
# File 'lib/mini_cache/store.rb', line 60

def set(key, value = nil, expires_in: nil)
  check_key!(key)
  data = block_given? ? yield : value
  @data[key.to_s] = if data.is_a?(MiniCache::Data)
                      data
                    else
                      MiniCache::Data.new(data, expires_in: expires_in)
                    end
  get(key)
end

#set?(key) ⇒ Boolean

Public: Determines whether a value has been set for a given key.

key - A String or Symbol representing the key.

Returns a Boolean.

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/mini_cache/store.rb', line 77

def set?(key)
  check_key!(key)
  expires!(key)
  @data.keys.include?(key.to_s)
end

#unset(key) ⇒ Object

Public: Removes the key-value pair from the cache for a given key.

key - A String or Symbol representing the key.

Returns the value.



122
123
124
125
# File 'lib/mini_cache/store.rb', line 122

def unset(key)
  check_key!(key)
  @data.delete(key.to_s)
end