Sha256: 8bc1f697a123290a3e64174ffad9c030f86babbe9081903e8bcb1f8ef92ae2cc

Contents?: true

Size: 789 Bytes

Versions: 16

Compression:

Stored size: 789 Bytes

Contents

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

require 'phraseapp-in-context-editor-ruby'

module PhraseApp
  module InContextEditor
    class Cache
      attr_accessor :lifetime

      def initialize(args={})
        @store = {}
        @lifetime = args.fetch(:lifetime, PhraseApp::InContextEditor.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
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
phraseapp-in-context-editor-ruby-1.4.0 lib/phraseapp-in-context-editor-ruby/cache.rb
phraseapp-in-context-editor-ruby-1.3.1 lib/phraseapp-in-context-editor-ruby/cache.rb
phraseapp-in-context-editor-ruby-1.3.0 lib/phraseapp-in-context-editor-ruby/cache.rb
phraseapp-in-context-editor-ruby-1.2.1 lib/phraseapp-in-context-editor-ruby/cache.rb
phraseapp-in-context-editor-ruby-1.2.0 lib/phraseapp-in-context-editor-ruby/cache.rb
phraseapp-in-context-editor-ruby-1.1.0 lib/phraseapp-in-context-editor-ruby/cache.rb
phraseapp-in-context-editor-ruby-1.0.6 lib/phraseapp-in-context-editor-ruby/cache.rb
phraseapp-in-context-editor-ruby-1.0.5 lib/phraseapp-in-context-editor-ruby/cache.rb
phraseapp-in-context-editor-ruby-1.0.4 lib/phraseapp-in-context-editor-ruby/cache.rb
phraseapp-in-context-editor-ruby-1.0.3 lib/phraseapp-in-context-editor-ruby/cache.rb
phraseapp-in-context-editor-ruby-1.0.2 lib/phraseapp-in-context-editor-ruby/cache.rb
phraseapp-in-context-editor-ruby-1.0.1 lib/phraseapp-in-context-editor-ruby/cache.rb
phraseapp-in-context-editor-ruby-1.0.0 lib/phraseapp-in-context-editor-ruby/cache.rb
phraseapp-in-context-editor-ruby-1.0.0rc3 lib/phraseapp-in-context-editor-ruby/cache.rb
phraseapp-in-context-editor-ruby-1.0.0rc2 lib/phraseapp-in-context-editor-ruby/cache.rb
phraseapp-in-context-editor-ruby-1.0.0rc1 lib/phraseapp-in-context-editor-ruby/cache.rb