Sha256: 58f82532382a093d2ea1eef1d8f71027e6ae3c4a341f68262674a8caad548004

Contents?: true

Size: 1.61 KB

Versions: 91

Compression:

Stored size: 1.61 KB

Contents

module Sprockets
  class Cache
    # 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
    #
    class MemoryStore
      # Internal: Default key limit for store.
      DEFAULT_MAX_SIZE = 1000

      # Public: Initialize the cache store.
      #
      # max_size - A Integer of the maximum number of keys the store will hold.
      #            (default: 1000).
      def initialize(max_size = DEFAULT_MAX_SIZE)
        @max_size = max_size
        @cache = {}
      end

      # 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.
      def get(key)
        exists = true
        value = @cache.delete(key) { exists = false }
        if exists
          @cache[key] = value
        else
          nil
        end
      end

      # 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.
      def set(key, value)
        @cache.delete(key)
        @cache[key] = value
        @cache.shift if @cache.size > @max_size
        value
      end

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

Version data entries

91 entries across 85 versions & 15 rubygems

Version Path
tdiary-5.0.9 vendor/bundle/gems/sprockets-3.7.2/lib/sprockets/cache/memory_store.rb
sprockets-3.7.2 lib/sprockets/cache/memory_store.rb
tdiary-5.0.8 vendor/bundle/gems/tdiary-5.0.7/vendor/bundle/gems/sprockets-3.7.1/lib/sprockets/cache/memory_store.rb
tdiary-5.0.8 vendor/bundle/gems/sprockets-3.7.1/lib/sprockets/cache/memory_store.rb
tdiary-5.0.8 vendor/bundle/ruby/2.5.0/gems/sprockets-3.7.1/lib/sprockets/cache/memory_store.rb
tdiary-5.0.7 vendor/bundle/gems/sprockets-3.7.1/lib/sprockets/cache/memory_store.rb
tdiary-5.0.6 vendor/bundle/gems/sprockets-3.7.1/lib/sprockets/cache/memory_store.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/sprockets-3.7.1/lib/sprockets/cache/memory_store.rb
tdiary-5.0.5 vendor/bundle/gems/sprockets-3.7.1/lib/sprockets/cache/memory_store.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/sprockets-3.7.1/lib/sprockets/cache/memory_store.rb
tdiary-5.0.4 vendor/bundle/gems/sprockets-3.7.1/lib/sprockets/cache/memory_store.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/sprockets-3.7.1/lib/sprockets/cache/memory_store.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/sprockets-3.7.1/lib/sprockets/cache/memory_store.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/sprockets-3.7.1/lib/sprockets/cache/memory_store.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/sprockets-3.7.1/lib/sprockets/cache/memory_store.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/sprockets-3.7.1/lib/sprockets/cache/memory_store.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/sprockets-3.7.1/lib/sprockets/cache/memory_store.rb
autocompl-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/sprockets-3.7.1/lib/sprockets/cache/memory_store.rb
abaci-0.3.0 vendor/bundle/gems/sprockets-3.6.3/lib/sprockets/cache/memory_store.rb
sprockets-3.7.1 lib/sprockets/cache/memory_store.rb