class Sprockets::Cache::MemoryStore


ActiveSupport::Cache::MemoryStore
See Also
environment.cache = Sprockets::Cache::MemoryStore.new(1000)
Assign the instance to the Environment#cache.
Public: Basic in memory LRU cache.

def clear(options=nil)

Returns true

Public: Clear the cache
def clear(options=nil)
  @mutex.synchronize do
    @cache.clear
  end
  true
end

def get(key)

Returns Object or nil or the value is not set.

key - String cache key.

This API should not be used directly, but via the Cache wrapper API.

Public: Retrieve value from cache.
def get(key)
  @mutex.synchronize do
    exists = true
    value = @cache.delete(key) { exists = false }
    if exists
      @cache[key] = value
    else
      nil
    end
  end
end

def initialize(max_size = DEFAULT_MAX_SIZE)

(default: 1000).
max_size - A Integer of the maximum number of keys the store will hold.

Public: Initialize the cache store.
def initialize(max_size = DEFAULT_MAX_SIZE)
  @max_size = max_size
  @cache = {}
  @mutex = Mutex.new
end

def inspect

Returns String.

Public: Pretty inspect
def inspect
  @mutex.synchronize do
    "#<#{self.class} size=#{@cache.size}/#{@max_size}>"
  end
end

def set(key, value)

Returns Object value.

value - Object value.
key - String cache key.

This API should not be used directly, but via the Cache wrapper API.

Public: Set a key and value in the cache.
def set(key, value)
  @mutex.synchronize do
    @cache.delete(key)
    @cache[key] = value
    @cache.shift if @cache.size > @max_size
  end
  value
end