Class: Tuktuk::Cache

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_keys = 1000) ⇒ Cache

Returns a new instance of Cache.



7
8
9
10
# File 'lib/tuktuk/cache.rb', line 7

def initialize(max_keys = 1000)
  @store = {}
  @max_keys = max_keys
end

Instance Attribute Details

#max_keysObject (readonly)

Returns the value of attribute max_keys.



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

def max_keys
  @max_keys
end

#storeObject (readonly)

Returns the value of attribute store.



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

def store
  @store
end

Instance Method Details

#get(key) ⇒ Object



12
13
14
# File 'lib/tuktuk/cache.rb', line 12

def get(key)
  store[key]
end

#popObject



22
23
24
# File 'lib/tuktuk/cache.rb', line 22

def pop
  store.delete(store.keys.last)
end

#set(key, value) ⇒ Object



16
17
18
19
20
# File 'lib/tuktuk/cache.rb', line 16

def set(key, value)
  return if store[key] == value
  pop if store.length > max_keys
  store[key] = value
end

#showObject



26
27
28
# File 'lib/tuktuk/cache.rb', line 26

def show
  store.each { |k,v| puts "#{k} -> #{v}" }; nil
end