Class: Gin::Cache
- Inherits:
-
Object
- Object
- Gin::Cache
- Defined in:
- lib/gin/cache.rb
Overview
Gin::Cache is a bare-bones in-memory data store. It’s thread-safe and built for read-bound data. Reads are lock-less when no writes are queued.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get a value from the cache with the given key.
-
#[]=(key, val) ⇒ Object
Set a value in the cache with the given key and value.
-
#cache(key, value = nil, &block) ⇒ Object
Returns a cache value if it exists.
-
#clear ⇒ Object
Clear all cache entries.
-
#decrease(key, amount = 1) ⇒ Object
Decreases the value of the given key in a thread-safe manner.
-
#has_key?(key) ⇒ Boolean
Check if the current key exists in the cache.
-
#increase(key, amount = 1) ⇒ Object
Increases the value of the given key in a thread-safe manner.
-
#initialize ⇒ Cache
constructor
A new instance of Cache.
-
#write_timeout ⇒ Object
Get the write timeout when waiting for reader thread locks.
-
#write_timeout=(sec) ⇒ Object
Set the write timeout when waiting for reader thread locks.
Constructor Details
Instance Method Details
#[](key) ⇒ Object
Get a value from the cache with the given key.
43 44 45 |
# File 'lib/gin/cache.rb', line 43 def [] key @lock.read_sync{ @data[key] } end |
#[]=(key, val) ⇒ Object
Set a value in the cache with the given key and value.
51 52 53 |
# File 'lib/gin/cache.rb', line 51 def []= key, val @lock.write_sync{ @data[key] = val } end |
#cache(key, value = nil, &block) ⇒ Object
Returns a cache value if it exists. Otherwise, locks and assigns the provided value or block result. Blocks get executed with the write lock to prevent redundant operations.
97 98 99 100 |
# File 'lib/gin/cache.rb', line 97 def cache key, value=nil, &block return self[key] if self.has_key?(key) @lock.write_sync{ @data[key] = block_given? ? yield() : value } end |
#clear ⇒ Object
Clear all cache entries.
17 18 19 |
# File 'lib/gin/cache.rb', line 17 def clear @lock.write_sync{ @data.clear } end |
#decrease(key, amount = 1) ⇒ Object
Decreases the value of the given key in a thread-safe manner. Only Numerics and nil values are supported. Treats nil or non-existant values as 0.
75 76 77 78 79 80 81 |
# File 'lib/gin/cache.rb', line 75 def decrease key, amount=1 @lock.write_sync do return unless @data[key].nil? || Numeric === @data[key] @data[key] ||= 0 @data[key] -= amount end end |
#has_key?(key) ⇒ Boolean
Check if the current key exists in the cache.
87 88 89 |
# File 'lib/gin/cache.rb', line 87 def has_key? key @lock.read_sync{ @data.has_key? key } end |
#increase(key, amount = 1) ⇒ Object
Increases the value of the given key in a thread-safe manner. Only Numerics and nil values are supported. Treats nil or non-existant values as 0.
61 62 63 64 65 66 67 |
# File 'lib/gin/cache.rb', line 61 def increase key, amount=1 @lock.write_sync do return unless @data[key].nil? || Numeric === @data[key] @data[key] ||= 0 @data[key] += amount end end |
#write_timeout ⇒ Object
Get the write timeout when waiting for reader thread locks. See Gin::RWLock for more details.
35 36 37 |
# File 'lib/gin/cache.rb', line 35 def write_timeout @lock.write_timeout end |
#write_timeout=(sec) ⇒ Object
Set the write timeout when waiting for reader thread locks. Defaults to 0.05 sec. See Gin::RWLock for more details.
26 27 28 |
# File 'lib/gin/cache.rb', line 26 def write_timeout= sec @lock.write_timeout = sec end |