Sha256: ccb5b10fb0562635cd92ff95bc03f2f4306773a63d93f34f327003775c5bde0d

Contents?: true

Size: 1.32 KB

Versions: 6

Compression:

Stored size: 1.32 KB

Contents

description  'Caching support'
dependencies 'utils/worker', 'utils/store'

class Cache
  def initialize(store)
    @store = store
    @disabled = false
  end

  def disable!
    @disabled = true
  end

  # Block around cacheable return value identified by a <i>key</i>.
  # The following options can be specified:
  # * :disable Disable caching
  # * :update  Force cache update
  # * :defer   Deferred cache update
  def cache(key, options = {}, &block)
    return yield(self) if options[:disable] || !Config['production']

    # Warning: don't change this. This must be thread safe!
    if options[:update]
      if options[:defer] && ((value = @store[key]) || @store.key?(key)) # Check key? because value could be nil
        Worker.defer { update(key, options, &block) }
        return value
      end
    else
      return value if (value = @store[key]) || @store.key?(key) # Check key? because value could be nil
    end
    update(key, options, &block)
  end

  def clear
    @store.clear
  end

  private

  def update(key, options = {}, &block)
    content = block.call(self)
    @store[key] = content if !@disabled
    content
  end

  class<< self
    def store
      @store ||= Store.create(Config['cache_store'])
    end

    def cache(*args, &block)
      Cache.new(store).cache(*args, &block)
    end
  end
end

Olelo::Cache = Cache

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
olelo-0.9.9 plugins/utils/cache.rb
olelo-0.9.8 plugins/utils/cache.rb
olelo-0.9.7 plugins/utils/cache.rb
olelo-0.9.6 plugins/utils/cache.rb
olelo-0.9.5 plugins/utils/cache.rb
olelo-0.9.4 plugins/utils/cache.rb