Sha256: 3ced9ba8c139a79f62a3ad722d279dd1d7c5fa9f7eb7aad9168e979027b0c4d3

Contents?: true

Size: 1008 Bytes

Versions: 2

Compression:

Stored size: 1008 Bytes

Contents

module Pause
  module Redis
    class OperationNotSupported < StandardError
    end

    # This class encapsulates Redis operations used by Pause.
    # Operations that are not possible when data is sharded
    # raise an error.
    class ShardedAdapter < Adapter
      def increment(scope, identifier, timestamp, count = 1)
        k = tracked_key(scope, identifier)
        redis.zincrby k, count, period_marker(resolution, timestamp)
        redis.expire k, history

        if redis.zcard(k) > time_blocks_to_keep
          list = extract_set_elements(k)
          to_remove = list.slice(0, (list.size - time_blocks_to_keep))
          redis.zrem(k, to_remove.map(&:ts))
        end
      end


      private

      def redis
        @redis_conn ||= ::Redis.new(host: Pause.config.redis_host,
          port: Pause.config.redis_port)
      end

      def keys(_key_scope)
        raise OperationNotSupported.new('Can not be executed when Pause is configured in sharded mode')
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pause-0.2.1 lib/pause/redis/sharded_adapter.rb
pause-0.2.0 lib/pause/redis/sharded_adapter.rb