Class: TimedLRU
- Inherits:
-
Object
- Object
- TimedLRU
- Extended by:
- Forwardable
- Includes:
- MonitorMixin
- Defined in:
- lib/timed_lru.rb
Defined Under Namespace
Modules: ThreadUnsafe Classes: Node
Instance Attribute Summary collapse
- #max_size ⇒ Object readonly
- #ttl ⇒ Object readonly
Instance Method Summary collapse
-
#delete(key) ⇒ Object, NilClass
Deletes by ‘key`.
-
#fetch(key) ⇒ Object, NilClass
(also: #[])
Retrieves a ‘value` by `key`.
-
#initialize(opts = {}) ⇒ TimedLRU
constructor
A new instance of TimedLRU.
-
#store(key, value) ⇒ Object
(also: #[]=)
Stores a ‘value` by `key`.
Constructor Details
#initialize(opts = {}) ⇒ TimedLRU
Returns a new instance of TimedLRU.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/timed_lru.rb', line 31 def initialize(opts = {}) super() # MonitorMixin @hash = {} @max_size = Integer(opts[:max_size] || 100) @ttl = Integer(opts[:ttl]) if opts[:ttl] raise ArgumentError, "Option :max_size must be > 0" unless max_size > 0 raise ArgumentError, "Option :ttl must be > 0" unless ttl.nil? || ttl > 0 extend ThreadUnsafe if opts[:thread_safe] == false end |
Instance Attribute Details
#max_size ⇒ Object (readonly)
18 19 20 |
# File 'lib/timed_lru.rb', line 18 def max_size @max_size end |
#ttl ⇒ Object (readonly)
21 22 23 |
# File 'lib/timed_lru.rb', line 21 def ttl @ttl end |
Instance Method Details
#delete(key) ⇒ Object, NilClass
Deletes by ‘key`
76 77 78 79 80 81 |
# File 'lib/timed_lru.rb', line 76 def delete(key) mon_synchronize do node = @hash[key] remove(node).value if node end end |
#fetch(key) ⇒ Object, NilClass Also known as: []
Retrieves a ‘value` by `key`
62 63 64 65 66 67 68 69 70 |
# File 'lib/timed_lru.rb', line 62 def fetch(key) mon_synchronize do node = @hash[key] break unless node touch(node) node.value end end |