Class: Xampl::XamplCache
- Inherits:
-
Object
- Object
- Xampl::XamplCache
- Defined in:
- lib/xamplr/persisters/caches.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#capacity ⇒ Object
readonly
Returns the value of attribute capacity.
Instance Method Summary collapse
- #delete(key) ⇒ Object
- #fetch(key, default_value = nil) ⇒ Object (also: #[])
-
#initialize(capacity = DEFAULT_CAPACITY) ⇒ XamplCache
constructor
A new instance of XamplCache.
- #limit ⇒ Object
- #print(out = "") ⇒ Object
- #size ⇒ Object
- #store(key, value) ⇒ Object (also: #[]=)
Constructor Details
#initialize(capacity = DEFAULT_CAPACITY) ⇒ XamplCache
Returns a new instance of XamplCache.
19 20 21 22 23 24 |
# File 'lib/xamplr/persisters/caches.rb', line 19 def initialize(capacity=DEFAULT_CAPACITY) @capacity = capacity @now = 0 @size = 0 @cache = {} end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
17 18 19 |
# File 'lib/xamplr/persisters/caches.rb', line 17 def cache @cache end |
#capacity ⇒ Object (readonly)
Returns the value of attribute capacity.
17 18 19 |
# File 'lib/xamplr/persisters/caches.rb', line 17 def capacity @capacity end |
Instance Method Details
#delete(key) ⇒ Object
31 32 33 |
# File 'lib/xamplr/persisters/caches.rb', line 31 def delete(key) @cache.delete(key) end |
#fetch(key, default_value = nil) ⇒ Object Also known as: []
60 61 62 63 64 65 66 67 68 |
# File 'lib/xamplr/persisters/caches.rb', line 60 def fetch(key, default_value=nil) pair = @cache[key] if pair then pair[1] = (@now += 1) return pair[0] else return default_value end end |
#limit ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/xamplr/persisters/caches.rb', line 35 def limit victim = nil actual_victim = nil min = 1 + @now @size = 0 @cache.each do |key, pair| possibility = pair[0] if (not possibility.load_needed) and pair[1] < min then @size += 1 victim = key actual_victim = possibility min = pair[1] end end #puts #puts #puts "REMOVE FROM CACHE(XamplCache): victim: #{victim}, actual: #{actual_victim}" #puts " size: #{@size}, physical size: #{@cache.size}" #puts #puts #@cache.delete(victim) actual_victim.force_load if actual_victim #puts ">>>>>>> #{actual_victim.load_needed} #{actual_victim.class.name}" if actual_victim end |
#print(out = "") ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/xamplr/persisters/caches.rb', line 86 def print(out = "") out << "Cache (standard) with capacity: #{@capacity}, current size: #{@size}\n" @cache.each do |key, pair| out << sprintf(" key: '%s', value: '%s', accessed: %s\n", key, pair[0], pair[1]) end out end |
#size ⇒ Object
26 27 28 29 |
# File 'lib/xamplr/persisters/caches.rb', line 26 def size #@cache.size return @size end |
#store(key, value) ⇒ Object Also known as: []=
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/xamplr/persisters/caches.rb', line 70 def store(key, value) if (@capacity <= @size) then self.limit end pair = @cache[key] if pair then pair[0] = value pair[1] = (@now += 1) else @cache[key] = [value, (@now += 1)] end return value end |