Class: Kisyo::Cache

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

Constant Summary collapse

CACHE_SIZE =
100

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



5
6
7
8
9
# File 'lib/kisyo/cache.rb', line 5

def initialize
  @keys = []
  @values = {}
  @m = Mutex.new
end

Instance Method Details

#get(key) ⇒ Object



11
12
13
# File 'lib/kisyo/cache.rb', line 11

def get(key)
  values[key]
end

#set(key, value) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kisyo/cache.rb', line 15

def set(key, value)
  m.synchronize do
    if values[key]
      return
    end

    keys << key
    values[key] = value

    if keys.size > CACHE_SIZE
      oldest_key = keys.shift
      values.delete(oldest_key)
    end
  end
end