<commit_before>require "redis-store"

module ::RedisStore
  module Cache
    module Rails2
      # Instantiate the store.
      #
      # Example:
      #   RedisStore.new
      #     # => host: localhost,   port: 6379,  db: 0
      #
      #   RedisStore.new "example.com"
      #     # => host: example.com, port: 6379,  db: 0
      #
      #   RedisStore.new "example.com:23682"
      #     # => host: example.com, port: 23682, db: 0
      #
      #   RedisStore.new "example.com:23682/1"
      #     # => host: example.com, port: 23682, db: 1
      #
      #   RedisStore.new "example.com:23682/1/theplaylist"
      #     # => host: example.com, port: 23682, db: 1, namespace: theplaylist
      #
      #   RedisStore.new "localhost:6379/0", "localhost:6380/0"
      #     # => instantiate a cluster
      def initialize(*addresses)
        @data = ::Redis::Factory.create(addresses)
      end

      def write(key, value, options = nil)
        super
        method = options && options[:unless_exist] ? :setnx : :set
        @data.send method, key, value, options
      end

      def read(key, options = nil)
        super
        @data.get key, options
      end

      def delete(key, options = nil)
        super
        @data.del key
      end

      def exist?(key, options = nil)
        super
        @data.exists key
      end

      # Delete objects for matched keys.
      #
      # Example:
      #   cache.del_matched "rab*"
      def delete_matched(matcher, options = nil)
        instrument(:delete_matched, matcher, options) do
          !(keys = @data.keys(matcher)).empty? && @data.del(*keys)
        end
      end

      private
        def instrument(operation, key, options = nil)
          log(operation.to_s, key, options)
          yield
        end
    end

    module Rails3
      # Instantiate the store.
      #
      # Example:
      #   RedisStore.new
      #     # => host: localhost,   port: 6379,  db: 0
      #
      #   RedisStore.new "example.com"
      #     # => host: example.com, port: 6379,  db: 0
      #
      #   RedisStore.new "example.com:23682"
      #     # => host: example.com, port: 23682, db: 0
      #
      #   RedisStore.new "example.com:23682/1"
      #     # => host: example.com, port: 23682, db: 1
      #
      #   RedisStore.new "example.com:23682/1/theplaylist"
      #     # => host: example.com, port: 23682, db: 1, namespace: theplaylist
      #
      #   RedisStore.new "localhost:6379/0", "localhost:6380/0"
      #     # => instantiate a cluster
      def initialize(*addresses)
        @data = ::Redis::Factory.create(addresses)
        super(addresses.extract_options!)
      end

      # Delete objects for matched keys.
      #
      # Example:
      #   cache.del_matched "rab*"
      def delete_matched(matcher, options = nil)
        options = merged_options(options)
        instrument(:delete_matched, matcher.inspect) do
          matcher = key_matcher(matcher, options)
          begin
            !(keys = @data.keys(matcher)).empty? && @data.del(*keys)
          rescue Errno::ECONNREFUSED => e
            false
          end
        end
      end

      protected
        def write_entry(key, entry, options)
          method = options && options[:unless_exist] ? :setnx : :set
          @data.send method, key, entry, options
        rescue Errno::ECONNREFUSED => e
          false
        end

        def read_entry(key, options)
          entry = @data.get key, options
          if entry
            entry.is_a?(ActiveSupport::Cache::Entry) ? entry : ActiveSupport::Cache::Entry.new(entry)
          end
        rescue Errno::ECONNREFUSED => e
          nil
        end

        # Add the namespace defined in the options to a pattern designed to match keys.
        #
        # This implementation is __different__ than ActiveSupport:
        # __it doesn't accept Regular expressions__, because the Redis matcher is designed<commit_msg>Re-implement the delete_entry because it s needed by the ActiveSupport::Cache implementation in Rails 3<commit_after>require "redis-store"

