File: rails.rb

package info (click to toggle)
ruby-typhoeus 1.4.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: ruby: 4,381; makefile: 6
file content (28 lines) | stat: -rw-r--r-- 951 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module Typhoeus
  module Cache
    # This module provides a simple way to cache HTTP responses in using the Rails cache.
    class Rails
      # @example Use the Rails cache setup to cache Typhoeus responses.
      #   Typhoeus::Config.cache = Typhoeus::Cache::Rails.new
      #
      # @param [ ActiveSupport::Cache::Store ] cache
      #   A Rails cache backend. Defaults to Rails.cache.
      # @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(cache = ::Rails.cache, options = {})
        @cache = cache
        @default_ttl = options[:default_ttl]
      end

      def get(request)
        @cache.read(request)
      end

      def set(request, response)
        @cache.write(request.cache_key, response, :expires_in => request.cache_ttl || @default_ttl)
      end
    end
  end
end