Class: Innate::LRUHash
- Inherits:
-
Struct
- Object
- Struct
- Innate::LRUHash
- Includes:
- Enumerable
- Defined in:
- /home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/lru_hash.rb
Overview
A Hash-alike LRU cache that provides fine-grained control over content restrictions.
It allows you to set: * a maximum number of elements * the maximum amount of memory used for all elements * the allowed memory-size per element * time to live
Differences to the original implementation include: * The Cache is now a Struct for speed
Copyright (C) 2002 Yoshinori K. Okuji okuji@enbug.org Copyright (c) 2009 Michael Fellinger manveru@rubyists.com
You may redistribute it and/or modify it under the same terms as Ruby.
Defined Under Namespace
Classes: CacheObject
Constant Summary
- KeyError =
On 1.8 we raise IndexError, on 1.9 we raise KeyError
Module.const_defined?(:KeyError) ? KeyError : IndexError
Instance Attribute Summary (collapse)
-
- (Object) expiration
Returns the value of attribute expiration.
-
- (Object) hook
Returns the value of attribute hook.
-
- (Object) list
Returns the value of attribute list.
-
- (Object) max_count
Returns the value of attribute max_count.
-
- (Object) objs
Returns the value of attribute objs.
Instance Method Summary (collapse)
-
- (Object) [](key)
-
- (Object) []=(key, obj)
-
- (Object) clear
(also: #invalidate_all)
-
- (Object) delete(key)
-
- (Object) expire
-
- (LRUHash) initialize(options = {}, &hook)
constructor
A new instance of LRUHash.
Constructor Details
- (LRUHash) initialize(options = {}, &hook)
Returns a new instance of LRUHash
26 27 28 29 30 31 32 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/lru_hash.rb', line 26 def initialize( = {}, &hook) self.max_count = [:max_count] self.expiration = [:expiration] self.hook = hook self.objs = {} self.list = [] end |
Instance Attribute Details
- (Object) expiration
Returns the value of attribute expiration
18 19 20 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/lru_hash.rb', line 18 def expiration @expiration end |
- (Object) hook
Returns the value of attribute hook
18 19 20 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/lru_hash.rb', line 18 def hook @hook end |
- (Object) list
Returns the value of attribute list
18 19 20 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/lru_hash.rb', line 18 def list @list end |
- (Object) max_count
Returns the value of attribute max_count
18 19 20 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/lru_hash.rb', line 18 def max_count @max_count end |
- (Object) objs
Returns the value of attribute objs
18 19 20 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/lru_hash.rb', line 18 def objs @objs end |
Instance Method Details
- (Object) [](key)
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/lru_hash.rb', line 63 def [](key) expire return unless objs.key?(key) obj = objs[key] obj.atime = Time.now.to_i list.delete_if{|list_key| key == list_key } list << key obj.content end |
- (Object) []=(key, obj)
77 78 79 80 81 82 83 84 85 86 87 88 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/lru_hash.rb', line 77 def []=(key, obj) expire delete key if objs.key?(key) delete list.first if max_count && max_count == list.size objs[key] = CacheObject.new(obj, size, Time.now.to_i) list << key obj end |
- (Object) clear Also known as: invalidate_all
46 47 48 49 50 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/lru_hash.rb', line 46 def clear objs.each{|key, obj| hook.call(key, obj) } if hook objs.clear list.clear end |
- (Object) delete(key)
34 35 36 37 38 39 40 41 42 43 44 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/lru_hash.rb', line 34 def delete(key) return unless objs.key?(key) obj = objs[key] hook.call(key, obj.content) if hook objs.delete key list.delete_if{|list_key| key == list_key } obj.content end |
- (Object) expire
53 54 55 56 57 58 59 60 61 |
# File '/home/manveru/github/ramaze/ramaze.net/tmp/git/innate/lib/innate/lru_hash.rb', line 53 def expire return unless expiration now = Time.now.to_i list.each_with_index do |key, index| break unless (objs[key].atime + expiration) <= now delete key end end |