Sha256: 934e749387175b29ac57a34bf812ceea93c1b2532eec092046170862b4d3f58d

Contents?: true

Size: 1.17 KB

Versions: 6772

Compression:

Stored size: 1.17 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 [ Hash ] options
      #   Options
      # @option options [ 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

6,772 entries across 6,770 versions & 23 rubygems

Version Path
ory-client-0.0.1.alpha50 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-client-0.0.1.alpha49 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-client-0.0.1.alpha48 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-client-0.0.1.alpha47 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-client-0.0.1.alpha46 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-client-0.0.1.alpha45 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-client-0.0.1.alpha44 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-client-0.0.1.alpha43 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-client-0.0.1.alpha42 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-client-0.0.1.alpha41 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-client-0.0.1.alpha40 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-client-0.0.1.alpha39 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-client-0.0.1.alpha38 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-client-0.0.1.alpha37 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-client-0.0.1.alpha36 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-kratos-client-0.8.2.alpha1 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-client-0.0.1.alpha31 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-client-0.0.1.alpha30 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-client-0.0.1.alpha29 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb
ory-client-0.0.1.alpha28 vendor/bundle/ruby/2.5.0/gems/typhoeus-1.4.0/lib/typhoeus/cache/redis.rb