Sha256: 9ad7a02585c4310ff7dc49f1fc7e8bcfc49d199f36d69d89f1ce36492972d193

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

require 'forwardable'
require 'fileutils'

module TwitterFriendly
  class Cache
    extend Forwardable
    def_delegators :@client, :clear, :cleanup

    def initialize(*args)
      options = {expires_in: 1.hour, race_condition_ttl: 5.minutes}.merge(args.extract_options!)

      path = options[:cache_dir] || File.join('.twitter_friendly', 'cache')
      FileUtils.mkdir_p(path) unless File.exists?(path)
      @client = ::ActiveSupport::Cache::FileStore.new(path, options)
    end

    # @param key [String]
    #
    # @option serialize_options [Array] :args
    def fetch(key, serialize_options, &block)
      block_result = nil
      yield_and_encode =
          Proc.new do
            block_result = yield
            encode(block_result, serialize_options)
          end

      fetch_result = @client.fetch(key, &yield_and_encode)

      block_result || decode(fetch_result, serialize_options)
    end

    private

    # @option options [Array] :args
    def encode(obj, options)
      Serializer.encode(obj, options)
    end

    # @option options [Array] :args
    def decode(str, options)
      Serializer.decode(str, options)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
twitter_friendly-1.2.0 lib/twitter_friendly/cache.rb
twitter_friendly-1.1.0 lib/twitter_friendly/cache.rb
twitter_friendly-1.0.0 lib/twitter_friendly/cache.rb
twitter_friendly-0.3.0 lib/twitter_friendly/cache.rb