Class: BaseCache
- Inherits:
-
Object
- Object
- BaseCache
- Defined in:
- lib/libisi/cache/base.rb
Overview
Copyright © 2007-2010 Logintas AG Switzerland
This file is part of Libisi.
Libisi is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Libisi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Libisi. If not, see <www.gnu.org/licenses/>.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#maxcount ⇒ Object
Returns the value of attribute maxcount.
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #fetch(key, options = {}, &block) ⇒ Object
- #get(key) ⇒ Object
- #has_key?(key) ⇒ Boolean
-
#initialize(options = {}) ⇒ BaseCache
constructor
A new instance of BaseCache.
- #set(key, value) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ BaseCache
Returns a new instance of BaseCache.
21 22 23 24 |
# File 'lib/libisi/cache/base.rb', line 21 def initialize( = {}) @maxcount = ([:maxcount] or 1000) @cache = OrderedHash.new end |
Instance Attribute Details
#maxcount ⇒ Object
Returns the value of attribute maxcount.
20 21 22 |
# File 'lib/libisi/cache/base.rb', line 20 def maxcount @maxcount end |
Instance Method Details
#delete(key) ⇒ Object
59 60 61 |
# File 'lib/libisi/cache/base.rb', line 59 def delete(key) @cache.delete(key) end |
#fetch(key, options = {}, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/libisi/cache/base.rb', line 26 def fetch(key, = {}, &block) if has_key?(key) return get(key) else raise "No block given" unless block value = case block.arity when 0 yield when 1 yield(key) when 2 yield(key, ) else raise "Unexpected arity count of fetch block #{block.arity}" end set(key,value) value end end |
#get(key) ⇒ Object
47 48 49 |
# File 'lib/libisi/cache/base.rb', line 47 def get(key) @cache[key] end |
#has_key?(key) ⇒ Boolean
63 64 65 |
# File 'lib/libisi/cache/base.rb', line 63 def has_key?(key) @cache.has_key?(key) end |
#set(key, value) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/libisi/cache/base.rb', line 51 def set(key, value) while @cache.length >= maxcount @cache.delete(@cache.first.keys[0]) end @cache[key] = value end |