Class: Zache
- Inherits:
-
Object
- Object
- Zache
- Defined in:
- lib/zache.rb
Overview
It is a very simple thread-safe in-memory cache with an ability to expire keys automatically, when their lifetime is over. Use it like this:
require 'zache'
zache = Zache.new
# Expires in 5 minutes
v = zache.get(:count, lifetime: 5 * 60) { expensive() }
For more information read README file.
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2018 Yegor Bugayenko
- License
-
MIT
Instance Method Summary collapse
-
#clean ⇒ Object
Remove keys that are expired.
-
#exists?(key) ⇒ Boolean
Checks whether the value exists in the cache by the provided key.
-
#get(key, lifetime: 2**32, dirty: false) ⇒ Object
Gets the value from the cache by the provided key.
-
#initialize(sync: true, dirty: false) ⇒ Zache
constructor
Makes a new object of the cache.
-
#locked? ⇒ Boolean
Is cache currently locked doing something?.
-
#put(key, value, lifetime: 2**32) ⇒ Object
Put a value into the cache.
-
#remove(key) ⇒ Object
Removes the value from the hash, by the provied key.
-
#remove_all ⇒ Object
Remove all keys from the cache.
Constructor Details
#initialize(sync: true, dirty: false) ⇒ Zache
Makes a new object of the cache. “sync” is whether the hash is thread-safe (‘true`) or not (`false`); it is recommended to leave this parameter untouched, unless you really know what you are doing.
If the dirty
argument is set to true
, a previously calculated result will be returned if it exists and is already expired.
49 50 51 52 53 54 |
# File 'lib/zache.rb', line 49 def initialize(sync: true, dirty: false) @hash = {} @sync = sync @dirty = dirty @monitor = Monitor.new end |
Instance Method Details
#clean ⇒ Object
Remove keys that are expired.
121 122 123 |
# File 'lib/zache.rb', line 121 def clean synchronized { @hash.delete_if { |_key, value| key_expired?(value) } } end |
#exists?(key) ⇒ Boolean
Checks whether the value exists in the cache by the provided key. Returns TRUE if the value is here. If the key is already expired in the hash, it will be removed by this method and the result will be FALSE.
84 85 86 87 88 89 90 91 |
# File 'lib/zache.rb', line 84 def exists?(key) rec = @hash[key] if key_expired?(key) @hash.delete(key) rec = nil end !rec.nil? end |
#get(key, lifetime: 2**32, dirty: false) ⇒ Object
Gets the value from the cache by the provided key. If the value is not found in the cache, it will be calculated via the provided block. If the block is not given, an exception will be raised. The lifetime must be in seconds. The default lifetime is huge, which means that the key will never be expired.
If the dirty
argument is set to true
, a previously calculated result will be returned if it exists and is already expired.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/zache.rb', line 64 def get(key, lifetime: 2**32, dirty: false) if block_given? if (dirty || @dirty) && locked? && key_expired?(key) && @hash.key?(key) return @hash[key][:value] end synchronized { calc(key, lifetime) { yield } } else rec = @hash[key] if key_expired?(key) @hash.delete(key) rec = nil end raise 'The key is absent in the cache' if rec.nil? rec[:value] end end |
#locked? ⇒ Boolean
Is cache currently locked doing something?
94 95 96 |
# File 'lib/zache.rb', line 94 def locked? !@monitor.mon_try_enter { true } end |
#put(key, value, lifetime: 2**32) ⇒ Object
Put a value into the cache.
99 100 101 102 103 104 105 106 107 |
# File 'lib/zache.rb', line 99 def put(key, value, lifetime: 2**32) synchronized do @hash[key] = { value: value, start: Time.now, lifetime: lifetime } end end |
#remove(key) ⇒ Object
Removes the value from the hash, by the provied key. If the key is absent and the block is provide, the block will be called.
111 112 113 |
# File 'lib/zache.rb', line 111 def remove(key) synchronized { @hash.delete(key) { yield if block_given? } } end |
#remove_all ⇒ Object
Remove all keys from the cache.
116 117 118 |
# File 'lib/zache.rb', line 116 def remove_all synchronized { @hash = {} } end |