Class: Sprockets::Cache::MemoryStore
- Inherits:
-
Object
- Object
- Sprockets::Cache::MemoryStore
- Defined in:
- lib/sprockets/cache/memory_store.rb
Overview
Constant Summary collapse
- DEFAULT_MAX_SIZE =
Internal: Default key limit for store.
1000
Instance Method Summary collapse
-
#clear(options = nil) ⇒ Object
Public: Clear the cache.
-
#get(key) ⇒ Object
Public: Retrieve value from cache.
-
#initialize(max_size = DEFAULT_MAX_SIZE) ⇒ MemoryStore
constructor
Public: Initialize the cache store.
-
#inspect ⇒ Object
Public: Pretty inspect.
-
#set(key, value) ⇒ Object
Public: Set a key and value in the cache.
Constructor Details
#initialize(max_size = DEFAULT_MAX_SIZE) ⇒ MemoryStore
Public: Initialize the cache store.
max_size - A Integer of the maximum number of keys the store will hold.
(default: 1000).
22 23 24 25 26 |
# File 'lib/sprockets/cache/memory_store.rb', line 22 def initialize(max_size = DEFAULT_MAX_SIZE) @max_size = max_size @cache = {} @mutex = Mutex.new end |
Instance Method Details
#clear(options = nil) ⇒ Object
Public: Clear the cache
Returns true
76 77 78 79 80 81 |
# File 'lib/sprockets/cache/memory_store.rb', line 76 def clear(=nil) @mutex.synchronize do @cache.clear end true end |
#get(key) ⇒ Object
Public: Retrieve value from cache.
This API should not be used directly, but via the Cache wrapper API.
key - String cache key.
Returns Object or nil or the value is not set.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/sprockets/cache/memory_store.rb', line 35 def get(key) @mutex.synchronize do exists = true value = @cache.delete(key) { exists = false } if exists @cache[key] = value else nil end end end |
#inspect ⇒ Object
Public: Pretty inspect
Returns String.
67 68 69 70 71 |
# File 'lib/sprockets/cache/memory_store.rb', line 67 def inspect @mutex.synchronize do "#<#{self.class} size=#{@cache.size}/#{@max_size}>" end end |
#set(key, value) ⇒ Object
Public: Set a key and value in the cache.
This API should not be used directly, but via the Cache wrapper API.
key - String cache key. value - Object value.
Returns Object value.
55 56 57 58 59 60 61 62 |
# File 'lib/sprockets/cache/memory_store.rb', line 55 def set(key, value) @mutex.synchronize do @cache.delete(key) @cache[key] = value @cache.shift if @cache.size > @max_size end value end |