Class: Fontisan::Variation::ThreadSafeCache
- Inherits:
-
Cache
- Object
- Cache
- Fontisan::Variation::ThreadSafeCache
show all
- Defined in:
- lib/fontisan/variation/cache.rb
Overview
Thread-safe cache wrapper
Wraps Cache with Mutex for thread-safe operations.
Instance Attribute Summary
Attributes inherited from Cache
#max_size, #stats
Instance Method Summary
collapse
Methods inherited from Cache
#cached?, #empty?, #fetch_instance, #fetch_interpolated, #fetch_region_matches, #fetch_scalars, #full?, #invalidate_matching, #size
Constructor Details
#initialize(max_size: 1000, ttl: nil) ⇒ ThreadSafeCache
Returns a new instance of ThreadSafeCache.
245
246
247
248
|
# File 'lib/fontisan/variation/cache.rb', line 245
def initialize(max_size: 1000, ttl: nil)
super
@mutex = Mutex.new
end
|
Instance Method Details
#clear ⇒ Object
287
288
289
|
# File 'lib/fontisan/variation/cache.rb', line 287
def clear
@mutex.synchronize { super }
end
|
#fetch(key) ⇒ Object
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
# File 'lib/fontisan/variation/cache.rb', line 250
def fetch(key)
@mutex.synchronize do
if cached?(key)
@stats[:hits] += 1
touch(key)
return @cache[key][:value]
end
end
value = yield
@mutex.synchronize do
evict_if_needed
@cache[key] = {
value: value,
created_at: Time.now,
}
touch(key)
end
value
end
|
#invalidate(key) ⇒ Object
291
292
293
|
# File 'lib/fontisan/variation/cache.rb', line 291
def invalidate(key)
@mutex.synchronize { super }
end
|
#statistics ⇒ Object
295
296
297
|
# File 'lib/fontisan/variation/cache.rb', line 295
def statistics
@mutex.synchronize { super }
end
|
#store(key, value) ⇒ Object
276
277
278
279
280
281
282
283
284
285
|
# File 'lib/fontisan/variation/cache.rb', line 276
def store(key, value)
@mutex.synchronize do
evict_if_needed
@cache[key] = {
value: value,
created_at: Time.now,
}
touch(key)
end
end
|