Class: SimpleCache

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

Overview

SimpleCache is a very simple cache store.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {:max_size => 10}) ⇒ SimpleCache

Create a new cache store:

SimpleCache.new(:max_size => 100)

to set the size of the cache



10
11
12
13
# File 'lib/simple_cache.rb', line 10

def initialize(options = {:max_size => 10})
  @options = options
  @store = {}
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/simple_cache.rb', line 4

def options
  @options
end

#storeObject

Returns the value of attribute store.



5
6
7
# File 'lib/simple_cache.rb', line 5

def store
  @store
end

Instance Method Details

#cache!(key, value) ⇒ Object

Cache an item



21
22
23
24
25
26
27
# File 'lib/simple_cache.rb', line 21

def cache!(key, value)
  if @store.size >= @options[:max_size]
    @store.delete(@store.keys.first)
  end
    
  @store[key] = value
end

#has_key?(key) ⇒ Boolean

Does it have this key

Returns:

  • (Boolean)


16
17
18
# File 'lib/simple_cache.rb', line 16

def has_key?(key)
  !@store[key].nil?
end

#value(key) ⇒ Object

Get value of an key



31
32
33
# File 'lib/simple_cache.rb', line 31

def value(key)
  @store[key]
end