Class: Garcon::MemStash
Overview
In-process cache with least-recently used (LRU) and time-to-live (TTL) expiration semantics. This implementation is thread-safe. It does not use a thread to clean up expired values. Instead, an expiration check is performed:
-
Every time you retrieve a value, against that value. If the value has expired, it will be removed and ‘nil` will be returned.
-
Every ‘expire_interval` operations as the cache is used to remove all expired values up to that point.
For manual expiration call #expire!.
Defined Under Namespace
Classes: Entry
Constant Summary collapse
- DEFAULT_TTL_SECONDS =
The maximum number of seconds an element can exist in the cache regardless of use. The element expires at this limit and will no longer be returned from the cache. The default value is 3600, or 1 hour. Setting A TTL value of 0 means no TTL eviction takes place (infinite lifetime).
3600
- DEFAULT_TTI_SECONDS =
The maximum number of seconds an element can exist in the cache without being accessed. The element expires at this limit and will no longer be returned from the cache. The default value is 3600, or 1 hour. Setting a TTI value of 0 means no TTI eviction takes place (infinite lifetime).
3600
- DEFAULT_MAX_ENTRIES =
The maximum sum total number of elements (cache entries) allowed on the disk tier for the cache. If this target is exceeded, eviction occurs to bring the count within the allowed target. The default value is 1,000. A setting of 0 means that no eviction of the cache’s entries takes place (infinite size is allowed), and consequently can cause the node to run out of disk space.
1_000
Instance Attribute Summary collapse
-
#:stats(: stats) ⇒ CacheStats
readonly
The Cache statistics.
-
#:tti (DEFAULT_TTI_SECONDS)(: tti(DEFAULT_TTI_SECONDS)) ⇒ Integer
readonly
The time to idle for an element before it expires.
-
#:ttl (DEFAULT_TTL_SECONDS)(: ttl(DEFAULT_TTL_SECONDS)) ⇒ Integer
readonly
The time to live for an element before it expires.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
-
#tti ⇒ Object
readonly
Returns the value of attribute tti.
-
#ttl ⇒ Object
readonly
Returns the value of attribute ttl.
Instance Method Summary collapse
-
#[](key) ⇒ Object?
(also: #get)
Retrieves a value from the cache.
-
#[]=(key, val) ⇒ Object?
(also: #set)
Stores a value in the cache.
-
#clear ⇒ self
Clears the cache.
-
#count ⇒ Integer
(also: #size, #length)
Returns the number of elements in the cache.
-
#delete(key) ⇒ Object?
Removes a value from the cache.
-
#each {|Array<key, value>| ... } ⇒ Enumerator, Array<key, value>
Allows iteration over the items in the cache.
-
#empty? ⇒ Boolean
Checks whether the cache is empty.
-
#expire! ⇒ self
Removes expired values from the cache.
-
#fetch(key) { ... } ⇒ Object
Retrieves a value from the cache, if available and not expired, or yields to a block that calculates the value to be stored in the cache.
-
#initialize(opts = {}) ⇒ MemStash
constructor
Initializes the cache.
-
#inspect ⇒ String
Returns information about the number of objects in the cache, its maximum size and TTL.
-
#keys ⇒ Array<String, Symbol>
Return all keys in the store as an array.
-
#load(data) ⇒ Object
Loads a hash of data into the stash.
Constructor Details
#initialize(opts = {}) ⇒ MemStash
Initializes the cache.
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/garcon/utility/memstash.rb', line 96 def initialize(opts = {}) @max_entries = opts.fetch(:max_entries, DEFAULT_MAX_ENTRIES) @ttl_seconds = opts.fetch(:ttl_seconds, DEFAULT_TTL_SECONDS) @tti_seconds = opts.fetch(:ttl_seconds, DEFAULT_TTI_SECONDS) @interval = opts.fetch(:interval, 100) @operations = 0 @monitor = Monitor.new @stash = {} @expires_at = {} end |
Instance Attribute Details
#:stats(: stats) ⇒ CacheStats (readonly)
Returns The Cache statistics.
68 |
# File 'lib/garcon/utility/memstash.rb', line 68 attr_reader :stats |
#:tti (DEFAULT_TTI_SECONDS)(: tti(DEFAULT_TTI_SECONDS)) ⇒ Integer (readonly)
Returns The time to idle for an element before it expires.
76 |
# File 'lib/garcon/utility/memstash.rb', line 76 attr_reader :tti |
#:ttl (DEFAULT_TTL_SECONDS)(: ttl(DEFAULT_TTL_SECONDS)) ⇒ Integer (readonly)
Returns The time to live for an element before it expires.
72 |
# File 'lib/garcon/utility/memstash.rb', line 72 attr_reader :ttl |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
68 69 70 |
# File 'lib/garcon/utility/memstash.rb', line 68 def stats @stats end |
#tti ⇒ Object (readonly)
Returns the value of attribute tti.
76 77 78 |
# File 'lib/garcon/utility/memstash.rb', line 76 def tti @tti end |
#ttl ⇒ Object (readonly)
Returns the value of attribute ttl.
72 73 74 |
# File 'lib/garcon/utility/memstash.rb', line 72 def ttl @ttl end |
Instance Method Details
#[](key) ⇒ Object? Also known as: get
Retrieves a value from the cache.
152 153 154 155 156 157 |
# File 'lib/garcon/utility/memstash.rb', line 152 def [](key) @monitor.synchronize do _, value = get(key) value end end |
#[]=(key, val) ⇒ Object? Also known as: set
Stores a value in the cache.
171 172 173 174 175 176 |
# File 'lib/garcon/utility/memstash.rb', line 171 def []=(key, val) @monitor.synchronize do expire! store(key, val) end end |
#clear ⇒ self
Clears the cache.
213 214 215 216 217 218 219 |
# File 'lib/garcon/utility/memstash.rb', line 213 def clear @monitor.synchronize do @stash.clear @expires_at.clear self end end |
#count ⇒ Integer Also known as: size, length
Calls to #empty? do not count against ‘expire_interval`. Therefore, the number of elements is that prior to any expiration.
Returns the number of elements in the cache.
230 231 232 |
# File 'lib/garcon/utility/memstash.rb', line 230 def count @monitor.synchronize { @stash.count } end |
#delete(key) ⇒ Object?
Removes a value from the cache.
187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/garcon/utility/memstash.rb', line 187 def delete(key) @monitor.synchronize do entry = @stash.delete(key) if entry @expires_at.delete(entry) entry.value else nil end end end |
#each {|Array<key, value>| ... } ⇒ Enumerator, Array<key, value>
The returned values could have expired by the time the client code gets to accessing them.
Because of its stability, this operation is very expensive. Use with caution.
Allows iteration over the items in the cache. Enumeration is stable: it is not affected by changes to the cache, including value expiration. Expired values are removed first.
254 255 256 257 258 259 |
# File 'lib/garcon/utility/memstash.rb', line 254 def each(&block) @monitor.synchronize do expire! @stash.map { |key, entry| [key, entry.value] }.each(&block) end end |
#empty? ⇒ Boolean
calls to #empty? do not count against ‘expire_interval`.
Checks whether the cache is empty.
205 206 207 |
# File 'lib/garcon/utility/memstash.rb', line 205 def empty? @monitor.synchronize { count == 0 } end |
#expire! ⇒ self
Removes expired values from the cache.
265 266 267 268 269 270 |
# File 'lib/garcon/utility/memstash.rb', line 265 def expire! @monitor.synchronize do check_expired(Time.now.to_f) self end end |
#fetch(key) { ... } ⇒ Object
Retrieves a value from the cache, if available and not expired, or yields to a block that calculates the value to be stored in the cache.
137 138 139 140 141 142 |
# File 'lib/garcon/utility/memstash.rb', line 137 def fetch(key) @monitor.synchronize do found, value = get(key) found ? value : store(key, yield) end end |
#inspect ⇒ String
Returns information about the number of objects in the cache, its maximum size and TTL.
285 286 287 288 289 290 |
# File 'lib/garcon/utility/memstash.rb', line 285 def inspect @monitor.synchronize do "<#{self.class.name} count=#{count} max_entries=#{@max_entries} " \ "ttl=#{@ttl_seconds}>" end end |
#keys ⇒ Array<String, Symbol>
Return all keys in the store as an array.
276 277 278 |
# File 'lib/garcon/utility/memstash.rb', line 276 def keys @monitor.synchronize { @stash.keys } end |
#load(data) ⇒ Object
Loads a hash of data into the stash.
114 115 116 117 118 119 120 121 |
# File 'lib/garcon/utility/memstash.rb', line 114 def load(data) @monitor.synchronize do data.each do |key, value| expire! store(key, val) end end end |