module ::RedisStore
  module Cache
    module Rails2
      # Instantiate the store.
      #
      # Example:
      #   RedisStore.new
      #     # => host: localhost,   port: 6379,  db: 0
      #
      #   RedisStore.new "example.com"
      #     # => host: example.com, port: 6379,  db: 0
      #
      #   RedisStore.new "example.com:23682"
      #     # => host: example.com, port: 23682, db: 0
      #
      #   RedisStore.new "example.com:23682/1"
      #     # => host: example.com, port: 23682, db: 1
      #
      #   RedisStore.new "example.com:23682/1/theplaylist"
      #     # => host: example.com, port: 23682, db: 1, namespace: theplaylist
      #
      #   RedisStore.new "localhost:6379/0", "localhost:6380/0"
      #     # => instantiate a cluster
      def initialize(*addresses)
        @data = ::Redis::Factory.create(addresses)
      end

      def write(key, value, options = nil)
        super
        method = options && options[:unless_exist] ? :setnx : :set
        @data.send method, key, value, options
      end

      def read(key, options = nil)
        super
        @data.get key, options
      end

      def delete(key, options = nil)
        super
        @data.del key
      end

      def exist?(key, options = nil)
        super
        @data.exists key
      end

      # Delete objects for matched keys.
      #
      # Example:
      #   cache.del_matched "rab*"
      def delete_matched(matcher, options = nil)
        instrument(:delete_matched, matcher, options) do
          !(keys = @data.keys(matcher)).empty? && @data.del(*keys)
        end
      end

      private
        def instrument(operation, key, options = nil)
          log(operation.to_s, key, options)
          yield
        end
    end

    module Rails3
      # Instantiate the store.
      #
      # Example:
      #   RedisStore.new
      #     # => host: localhost,   port: 6379,  db: 0
      #
      #   RedisStore.new "example.com"
      #     # => host: example.com, port: 6379,  db: 0
      #
      #   RedisStore.new "example.com:23682"
      #     # => host: example.com, port: 23682, db: 0
      #
      #   RedisStore.new "example.com:23682/1"
      #     # => host: example.com, port: 23682, db: 1
      #
      #   RedisStore.new "example.com:23682/1/theplaylist"
      #     # => host: example.com, port: 23682, db: 1, namespace: theplaylist
      #
      #   RedisStore.new "localhost:6379/0", "localhost:6380/0"
      #     # => instantiate a cluster
      def initialize(*addresses)
        @data = ::Redis::Factory.create(addresses)
        super(addresses.extract_options!)
      end

      # Delete objects for matched keys.
      #
      # Example:
      #   cache.del_matched "rab*"
      def delete_matched(matcher, options = nil)
        options = merged_options(options)
        instrument(:delete_matched, matcher.inspect) do
          matcher = key_matcher(matcher, options)
          begin
            !(keys = @data.keys(matcher)).empty? && @data.del(*keys)
          rescue Errno::ECONNREFUSED => e
            false
          end
        end
      end

      protected
        def write_entry(key, entry, options)
          method = options && options[:unless_exist] ? :setnx : :set
          @data.send method, key, entry, options
        rescue Errno::ECONNREFUSED => e
          false
        end

        def read_entry(key, options)
          entry = @data.get key, options
          if entry
            entry.is_a?(ActiveSupport::Cache::Entry) ? entry : ActiveSupport::Cache::Entry.new(entry)
          end
        rescue Errno::ECONNREFUSED => e
          nil
        end

        ##
        # Implement the ActiveSupport::Cache#delete_entry
        #
        # It's really needed and use
        #
        def delete_entry(key, options)
          @data.del key
        rescue Errno::ECONNREFUSED => e
          false
        end


        # Add the namespace defined in the options to a pattern designed to match keys.
        #
        # This implementation is __different__ than ActiveSupport:
        # __it doesn't accept Regular expressions__, because the Redis matcher is designed
{'fraction_non_alphanumeric': 0.10885182028491416, 'fraction_numerical': 0.02739559235358578, 'mean_word_length': 1.8441828254847645, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 34, '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': '917781', 'n_tokens_mistral': 2668, 'n_tokens_neox': 2489, 'n_words': 626}