Class: AnyCache::Adapters::Redis Private

Inherits:
Basic
  • Object
show all
Defined in:
lib/any_cache/adapters/redis.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

Direct Known Subclasses

RedisStore

Constant Summary collapse

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
DELETE_MATCHED_CURSOR_START =

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:

  • (String)

Since:

  • 0.3.0

'0'
DELETE_MATCHED_BATCH_SIZE =

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.3.0

10

Instance Attribute Summary

Attributes inherited from Basic

#driver

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Basic

#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



13
14
15
# File 'lib/any_cache/adapters/redis.rb', line 13

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

Instance Method Details

#clear(**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:

  • options (Hash)

Since:

  • 0.1.0



237
238
239
# File 'lib/any_cache/adapters/redis.rb', line 237

def clear(**options)
  flushdb
end

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

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) (defaults to: DEFAULT_INCR_DECR_AMOUNT)

Returns:

  • (NillClass, Integer)

Since:

  • 0.1.0



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/any_cache/adapters/redis.rb', line 198

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) ⇒ 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



143
144
145
# File 'lib/any_cache/adapters/redis.rb', line 143

def delete(key, **options)
  del(key)
end

#delete_matched(pattern, **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:

  • pattern (String, Regexp)
  • options (Hash)

Since:

  • 0.3.0



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/any_cache/adapters/redis.rb', line 153

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) ⇒ 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



247
248
249
# File 'lib/any_cache/adapters/redis.rb', line 247

def exist?(key, **options)
  exists(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



216
217
218
219
220
# File 'lib/any_cache/adapters/redis.rb', line 216

def expire(key, expires_in: DEAD_TTL)
  expires_in ||= DEAD_TTL unless expires_in

  driver.expire(key, expires_in)
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)
  • expires_in (Hash)

    a customizable set of options

  • force (Hash)

    a customizable set of options

Returns:

  • (Object)

Since:

  • 0.2.0



113
114
115
116
117
118
119
120
121
# File 'lib/any_cache/adapters/redis.rb', line 113

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).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) ⇒ 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)
  • fallback (Proc)

Returns:

  • (Hash)

Since:

  • 0.3.0



130
131
132
133
134
135
# File 'lib/any_cache/adapters/redis.rb', line 130

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) ⇒ NilClass, Integer

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) (defaults to: DEFAULT_INCR_DECR_AMOUNT)
  • expires_in (Hash)

    a customizable set of options

Returns:

  • (NilClass, Integer)

Since:

  • 0.1.0



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/any_cache/adapters/redis.rb', line 179

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) ⇒ 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



228
229
230
# File 'lib/any_cache/adapters/redis.rb', line 228

def persist(key, **options)
  driver.persist(key)
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



69
70
71
# File 'lib/any_cache/adapters/redis.rb', line 69

def read(key, **options)
  get(key)
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



79
80
81
# File 'lib/any_cache/adapters/redis.rb', line 79

def read_multi(*keys, **options)
  mapped_mget(*keys)
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



90
91
92
93
94
# File 'lib/any_cache/adapters/redis.rb', line 90

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

  expires_in ? setex(key, expires_in, value) : set(key, value)
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.3.0



102
103
104
# File 'lib/any_cache/adapters/redis.rb', line 102

def write_multi(entries, **options)
  mapped_mset(entries)
end