class AnyCache::Adapters::Redis

@api private @since 0.1.0

Constants

DEAD_TTL

@return [Integer]

@api private @since 0.1.0

DEFAULT_INCR_DECR_AMOUNT

@return [Integer]

@api private @since 0.1.0

DELETE_MATCHED_BATCH_SIZE

@return [Integer]

@api private @since 0.3.0

DELETE_MATCHED_CURSOR_START

@return [String]

@api private @since 0.3.0

NO_EXPIRATION_TTL

@return [NilClass]

@api private @since 0.1.0

Public Class Methods

supported_driver?(driver) click to toggle source

@param driver [Object] @return [Boolean]

@api private @since 0.1.0

# File lib/any_cache/adapters/redis.rb, line 13
def supported_driver?(driver)
  AnyCache::Drivers::Redis.supported_source?(driver)
end

Public Instance Methods

cleanup(**options) click to toggle source

@param options [Hash] @return [void]

@api private @since 0.4.0

# File lib/any_cache/adapters/redis.rb, line 258
def cleanup(**options)
  # NOTE: manual removing is not suppored (redis doing this by itself)
end
clear(**options) click to toggle source

@param options [Hash] @return [void]

@api private @since 0.1.0

# File lib/any_cache/adapters/redis.rb, line 249
def clear(**options)
  flushdb
end
decrement(key, amount = DEFAULT_INCR_DECR_AMOUNT, **options) click to toggle source

@param key [String] @param amount [Integer] @options expires_in [NillClass, Integer] Time in seconds @return [NillClass, Integer]

@api private @since 0.1.0

# File lib/any_cache/adapters/redis.rb, line 210
def decrement(key, amount = DEFAULT_INCR_DECR_AMOUNT, **options)
  expires_in = options.fetch(:expires_in, NO_EXPIRATION_TTL)
  new_amount = nil

  pipelined do
    new_amount = decrby(key, amount)
    expire(key, expires_in: expires_in) if expires_in
  end

  new_amount&.value
end
delete(key, **options) click to toggle source

@param key [String] @param options [Hash] @return [void]

@api private @since 0.1.0

# File lib/any_cache/adapters/redis.rb, line 155
def delete(key, **options)
  del(key)
end
delete_matched(pattern, **options) click to toggle source

@param pattern [String, Regexp] @param options [Hash] @return [void]

@api private @since 0.3.0

# File lib/any_cache/adapters/redis.rb, line 165
def delete_matched(pattern, **options)
  cursor = DELETE_MATCHED_CURSOR_START

  case pattern
  when String
    loop do
      cursor, keys = scan(cursor, match: pattern, count: DELETE_MATCHED_BATCH_SIZE)
      del(keys)
      break if cursor == DELETE_MATCHED_CURSOR_START
    end
  when Regexp
    loop do
      cursor, keys = scan(cursor, count: DELETE_MATCHED_BATCH_SIZE)
      del(keys.grep(pattern))
      break if cursor == DELETE_MATCHED_CURSOR_START
    end
  end
end
exist?(key, **options) click to toggle source

@param key [String] @param options [Hash] @return [Boolean]

@api private @since 0.2.0

# File lib/any_cache/adapters/redis.rb, line 268
def exist?(key, **options)
  exists(key)
end
expire(key, expires_in: DEAD_TTL) click to toggle source

@param key [String] @option expires_in [Integer] @return [void]

@api private @since 0.1.0

# File lib/any_cache/adapters/redis.rb, line 228
def expire(key, expires_in: DEAD_TTL)
  expires_in ||= DEAD_TTL unless expires_in

  driver.expire(key, expires_in)
end
fetch(key, **options) { |key| ... } click to toggle source

@param key [String] @option expires_in [Integer] @option force [Boolean] @return [Object]

@api private @since 0.2.0

# File lib/any_cache/adapters/redis.rb, line 125
def fetch(key, **options, &fallback)
  force_rewrite = options.fetch(:force, false)
  force_rewrite = force_rewrite.call(key) if force_rewrite.respond_to?(:call)

  # NOTE: think about #pipelined
  read(key, **options).tap { |value| return value if value } unless force_rewrite

  yield(key).tap { |value| write(key, value, **options) } if block_given?
end
fetch_multi(*keys, **options, &fallback) click to toggle source

@param keys [Array<string>] @param options [Hash] @param fallback [Proc] @return [Hash]

@api private @since 0.3.0

# File lib/any_cache/adapters/redis.rb, line 142
def fetch_multi(*keys, **options, &fallback)
  # TODO: think about multi-thread approach
  keys.each_with_object({}) do |key, dataset|
    dataset[key] = fetch(key, **options, &fallback)
  end
end
increment(key, amount = DEFAULT_INCR_DECR_AMOUNT, **options) click to toggle source

@param key [String] @param amount [Integer] @option expires_in [NilClass, Integer] Time in seconds @return [NilClass, Integer]

@api private @since 0.1.0

# File lib/any_cache/adapters/redis.rb, line 191
def increment(key, amount = DEFAULT_INCR_DECR_AMOUNT, **options)
  expires_in = options.fetch(:expires_in, NO_EXPIRATION_TTL)
  new_amount = nil

  pipelined do
    new_amount = incrby(key, amount)
    expire(key, expires_in: expires_in) if expires_in
  end

  new_amount&.value
end
persist(key, **options) click to toggle source

@param key [String] @param options [Hash] @return [void]

@api private @since 0.1.0

# File lib/any_cache/adapters/redis.rb, line 240
def persist(key, **options)
  driver.persist(key)
end
read(key, **options) click to toggle source

@param key [String] @option raw [Boolean] @return [Object]

@api private @since 0.1.0

# File lib/any_cache/adapters/redis.rb, line 69
def read(key, **options)
  value = get(key)
  raw = options.fetch(:raw, false)

  raw ? value : detransform_value(value)
end
read_multi(*keys, **options) click to toggle source

@param keys [Array<String>] @option raw [Boolean] @return [Hash]

@api private @since 0.3.0

# File lib/any_cache/adapters/redis.rb, line 82
def read_multi(*keys, **options)
  raw = options.fetch(:raw, false)
  entries = mapped_mget(*keys)

  raw ? entries : detransform_pairset(entries)
end
write(key, value, **options) click to toggle source

@param key [String] @param value [Object] @option raw [Boolean] @option expires_in [NilClass, Integer] Time in seconds @return [void]

@api private @since 0.1.0

# File lib/any_cache/adapters/redis.rb, line 97
def write(key, value, **options)
  raw = options.fetch(:raw, false)
  expires_in = options.fetch(:expires_in, NO_EXPIRATION_TTL)
  value = transform_value(value) unless raw

  expires_in ? setex(key, expires_in, value) : set(key, value)
end
write_multi(entries, **options) click to toggle source

@param entries [Hash] @option raw [Boolean] @return [void]

@api private @since 0.3.0

# File lib/any_cache/adapters/redis.rb, line 111
def write_multi(entries, **options)
  raw = options.fetch(:raw, false)
  entries = transform_pairset(entries) unless raw

  mapped_mset(entries)
end