Class: DeviceDetector::MemoryCache
- Inherits:
-
Object
- Object
- DeviceDetector::MemoryCache
- Defined in:
- lib/device_detector/memory_cache.rb
Constant Summary collapse
- DEFAULT_MAX_KEYS =
5000
- STORES_NIL_VALUE =
:__is_nil__
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#max_keys ⇒ Object
readonly
Returns the value of attribute max_keys.
Instance Method Summary collapse
- #get(key) ⇒ Object
- #get_or_set(key, value = nil) ⇒ Object
-
#initialize(config) ⇒ MemoryCache
constructor
A new instance of MemoryCache.
- #set(key, value) ⇒ Object
Constructor Details
#initialize(config) ⇒ MemoryCache
Returns a new instance of MemoryCache.
11 12 13 14 15 |
# File 'lib/device_detector/memory_cache.rb', line 11 def initialize(config) @data = {} @max_keys = config[:max_cache_keys] || DEFAULT_MAX_KEYS @lock = Mutex.new end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
8 9 10 |
# File 'lib/device_detector/memory_cache.rb', line 8 def data @data end |
#max_keys ⇒ Object (readonly)
Returns the value of attribute max_keys.
8 9 10 |
# File 'lib/device_detector/memory_cache.rb', line 8 def max_keys @max_keys end |
Instance Method Details
#get(key) ⇒ Object
27 28 29 30 |
# File 'lib/device_detector/memory_cache.rb', line 27 def get(key) value, _hit = get_hit(key) value end |
#get_or_set(key, value = nil) ⇒ Object
32 33 34 35 36 37 38 39 40 |
# File 'lib/device_detector/memory_cache.rb', line 32 def get_or_set(key, value = nil) string_key = String(key) result, hit = get_hit(string_key) return result if hit value = yield if block_given? set(string_key, value) end |
#set(key, value) ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/device_detector/memory_cache.rb', line 17 def set(key, value) lock.synchronize do purge_cache # convert nil values into symbol so we know a value is present cache_value = value.nil? ? STORES_NIL_VALUE : value data[String(key)] = cache_value value end end |