123456789_123456789_123456789_123456789_123456789_

Class: Sprockets::Cache::MemoryStore

Relationships & Source Files
Inherits: Object
Defined in: lib/sprockets/cache/memory_store.rb

Overview

Public: Basic in memory LRU cache.

Assign the instance to the Environment#cache.

environment.cache = Sprockets::Cache::MemoryStore.new(1000)

See Also

ActiveSupport::Cache::MemoryStore

Constant Summary

Class Method Summary

Instance Method Summary

Constructor Details

.new(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).
[ GitHub ]

  
# 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)

Public: Clear the cache

Returns true

[ GitHub ]

  
# File 'lib/sprockets/cache/memory_store.rb', line 76

def clear(options=nil)
  @mutex.synchronize do
    @cache.clear
  end
  true
end

#get(key)

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.

[ GitHub ]

  
# 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

Public: Pretty inspect

Returns String.

[ GitHub ]

  
# 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)

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.

[ GitHub ]

  
# 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