Sha256: 6dfe77177708326dd61c5ccc4ab7b22600eea8068de94ad232eae379c4f55891

Contents?: true

Size: 1.11 KB

Versions: 20

Compression:

Stored size: 1.11 KB

Contents

module Typhoeus
  module Cache
    # This module provides a simple way to cache HTTP responses in Redis.
    class Redis
      # @example Set Redis as the Typhoeus cache backend
      #   Typhoeus::Config.cache = Typhoeus::Cache::Redis.new
      #
      # @param [ Redis ] redis
      #   A connection to Redis. Defaults to `Redis.new`, which uses the
      #   `REDIS_URL` environment variable to connect
      # @param [ Integer ] default_ttl
      #   The default TTL of cached responses in seconds, for requests which do not set a cache_ttl.
      def initialize(redis = ::Redis.new, options = {})
        @redis = redis
        @default_ttl = options[:default_ttl]
      end

      def get(request)
        serialized_response = @redis.get(request.cache_key)
        return unless serialized_response
        Marshal.load(serialized_response)
      end

      def set(request, response)
        ttl = request.cache_ttl || @default_ttl
        key = request.cache_key
        serialized_response = Marshal.dump(response)
        @redis.set(key, serialized_response)
        @redis.expire(key, ttl) if ttl
      end
    end
  end
end

Version data entries

20 entries across 19 versions & 6 rubygems

Version Path
talon_one-2.0.0 vendor/bundle/ruby/2.3.0/gems/typhoeus-1.3.1/lib/typhoeus/cache/redis.rb
talon_one-2.0.0 vendor/bundle/ruby/2.7.0/gems/typhoeus-1.3.1/lib/typhoeus/cache/redis.rb
cloudsmith-api-0.49.118 vendor/bundle/ruby/2.6.0/gems/typhoeus-1.3.1/lib/typhoeus/cache/redis.rb
cloudsmith-api-0.49.98 vendor/bundle/ruby/2.6.0/gems/typhoeus-1.3.1/lib/typhoeus/cache/redis.rb
cloudsmith-api-0.49.94 vendor/bundle/ruby/2.6.0/gems/typhoeus-1.3.1/lib/typhoeus/cache/redis.rb
cloudsmith-api-0.49.21 vendor/bundle/ruby/2.6.0/gems/typhoeus-1.3.1/lib/typhoeus/cache/redis.rb
cloudsmith-api-0.49.15 vendor/bundle/ruby/2.6.0/gems/typhoeus-1.3.1/lib/typhoeus/cache/redis.rb
cloudsmith-api-0.49.13 vendor/bundle/ruby/2.6.0/gems/typhoeus-1.3.1/lib/typhoeus/cache/redis.rb
cloudsmith-api-0.49.9 vendor/bundle/ruby/2.3.0/gems/typhoeus-1.3.1/lib/typhoeus/cache/redis.rb
cloudsmith-api-0.44.4 vendor/bundle/ruby/2.3.0/gems/typhoeus-1.3.1/lib/typhoeus/cache/redis.rb
dadapush_client-1.0.1 vendor/bundle/ruby/2.3.0/gems/typhoeus-1.3.0/lib/typhoeus/cache/redis.rb
approveapi-1.0.8 vendor/bundle/ruby/2.6.0/gems/typhoeus-1.3.1/lib/typhoeus/cache/redis.rb
approveapi-1.0.5 vendor/bundle/ruby/2.6.0/gems/typhoeus-1.3.1/lib/typhoeus/cache/redis.rb
typhoeus-1.3.1 lib/typhoeus/cache/redis.rb
cloudsmith-api-0.30.7 vendor/bundle/ruby/2.3.0/gems/typhoeus-1.3.0/lib/typhoeus/cache/redis.rb
color_me_shop-1.0.0 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.3.0/lib/typhoeus/cache/redis.rb
cloudsmith-api-0.21.4 vendor/bundle/ruby/2.3.0/gems/typhoeus-1.3.0/lib/typhoeus/cache/redis.rb
typhoeus-1.3.0 lib/typhoeus/cache/redis.rb
typhoeus-1.1.2 lib/typhoeus/cache/redis.rb
typhoeus-1.1.1 lib/typhoeus/cache/redis.rb