<commit_before># file havily based out off https://github.com/rails/rails/blob/3-2-stable/activesupport/lib/active_support/cache/mem_cache_store.rb
require 'digest/md5'

module ActiveSupport
  module Cache
    # A cache store implementation which stores data in Memcached:
    # http://memcached.org/
    #
    # MemcachedStore uses memcached gem as backend to connect to Memcached server.
    #
    # MemcachedStore implements the Strategy::LocalCache strategy which implements
    # an in-memory cache inside of a block.
    class MemcachedStore < Store
      ESCAPE_KEY_CHARS = /[\<KEY>

      class Codec
        # use dalli compatible flags
        SERIALIZED_FLAG = 0x1
        COMPRESSED_FLAG = 0x2

        # Older versions of this gem would use 0 for the flags whether or not
        # the value was marshal dumped. By setting this flag, we can tell if
        # it were set with an older version for backwards compatible decoding.
        RAW_FLAG = 0x10

        def initialize(serializer: Marshal, compressor: nil)
          @serializer = serializer
          @compressor = compressor
        end

        def encode(_key, value, flags)
          unless value.is_a?(String)
            flags |= SERIALIZED_FLAG
            value = @serializer.dump(value)
          end
          if @compressor
            flags |= COMPRESSED_FLAG
            value = @compressor.compress(value)
          end
          flags |= RAW_FLAG if flags == 0
          [value, flags]
        end

        def decode(_key, value, flags)
          if (flags & COMPRESSED_FLAG) != 0
            value = @compressor.decompress(value)
          end

          if (flags & SERIALIZED_FLAG) != 0
            @serializer.load(value)
          elsif flags == 0 # legacy cache value
            @serializer.load(value) rescue value
          else
            value
          end
        end
      end

      attr_accessor :read_only, :swallow_exceptions

      def initialize(*addresses, **options)
        addresses = addresses.flatten
        options[:codec] ||= Codec.new
        @swallow_exceptions = true
        @swallow_exceptions = options.delete(:swallow_exceptions) if options.key?(:swallow_exceptions)

        super(options)

        if addresses.first.is_a?(Memcached)
          @connection = addresses.first
          raise "Memcached::Rails is no longer supported, "\
                "use a Memcached instance instead" if @connection.is_a?(Memcached::Rails)
        else
          mem_cache_options = options.dup
          servers = mem_cache_options.delete(:servers)
          UNIVERSAL_OPTIONS.each { |name| mem_cache_options.delete(name) }
          @connection = Memcached.new([*addresses, *servers], mem_cache_options)
        end

        extend Strategy::LocalCache
      end

      def append(name, value, options = nil)
        return true if read_only
        options = merged_options(options)<commit_msg>Prepend Strategy::LocalCache<commit_after># file havily based out off https://github.com/rails/rails/blob/3-2-stable/activesupport/lib/active_support/cache/mem_cache_store.rb
require 'digest/md5'

module ActiveSupport
  module Cache
    # A cache store implementation which stores data in Memcached:
    # http://memcached.org/
    #
    # MemcachedStore uses memcached gem as backend to connect to Memcached server.
    #
    # MemcachedStore implements the Strategy::LocalCache strategy which implements
    # an in-memory cache inside of a block.
    class MemcachedStore < Store
      ESCAPE_KEY_CHARS = /[\<KEY>n

      class Codec
        # use dalli compatible flags
        SERIALIZED_FLAG = 0x1
        COMPRESSED_FLAG = 0x2

        # Older versions of this gem would use 0 for the flags whether or not
        # the value was marshal dumped. By setting this flag, we can tell if
        # it were set with an older version for backwards compatible decoding.
        RAW_FLAG = 0x10

        def initialize(serializer: Marshal, compressor: nil)
          @serializer = serializer
          @compressor = compressor
        end

        def encode(_key, value, flags)
          unless value.is_a?(String)
            flags |= SERIALIZED_FLAG
            value = @serializer.dump(value)
          end
          if @compressor
            flags |= COMPRESSED_FLAG
            value = @compressor.compress(value)
          end
          flags |= RAW_FLAG if flags == 0
          [value, flags]
        end

        def decode(_key, value, flags)
          if (flags & COMPRESSED_FLAG) != 0
            value = @compressor.decompress(value)
          end

          if (flags & SERIALIZED_FLAG) != 0
            @serializer.load(value)
          elsif flags == 0 # legacy cache value
            @serializer.load(value) rescue value
          else
            value
          end
        end
      end

      attr_accessor :read_only, :swallow_exceptions

      prepend(Strategy::LocalCache)

      def initialize(*addresses, **options)
        addresses = addresses.flatten
        options[:codec] ||= Codec.new
        @swallow_exceptions = true
        @swallow_exceptions = options.delete(:swallow_exceptions) if options.key?(:swallow_exceptions)

        super(options)

        if addresses.first.is_a?(Memcached)
          @connection = addresses.first
          raise "Memcached::Rails is no longer supported, "\
                "use a Memcached instance instead" if @connection.is_a?(Memcached::Rails)
        else
          mem_cache_options = options.dup
          servers = mem_cache_options.delete(:servers)
          UNIVERSAL_OPTIONS.each { |name| mem_cache_options.delete(name) }
          @connection = Memcached.new([*addresses, *servers], mem_cache_options)
        end
      end

      def append(name, value, options = nil)
        return true if read_only
        options = merged_options(options)
{'fraction_non_alphanumeric': 0.07578875171467764, 'fraction_numerical': 0.0051440329218107, 'mean_word_length': 2.196164383561644, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '663650', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1580, 'n_words': 459}