<commit_before>
require 'resourceful'
require 'redis'
require 'json'
require 'digest/md5'
require 'hitimes'

module Resourceful
  class RedisCacheManager

    attr_reader :db

    def initialize(options)
      @logger = options[:logger]
      @db = Redis.new(options)
    end
    
    def lookup(request)
      response = nil
      total_timer = Hitimes::TimedMetric.now("Total cache lookup")
      fetch_timer = Hitimes::TimedMetric.new("Fetching from redis")

      if metadata = fetch_timer.measure { db.list_range(request.uri.to_s, 0, -1) }
        metadata.detect { |meta|
          json = Marshal.load(meta)
          if valid_for?(request, json[:vary_header_values])
            key = json[:key]
            text = fetch_timer.measure { db.get(key) }
            response = Marshal.load(text)
            total_timer.stop
            @logger.debug("    [Redis] cache read: %0.4fs (%0.1f%%) fetching, %0.4f total" %
                          [fetch_timer.sum, fetch_timer.sum / total_timer.sum * 100, total_timer.sum ])
            true
          else
            @logger.debug("[Redis] cache miss")
          end

        }
      end


      response
    end

    def store(request, response)
      @logger.info("Storing in cache")
      key = Digest::MD5.hexdigest(request.uri.to_s)

      response.header['Cache-Control'].match(/max-age=(\d+)/) if response.header['Cache-Control']
      expires = $1
      db.set(key, Marshal.dump(response), expires)

      values = vary_header_values(request, response)<commit_msg>Use a uuid for unique key for cached responses<commit_after>
require 'resourceful'
require 'redis'
require 'json'
require 'digest/md5'
require 'hitimes'
require 'uuidtools'

module Resourceful
  class RedisCacheManager

    attr_reader :db

    def initialize(options)
      @logger = options[:logger]
      @db = Redis.new(options)
    end
    
    def lookup(request)
      response = nil
      total_timer = Hitimes::TimedMetric.now("Total cache lookup")
      fetch_timer = Hitimes::TimedMetric.new("Fetching from redis")

      if metadata = fetch_timer.measure { db.list_range(request.uri.to_s, 0, -1) }
        metadata.detect { |meta|
          json = Marshal.load(meta)
          if valid_for?(request, json[:vary_header_values])
            key = json[:key]
            text = fetch_timer.measure { db.get(key) }
            response = Marshal.load(text)
            total_timer.stop
            @logger.debug("    [Redis] cache read: %0.4fs (%0.1f%%) fetching, %0.4f total" %
                          [fetch_timer.sum, fetch_timer.sum / total_timer.sum * 100, total_timer.sum ])
            true
          else
            @logger.debug("[Redis] cache miss")
          end

        }
      end


      response
    end

    def store(request, response)
      @logger.info("Storing in cache")
      key = UUIDTools::UUID.random_create

      response.header['Cache-Control'].match(/max-age=(\d+)/) if response.header['Cache-Control']
      expires = $1
      db.set(key, Marshal.dump(response), expires)

      values = vary_header_values(request, response)
{'fraction_non_alphanumeric': 0.11768509537665697, 'fraction_numerical': 0.008729388942774006, 'mean_word_length': 2.366702937976061, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5816310', 'n_tokens_mistral': 968, 'n_tokens_neox': 939, 'n_words': 207}