Class: Sprockets::Cache
- Inherits:
-
Object
- Object
- Sprockets::Cache
- Defined in:
- lib/sprockets/cache.rb,
lib/sprockets/cache/file_store.rb,
lib/sprockets/cache/null_store.rb,
lib/sprockets/cache/memory_store.rb
Overview
Public: Wrapper interface to backend cache stores. Ensures a consistent API even when the backend uses get/set or read/write.
Public cache interface
Always assign the backend store instance to Environment#cache=.
environment.cache = Sprockets::Cache::MemoryStore.new(1000)
Environment#cache will always return a wrapped Cache interface. See the methods marked public on this class.
Backend cache interface
The Backend cache store must implement two methods.
get(key)
key - An opaque String with a length less than 250 characters.
Returns an JSON serializable object.
set(key, value)
Will only be called once per key. Setting a key "foo" with value "bar",
then later key "foo" with value "baz" is an undefined behavior.
key - An opaque String with a length less than 250 characters.
value - A JSON serializable object.
Returns argument value.
clear(options)
Clear the entire cache. Be careful with this method since it could
affect other processes if shared cache is being used.
The options hash is passed to the underlying cache implementation.
Defined Under Namespace
Classes: FileStore, GetWrapper, HashWrapper, MemoryStore, NullStore, ReadWriteWrapper, Wrapper
Constant Summary collapse
- VERSION =
Internal: Cache key version for this class. Rarely should have to change unless the cache format radically changes. Will be bump on major version releases though.
'4.0.0'
Class Method Summary collapse
Instance Method Summary collapse
-
#clear(options = nil) ⇒ Object
Public: Clear cache.
-
#fetch(key) ⇒ Object
Public: Prefer API to retrieve and set values in the cache store.
-
#get(key, local = false) ⇒ Object
Public: Low level API to retrieve item directly from the backend cache store.
-
#initialize(cache = nil, logger = self.class.default_logger) ⇒ Cache
constructor
Internal: Wrap a backend cache store.
-
#inspect ⇒ Object
Public: Pretty inspect.
-
#set(key, value, local = false) ⇒ Object
Public: Low level API to set item directly to the backend cache store.
Constructor Details
#initialize(cache = nil, logger = self.class.default_logger) ⇒ Cache
Internal: Wrap a backend cache store.
Always assign a backend cache store instance to Environment#cache= and use Environment#cache to retrieve a wrapped interface.
cache - A compatible backend cache store instance.
68 69 70 71 72 |
# File 'lib/sprockets/cache.rb', line 68 def initialize(cache = nil, logger = self.class.default_logger) @cache_wrapper = get_cache_wrapper(cache) @fetch_cache = Cache::MemoryStore.new(1024) @logger = logger end |
Class Method Details
.default_logger ⇒ Object
56 57 58 59 60 |
# File 'lib/sprockets/cache.rb', line 56 def self.default_logger logger = Logger.new($stderr) logger.level = Logger::FATAL logger end |
Instance Method Details
#clear(options = nil) ⇒ Object
Public: Clear cache
Returns truthy on success, potentially raises exception on failure
156 157 158 159 |
# File 'lib/sprockets/cache.rb', line 156 def clear(=nil) @cache_wrapper.clear @fetch_cache.clear end |
#fetch(key) ⇒ Object
Public: Prefer API to retrieve and set values in the cache store.
key - JSON serializable key block -
Must return a consistent JSON serializable object for the given key.
Examples
cache.fetch("foo") { "bar" }
Returns a JSON serializable object.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/sprockets/cache.rb', line 85 def fetch(key) start = Time.now.to_f = (key) value = @fetch_cache.get() if value.nil? value = @cache_wrapper.get() if value.nil? value = yield @cache_wrapper.set(, value) @logger.debug do ms = "(#{((Time.now.to_f - start) * 1000).to_i}ms)" "Sprockets Cache miss #{peek_key(key)} #{ms}" end end @fetch_cache.set(, value) end value end |
#get(key, local = false) ⇒ Object
Public: Low level API to retrieve item directly from the backend cache store.
This API may be used publicly, but may have undefined behavior depending on the backend store being used. Prefer the Cache#fetch API over using this.
key - JSON serializable key local - Check local cache first (default: false)
Returns a JSON serializable object or nil if there was a cache miss.
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/sprockets/cache.rb', line 115 def get(key, local = false) = (key) if local && value = @fetch_cache.get() return value end value = @cache_wrapper.get() @fetch_cache.set(, value) if local value end |
#inspect ⇒ Object
Public: Pretty inspect
Returns String.
149 150 151 |
# File 'lib/sprockets/cache.rb', line 149 def inspect "#<#{self.class} local=#{@fetch_cache.inspect} store=#{@cache_wrapper.cache.inspect}>" end |
#set(key, value, local = false) ⇒ Object
Public: Low level API to set item directly to the backend cache store.
This API may be used publicly, but may have undefined behavior depending on the backend store being used. Prefer the Cache#fetch API over using this.
key - JSON serializable key value - A consistent JSON serializable object for the given key. Setting
a different value for the given key has undefined behavior.
local - Set on local cache (default: false)
Returns the value argument.
140 141 142 143 144 |
# File 'lib/sprockets/cache.rb', line 140 def set(key, value, local = false) = (key) @fetch_cache.set(, value) if local @cache_wrapper.set(, value) end |