class AnyCache::Adapters::ActiveSupportNaiveStore

@api private @since 0.1.0

Attributes

decr_operation[R]

@return [Operation::Decrement]

@api private @since 0.1.0

expr_operation[R]

@return [Operation::Expire]

@api private @since 0.1.0

incr_operation[R]

@return [Operation::Increment]

@api private @since 0.1.0

lock[R]

@return [Concurrent::ReentrantReadWriteLock]

@api private @since 0.1.0

pers_operation[R]

@return [Operation::Persist]

@api private @since 0.1.0

Public Class Methods

new(driver) click to toggle source

@param driver [Object] @return [void]

@api private @since 0.1.0

Calls superclass method
# File lib/any_cache/adapters/active_support_naive_store.rb, line 18
def initialize(driver)
  super
  @lock = Concurrent::ReentrantReadWriteLock.new
  @incr_operation = self.class::Increment.new(driver)
  @decr_operation = self.class::Decrement.new(driver)
  @expr_operation = self.class::Expire.new(driver)
  @pers_operation = self.class::Persist.new(driver)
end

Public Instance Methods

cleanup(**options) click to toggle source

@param options [Hash] @return [void]

@api private @since 0.4.0

Calls superclass method
# File lib/any_cache/adapters/active_support_naive_store.rb, line 85
def cleanup(**options)
  lock.with_write_lock { super }
end
clear(**options) click to toggle source

@param options [Hash] @return [void]

@api private @since 0.1.0

Calls superclass method
# File lib/any_cache/adapters/active_support_naive_store.rb, line 76
def clear(**options)
  lock.with_write_lock { super }
end
decrement(key, amount = self.class::Decrement::DEFAULT_AMOUNT, **options) click to toggle source

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

@api private @since 0.1.0

# File lib/any_cache/adapters/active_support_naive_store.rb, line 181
def decrement(key, amount = self.class::Decrement::DEFAULT_AMOUNT, **options)
  lock.with_write_lock do
    expires_in = options.fetch(:expires_in, self.class::Operation::NO_EXPIRATION_TTL)

    decr_operation.call(key, amount, expires_in: expires_in)
  end
end
delete(key, **options) click to toggle source

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

@api private @since 0.1.0

Calls superclass method
# File lib/any_cache/adapters/active_support_naive_store.rb, line 57
def delete(key, **options)
  lock.with_write_lock { super }
end
delete_matched(pattern, **options) click to toggle source

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

@api private @since 0.3.0

Calls superclass method
# File lib/any_cache/adapters/active_support_naive_store.rb, line 67
def delete_matched(pattern, **options)
  lock.with_write_lock { super }
end
exist?(key, **options) click to toggle source

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

@api private @since 0.2.0

Calls superclass method
# File lib/any_cache/adapters/active_support_naive_store.rb, line 215
def exist?(key, **options)
  lock.with_read_lock { super }
end
expire(key, expires_in: self.class::Operation::DEAD_TTL) click to toggle source

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

@api private @since 0.1.0

# File lib/any_cache/adapters/active_support_naive_store.rb, line 195
def expire(key, expires_in: self.class::Operation::DEAD_TTL)
  lock.with_write_lock { expr_operation.call(key, expires_in: expires_in) }
end
fetch(key, **options, &fallback) click to toggle source

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

@api private @since 0.2.0

Calls superclass method
# File lib/any_cache/adapters/active_support_naive_store.rb, line 126
def fetch(key, **options, &fallback)
  lock.with_write_lock do
    force_rewrite = options.fetch(:force, false)
    force_rewrite = force_rewrite.call(key) if force_rewrite.respond_to?(:call)
    expires_in    = options.fetch(:expires_in, self.class::Operation::NO_EXPIRATION_TTL)

    super(key, force: force_rewrite, expires_in: expires_in, &fallback)
  end
end
fetch_multi(*keys, **options, &fallback) click to toggle source

@param keys [Array<String>] @param fallback [Proc] @option force [Boolean, Proc] @option expires_in [Integer] @return [Hash]

@api private @since 0.3.0

# File lib/any_cache/adapters/active_support_naive_store.rb, line 144
def fetch_multi(*keys, **options, &fallback)
  lock.with_write_lock do
    force_rewrite = options.fetch(:force, false)
    expires_in    = options.fetch(:expires_in, self.class::Operation::NO_EXPIRATION_TTL)

    # NOTE:
    #   use own #fetch_multi implementation cuz original #fetch_multi
    #   does not support :force option
    keys.each_with_object({}) do |key, dataset|
      force = force_rewrite.respond_to?(:call) ? force_rewrite.call(key) : force_rewrite
      dataset[key] = driver.fetch(key, force: force, expires_in: expires_in, &fallback)
    end
  end
end
increment(key, amount = self.class::Increment::DEFAULT_AMOUNT, **options) click to toggle source

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

@api private @since 0.1.0

# File lib/any_cache/adapters/active_support_naive_store.rb, line 166
def increment(key, amount = self.class::Increment::DEFAULT_AMOUNT, **options)
  lock.with_write_lock do
    expires_in = options.fetch(:expires_in, self.class::Operation::NO_EXPIRATION_TTL)

    incr_operation.call(key, amount, expires_in: expires_in)
  end
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/active_support_naive_store.rb, line 205
def persist(key, **options)
  lock.with_write_lock { pers_operation.call(key) }
end
read(key, **options) click to toggle source

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

@api private @since 0.1.0

Calls superclass method
# File lib/any_cache/adapters/active_support_naive_store.rb, line 33
def read(key, **options)
  lock.with_read_lock { super }
end
read_multi(*keys, **options) click to toggle source

@param keys [Array<String>] @param options [Hash] @return [Hash]

@api private @since 0.3.0

Calls superclass method
# File lib/any_cache/adapters/active_support_naive_store.rb, line 43
def read_multi(*keys, **options)
  lock.with_read_lock do
    super.tap do |res|
      res.merge!(Hash[(keys - res.keys).zip(Operation::READ_MULTI_EMPTY_KEYS_SET)])
    end
  end
end
write(key, value, **options) click to toggle source

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

@api private @since 0.1.0

Calls superclass method
# File lib/any_cache/adapters/active_support_naive_store.rb, line 96
def write(key, value, **options)
  lock.with_write_lock do
    expires_in = options.fetch(:expires_in, self.class::Operation::NO_EXPIRATION_TTL)

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

@param entries [Hash] @param options [Hash] @return [void]

@api private @since 0.3.0

Calls superclass method
# File lib/any_cache/adapters/active_support_naive_store.rb, line 110
def write_multi(entries, **options)
  lock.with_write_lock do
    expires_in = self.class::Operation::NO_EXPIRATION_TTL

    super(entries, expires_in: expires_in)
  end
end