Sha256: 50a07a9b268aeafdb2aea1061b6329e444366ba2a05591438a23a2b0b88c1334

Contents?: true

Size: 604 Bytes

Versions: 19

Compression:

Stored size: 604 Bytes

Contents

# -*- encoding : utf-8 -*-

require 'phrase'

class Phrase::Cache

  attr_accessor :lifetime

  def initialize(args={})
    @store = {}
    @lifetime = args.fetch(:lifetime, Phrase.cache_lifetime)
  end

  def cached?(cache_key)
    @store.has_key?(cache_key) && !expired?(cache_key)
  end

  def get(cache_key)
    begin
      @store.fetch(cache_key)[:payload]
    rescue
      nil
    end
  end

  def set(cache_key, value)
    @store[cache_key] = {timestamp: Time.now, payload: value}
  end

private
  def expired?(cache_key)
    @store.fetch(cache_key)[:timestamp] < (Time.now - @lifetime)
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
phrase-0.4.33 lib/phrase/cache.rb
phrase-0.4.32 lib/phrase/cache.rb
phrase-0.4.31 lib/phrase/cache.rb
phrase-0.4.30 lib/phrase/cache.rb
phrase-0.4.29 lib/phrase/cache.rb
phrase-0.4.28 lib/phrase/cache.rb
phrase-0.4.27 lib/phrase/cache.rb
phrase-0.4.26 lib/phrase/cache.rb
phrase-0.4.25 lib/phrase/cache.rb
phrase-0.4.24 lib/phrase/cache.rb
phrase-0.4.23 lib/phrase/cache.rb
phrase-0.4.22 lib/phrase/cache.rb
phrase-0.4.21 lib/phrase/cache.rb
phrase-0.4.20 lib/phrase/cache.rb
phrase-0.4.19 lib/phrase/cache.rb
phrase-0.4.18 lib/phrase/cache.rb
phrase-0.4.17 lib/phrase/cache.rb
phrase-0.4.16 lib/phrase/cache.rb
phrase-0.4.15 lib/phrase/cache.rb