class AnyCache::Adapters::ActiveSupportMemCacheStore

@api private @since 0.2.0

Constants

DEAD_TTL

@return [Integer]

@api private @since 0.2.0

DEFAULT_INCR_DECR_AMOUNT

@return [Integer]

@api private @since 0.2.0

INITIAL_DECREMNETED_VALUE

@return [Integer]

@api private @since 0.2.0

NO_EXPIRATION_TTL

@return [NilClass]

@api private @since 0.2.0

READ_MULTI_EMPTY_KEYS_SET

@return [Array]

@api private @since 0.3.0

Public Class Methods

supported_driver?(driver) click to toggle source

@param driver [Object] @retunr [Boolean]

@api private @since 0.2.0

# File lib/any_cache/adapters/active_support_mem_cache_store.rb, line 15
def supported_driver?(driver)
  AnyCache::Drivers::ActiveSupportMemCacheStore.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/active_support_mem_cache_store.rb, line 227
def cleanup(**options)
  # NOTE: manual removing is not supported (memcached doing this by itself)
end
decrement(key, amount = DEFAULT_INCR_DECR_AMOUNT, **options) click to toggle source

@param key [String] @param amount [Integer] @option expires_in [Integer] @return [Integer, Float]

@api private @since 0.2.0

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

  unless exist?(key)
    # NOTE: Dalli::Client can't decrement:
    #   - non-raw values;
    #   - values lower than zero;
    #   - empty entries;
    write(key, INITIAL_DECREMNETED_VALUE, expires_in: expires_in, raw: true)
    INITIAL_DECREMNETED_VALUE
  else
    driver.decrement(key, amount).tap do
      expire(key, expires_in: expires_in) if expires_in
    end
  end
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/active_support_mem_cache_store.rb, line 142
def delete_matched(pattern, **options)
  # TODO: make it real >:]
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/active_support_mem_cache_store.rb, line 218
def exist?(key, **options)
  driver.exist?(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.2.0

# File lib/any_cache/adapters/active_support_mem_cache_store.rb, line 195
def expire(key, expires_in: DEAD_TTL)
  read(key, raw: true).tap do |value|
    is_alive = expires_in ? expires_in.positive? : false
    is_alive ? write(key, value, expires_in: expires_in, raw: true) : delete(key)
  end
end
fetch(key, **options, &fallback) click to toggle source

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

@api private @since 0.2.0

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

  driver.fetch(key, force: force_rewrite, expires_in: expires_in, raw: raw, &fallback)
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/active_support_mem_cache_store.rb, line 130
def fetch_multi(*keys, **options, &fallback)
  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] @options expires_in [Integer] @return [Integer, Float]

@api private @since 0.2.0

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

  unless exist?(key)
    write(key, amount, expires_in: expires_in, raw: true) && amount
  else
    driver.increment(key, amount).tap do
      expire(key, expires_in: expires_in) if expires_in
    end
  end
end
persist(key, **options) click to toggle source

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

@api private @sicne 0.2.0

# File lib/any_cache/adapters/active_support_mem_cache_store.rb, line 208
def persist(key, **options)
  read(key).tap { |value| write(key, value) }
end
read(key, **options) click to toggle source

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

@api private @since 0.2.0

# File lib/any_cache/adapters/active_support_mem_cache_store.rb, line 59
def read(key, **options)
  raw = options.fetch(:raw, false)

  driver.read(key, raw: raw)
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/active_support_mem_cache_store.rb, line 71
def read_multi(*keys, **options)
  raw = options.fetch(:raw, false)

  driver.read_multi(*keys, raw: raw).tap do |entries|
    entries.merge!(Hash[(keys - entries.keys).zip(READ_MULTI_EMPTY_KEYS_SET)])
  end
end
write(key, value, **options) click to toggle source

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

@api private @sicne 0.2.0

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

  driver.write(key, value, expires_in: expires_in, raw: raw)
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/active_support_mem_cache_store.rb, line 100
def write_multi(entries, **options)
  raw = options.fetch(:raw, false)

  driver.write_multi(entries, expires_in: NO_EXPIRATION_TTL, raw: raw)
end