<commit_before># frozen_string_literal: true
module CacheConnection
  extend self

  # This patches AR::MemcacheStore to notify AS::Notifications upon read_multis like the rest of rails does
  module MemcachedStoreInstrumentation
    def read_multi(*args, &block)
      instrument('read_multi', 'MULTI', keys: args) do
        super(*args, &block)
      end
    end
  end

  def host
    ENV['MEMCACHED_HOST'] || "127.0.0.1"
  end

  def backend
    @backend ||= case ENV['ADAPTER']
    when nil, 'dalli'
      require 'active_support/cache/mem_cache_store'
      ActiveSupport::Cache::MemCacheStore.new("#{host}:11211", failover: false)
    when 'memcached'
      require 'memcached_store'
      require 'active_support/cache/memcached_store'
      ActiveSupport::Cache::MemcachedStore.prepend(MemcachedStoreInstrumentation)
      ActiveSupport::Cache::MemcachedStore.new("#{host}:11211", support_cas: true, auto_eject_hosts: false)
    else
      raise "Unknown adapter: #{ENV['ADAPTER']}"
    end
  end

  def setup
    IdentityCache.cache_backend = backend
  end
end<commit_msg>Set up MemCacheStore in a test suite with TTL from docs<commit_after># frozen_string_literal: true
module CacheConnection
  extend self

  # This patches AR::MemcacheStore to notify AS::Notifications upon read_multis like the rest of rails does
  module MemcachedStoreInstrumentation
    def read_multi(*args, &block)
      instrument('read_multi', 'MULTI', keys: args) do
        super(*args, &block)
      end
    end
  end

  def host
    ENV['MEMCACHED_HOST'] || "127.0.0.1"
  end

  def backend
    @backend ||= case ENV['ADAPTER']
    when nil, 'dalli'
      require 'active_support/cache/mem_cache_store'
      ActiveSupport::Cache::MemCacheStore.new("#{host}:11211", failover: false, expires_in: 6.hours.to_i)
    when 'memcached'
      require 'memcached_store'
      require 'active_support/cache/memcached_store'
      ActiveSupport::Cache::MemcachedStore.prepend(MemcachedStoreInstrumentation)
      ActiveSupport::Cache::MemcachedStore.new("#{host}:11211", support_cas: true, auto_eject_hosts: false)
    else
      raise "Unknown adapter: #{ENV['ADAPTER']}"
    end
  end

  def setup
    IdentityCache.cache_backend = backend
  end
end
{'fraction_non_alphanumeric': 0.1039426523297491, 'fraction_numerical': 0.01478494623655914, 'mean_word_length': 4.0179775280898875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5374267', 'n_tokens_mistral': 720, 'n_tokens_neox': 685, 'n_words': 169}