<commit_before>require 'adapter'
require 'memcached'

module Adapter
  module Memcached
    def read(key)
      decode(client.get(key_for(key)))
    rescue ::Memcached::NotFound
    end

    def write(key, value)
      client.set(key_for(key), encode(value))
    end

    def delete(key)
      read(key).tap { client.delete(key_for(key)) }
    rescue ::Memcached::NotFound
    end

    def clear
      client.flush
    end

    def lock(name, options={}, &block)
      key           = name.to_s
      start         = Time.now
      lock_acquired = false
      expiration    = options.fetch(:expiration, 1)
      timeout       = options.fetch(:timeout, 5)

      while (Time.now - start) < timeout
        begin
          client.add(key, 'locked', expiration)
          lock_acquired = true
          break
        rescue ::Memcached::NotStored
          sleep 0.1
        end
      end

      raise(Adapter::LockTimeout.new(name, timeout)) unless lock_acquired

      begin
        yield
      ensure
        delete(key)
      end
    end

    def key_for(key)
      [super].pack("m").strip
    end
  end
end

Adapter.define(:memcached, Adapter::Memcached)<commit_msg>Removed need for key packing.<commit_after>require 'adapter'
require 'memcached'

module Adapter
  module Memcached
    def read(key)
      decode(client.get(key_for(key)))
    rescue ::Memcached::NotFound
    end

    def write(key, value)
      client.set(key_for(key), encode(value))
    end

    def delete(key)
      read(key).tap { client.delete(key_for(key)) }
    rescue ::Memcached::NotFound
    end

    def clear
      client.flush
    end

    def lock(name, options={}, &block)
      key           = key_for(name)
      start         = Time.now
      lock_acquired = false
      expiration    = options.fetch(:expiration, 1)
      timeout       = options.fetch(:timeout, 5)

      while (Time.now - start) < timeout
        begin
          client.add(key, 'locked', expiration)
          lock_acquired = true
          break
        rescue ::Memcached::NotStored
          sleep 0.1
        end
      end

      raise(Adapter::LockTimeout.new(name, timeout)) unless lock_acquired

      begin
        yield
      ensure
        begin
          client.delete(key)
        rescue ::Memcached::NotFound
        end
      end
    end
  end
end

Adapter.define(:memcached, Adapter::Memcached)
{'fraction_non_alphanumeric': 0.09839527027027027, 'fraction_numerical': 0.0033783783783783786, 'mean_word_length': 2.048906048906049, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2453194', 'n_tokens_mistral': 728, 'n_tokens_neox': 702, 'n_words': 167}