Sha256: 620d4bad05ebc87c1216e88914673becd02ccec4dac0ef66e3102c24b7438038

Contents?: true

Size: 1.01 KB

Versions: 19

Compression:

Stored size: 1.01 KB

Contents

require 'routemaster/cache_key'

module Routemaster
  module Tasks
    class FixCacheTTL
      def initialize(cache: Config.cache_redis, batch_size: 100)
        @cache = cache
        @batch_size = batch_size
      end

      def call
        pattern = "#{@cache.namespace}:#{CacheKey::PREFIX}*"
        _each_key_batch(pattern) do |node, keys|
          _fix_keys(node, keys)
        end
      end

      private

      def _each_key_batch(pattern)
        @cache.redis.nodes.each do |node|
          cursor = 0
          loop do
            cursor, keys = node.scan(cursor, count: @batch_size, match: pattern)
            yield node, keys
            break if cursor.to_i == 0
          end
        end
      end

      def _fix_keys(node, keys)
        ttls = node.pipelined do |p|
          keys.each { |k| p.ttl(k) }
        end

        node.pipelined do |p|
          keys.zip(ttls).each do |k,ttl|
            next unless ttl < 0
            p.expire(k, Config.cache_expiry)
          end
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
routemaster-drain-3.7.1 lib/routemaster/tasks/fix_cache_ttl.rb
routemaster-drain-3.7.0 lib/routemaster/tasks/fix_cache_ttl.rb
routemaster-drain-3.6.8 lib/routemaster/tasks/fix_cache_ttl.rb
routemaster-drain-3.6.7 lib/routemaster/tasks/fix_cache_ttl.rb
routemaster-drain-3.6.6 lib/routemaster/tasks/fix_cache_ttl.rb
routemaster-drain-3.6.5 lib/routemaster/tasks/fix_cache_ttl.rb
routemaster-drain-3.6.4 lib/routemaster/tasks/fix_cache_ttl.rb
routemaster-drain-3.6.3 lib/routemaster/tasks/fix_cache_ttl.rb
routemaster-drain-3.6.2 lib/routemaster/tasks/fix_cache_ttl.rb
routemaster-drain-3.6.1 lib/routemaster/tasks/fix_cache_ttl.rb
routemaster-drain-3.6.0 lib/routemaster/tasks/fix_cache_ttl.rb
routemaster-drain-3.5.1 lib/routemaster/tasks/fix_cache_ttl.rb
routemaster-drain-3.5.0 lib/routemaster/tasks/fix_cache_ttl.rb
routemaster-drain-3.4.0 lib/routemaster/tasks/fix_cache_ttl.rb
routemaster-drain-3.3.0 lib/routemaster/tasks/fix_cache_ttl.rb
routemaster-drain-3.2.0 lib/routemaster/tasks/fix_cache_ttl.rb
routemaster-drain-3.1.0 lib/routemaster/tasks/fix_cache_ttl.rb
routemaster-drain-3.0.3 lib/routemaster/tasks/fix_cache_ttl.rb
routemaster-drain-3.0.2 lib/routemaster/tasks/fix_cache_ttl.rb