Sha256: 86af8eb3e23a3e123fa42cae8b5cc8a955677b86e30175765fde1e9ae1dc3337

Contents?: true

Size: 1.47 KB

Versions: 9

Compression:

Stored size: 1.47 KB

Contents

# Defines the default cache engine for RABL when caching is invoked for a template.
# You can define your own caching engines by creating an object that responds to fetch and
# setting the configuration option:
#
#     config.cache_engine = AdvancedCacheEngine.new
#

class LRU < Hash
  attr_accessor :max_size

  def initialize
    super
    self.max_size = 100_000
  end

  def []= k,v
    r = super
    limit_size
    r
  end

  def limit_size
    if size > max_size then
      delete keys.shift while size > max_size
    end
  end
end

module Rabl
  class CacheEngine
    def initialize
      unless defined?(Rails)
        @cache = LRU.new
      end
    end


    # Fetch given a key and options and a fallback block attempts to find the key in the cache
    # and stores the block result in there if no key is found.
    #
    # cache = Rabl::CacheEngine.new; cache.fetch("some_key") { "fallback data" }
    #
    def fetch(key, cache_options, &block)
      if defined?(Rails)
        Rails.cache.fetch(key, cache_options, &block)
      else
        @cache[key] ||= yield
      end
    end

    def write(key, value, options = {})
      if defined?(Rails)
        Rails.cache.write(key, value, options)
      else
        @cache[key] = yield
      end
    end

    def read_multi(*keys)
      options = keys.extract_options!
      if defined?(Rails)
        Rails.cache.read_multi(*keys, options)
      else
        keys.inject({}) { |hash, key| hash[key] = nil; hash }
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rabl-0.17.0 lib/rabl/cache_engine.rb
rabl-0.16.1 lib/rabl/cache_engine.rb
rabl-0.16.0 lib/rabl/cache_engine.rb
rabl-0.15.0 lib/rabl/cache_engine.rb
rabl-0.14.5 lib/rabl/cache_engine.rb
rabl-0.14.4 lib/rabl/cache_engine.rb
rabl-0.14.3 lib/rabl/cache_engine.rb
rabl-0.14.2 lib/rabl/cache_engine.rb
rabl-0.14.1 lib/rabl/cache_engine.rb