Class: AnyCache::Adapters::ActiveSupportRedisCacheStore Private

Inherits:
Basic
  • Object
show all
Defined in:
lib/any_cache/adapters/active_support_redis_cache_store.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

Constant Summary collapse

READ_MULTI_EMPTY_KEYS_SET =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (Array)

Since:

  • 0.3.0

[].freeze
NO_EXPIRATION_TTL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (NilClass)

Since:

  • 0.1.0

nil
DEAD_TTL =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (Integer)

Since:

  • 0.1.0

0
DEFAULT_INCR_DECR_AMOUNT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Returns:

  • (Integer)

Since:

  • 0.1.0

1

Instance Attribute Summary

Attributes inherited from Basic

#driver

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Basic

#clear, #delete, #delete_matched, #initialize

Constructor Details

This class inherits a constructor from AnyCache::Adapters::Basic

Class Method Details

.supported_driver?(driver) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • driver (Object)

Returns:

  • (Boolean)

Since:

  • 0.1.0



15
16
17
# File 'lib/any_cache/adapters/active_support_redis_cache_store.rb', line 15

def supported_driver?(driver)
  AnyCache::Drivers::ActiveSupportRedisCacheStore.supported_source?(driver)
end

Instance Method Details

#decrement(key, amount = DEFAULT_INCR_DECR_AMOUNT, **options) ⇒ Integer, Float

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • key (String)
  • amount (Integer, Float) (defaults to: DEFAULT_INCR_DECR_AMOUNT)
  • expires_in (Hash)

    a customizable set of options

Returns:

  • (Integer, Float)

Since:

  • 0.1.0



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/any_cache/adapters/active_support_redis_cache_store.rb', line 159

def decrement(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) && -amount
  else
    driver.decrement(key, amount).tap do
      expire(key, expires_in: expires_in) if expires_in
    end
  end
end

#exist?(key, **options) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • key (String)
  • options (Hash)

Returns:

  • (Boolean)

Since:

  • 0.2.0



200
201
202
# File 'lib/any_cache/adapters/active_support_redis_cache_store.rb', line 200

def exist?(key, **options)
  driver.exist?(key)
end

#expire(key, expires_in: DEAD_TTL) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

  • key (String)
  • expires_in (Hash) (defaults to: DEAD_TTL)

    a customizable set of options

Options Hash (expires_in:):

  • (Integer)

Since:

  • 0.1.0



177
178
179
180
181
182
# File 'lib/any_cache/adapters/active_support_redis_cache_store.rb', line 177

def expire(key, expires_in: DEAD_TTL)
  read(key).tap do |value|
    is_alive = expires_in ? expires_in.positive? : false
    is_alive ? write(key, value, expires_in: expires_in) : delete(key)
  end
end

#fetch(key, **options, &fallback) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • key (String)
  • fallback (Proc)
  • expires_in (Hash)

    a customizable set of options

  • force (Hash)

    a customizable set of options

Returns:

  • (Object)

Since:

  • 0.2.0



107
108
109
110
111
112
113
114
# File 'lib/any_cache/adapters/active_support_redis_cache_store.rb', line 107

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, true)

  driver.fetch(key, force: force_rewrite, expires_in: expires_in, raw: raw, &fallback)
end

#fetch_multi(*keys, **options, &fallback) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • keys (Array<string])

    eys [Array<string]

  • fallback (Proc)
  • force (Hash)

    a customizable set of options

Returns:

  • (Hash)

Since:

  • 0.3.0



124
125
126
127
128
129
130
131
# File 'lib/any_cache/adapters/active_support_redis_cache_store.rb', line 124

def fetch_multi(*keys, **options, &fallback)
  # NOTE:
  #   use own :fetch_multi implementation cuz original :fetch_multi
  #   doesnt support :force option
  keys.each_with_object({}) do |key, dataset|
    dataset[key] = fetch(key, **options, &fallback)
  end
end

#increment(key, amount = DEFAULT_INCR_DECR_AMOUNT, **options) ⇒ Integer, Float

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • key (String)
  • amount (Integer, Float) (defaults to: DEFAULT_INCR_DECR_AMOUNT)
  • expires_in (Hash)

    a customizable set of options

Returns:

  • (Integer, Float)

Since:

  • 0.1.0



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/any_cache/adapters/active_support_redis_cache_store.rb', line 140

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) && amount
  else
    driver.increment(key, amount).tap do
      expire(key, expires_in: expires_in) if expires_in
    end
  end
end

#persist(key, **options) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

  • key (String)
  • options (Hash)

Since:

  • 0.1.0



190
191
192
# File 'lib/any_cache/adapters/active_support_redis_cache_store.rb', line 190

def persist(key, **options)
  read(key).tap { |value| write(key, value) }
end

#read(key, **options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • key (String)
  • options (Hash)

Returns:

  • (Object)

Since:

  • 0.1.0



53
54
55
56
57
# File 'lib/any_cache/adapters/active_support_redis_cache_store.rb', line 53

def read(key, **options)
  raw = options.fetch(:raw, true)

  driver.read(key, raw: raw)
end

#read_multi(*keys, **options) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • keys (Array<String>)
  • options (Hash)

Returns:

  • (Hash)

Since:

  • 0.3.0



65
66
67
68
69
70
71
# File 'lib/any_cache/adapters/active_support_redis_cache_store.rb', line 65

def read_multi(*keys, **options)
  raw = options.fetch(:raw, true)

  driver.read_multi(*keys, raw: raw).tap do |res|
    res.merge!(Hash[(keys - res.keys).zip(READ_MULTI_EMPTY_KEYS_SET)])
  end
end

#write(key, value, **options) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

  • key (String)
  • value (Object)
  • expires_in (Hash)

    a customizable set of options

Since:

  • 0.1.0



80
81
82
83
84
85
# File 'lib/any_cache/adapters/active_support_redis_cache_store.rb', line 80

def write(key, value, **options)
  expires_in = options.fetch(:expires_in, NO_EXPIRATION_TTL)
  raw = options.fetch(:raw, true)

  driver.write(key, value, expires_in: expires_in, raw: raw)
end

#write_multi(entries, **options) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

  • entries (Hash)
  • options (Hash)

Since:

  • 0.1.0



93
94
95
96
97
# File 'lib/any_cache/adapters/active_support_redis_cache_store.rb', line 93

def write_multi(entries, **options)
  raw = options.fetch(:raw, true)

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