Class: Tuktuk::Cache
- Inherits:
-
Object
- Object
- Tuktuk::Cache
- Defined in:
- lib/tuktuk/cache.rb
Instance Attribute Summary collapse
-
#max_keys ⇒ Object
readonly
Returns the value of attribute max_keys.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
- #get(key) ⇒ Object
-
#initialize(max_keys = 1000) ⇒ Cache
constructor
A new instance of Cache.
- #pop ⇒ Object
- #set(key, value) ⇒ Object
- #show ⇒ Object
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_keys ⇒ Object (readonly)
Returns the value of attribute max_keys.
5 6 7 |
# File 'lib/tuktuk/cache.rb', line 5 def max_keys @max_keys end |
#store ⇒ Object (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 |
#pop ⇒ Object
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 |
#show ⇒ Object
26 27 28 |
# File 'lib/tuktuk/cache.rb', line 26 def show store.each { |k,v| puts "#{k} -> #{v}" }; nil end |