Class: AnyCache::Adapters::ActiveSupportDalliStore Private

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

1
INITIAL_DECREMNETED_VALUE =

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

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

0

Instance Attribute Summary

Attributes inherited from Basic

#driver

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Basic

#cleanup, #clear, #delete, #initialize

Methods included from Dumper::InterfaceAccessMixin

#detransform_pairset, #detransform_value, #transform_pairset, #transform_value

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



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

def supported_driver?(driver)
  AnyCache::Drivers::ActiveSupportDalliStore.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) (defaults to: DEFAULT_INCR_DECR_AMOUNT)
  • expires_in (Hash)

    a customizable set of options

Returns:

  • (Integer, Float)

Since:

  • 0.3.0



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/any_cache/adapters/active_support_dalli_store.rb', line 163

def decrement(key, amount = DEFAULT_INCR_DECR_AMOUNT, **options)
  expires_in = options.fetch(:expires_in, NO_EXPIRATION_TTL)

  unless exist?(key)
    # NOTE: Dalli::Client (under the hood of this) 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) ⇒ 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



186
187
188
# File 'lib/any_cache/adapters/active_support_dalli_store.rb', line 186

def delete_matched(pattern, **options)
  # TODO: make it real >:]
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.3.0



219
220
221
# File 'lib/any_cache/adapters/active_support_dalli_store.rb', line 219

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



196
197
198
199
200
201
# File 'lib/any_cache/adapters/active_support_dalli_store.rb', line 196

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

  • raw (Hash)

    a customizable set of options

Returns:

  • (Object)

Since:

  • 0.3.0



115
116
117
118
119
120
121
122
# File 'lib/any_cache/adapters/active_support_dalli_store.rb', line 115

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



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

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

Returns:

  • (Integer, Float)

Since:

  • 0.3.0



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/any_cache/adapters/active_support_dalli_store.rb', line 144

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



209
210
211
# File 'lib/any_cache/adapters/active_support_dalli_store.rb', line 209

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

#read(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)
  • raw (Hash)

    a customizable set of options

Since:

  • 0.3.0



57
58
59
60
61
# File 'lib/any_cache/adapters/active_support_dalli_store.rb', line 57

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

  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>)
  • raw (Hash)

    a customizable set of options

Returns:

  • (Hash)

Since:

  • 0.3.0



69
70
71
72
73
74
75
# File 'lib/any_cache/adapters/active_support_dalli_store.rb', line 69

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

  • raw (Hash)

    a customizable set of options

Since:

  • 0.3.0



85
86
87
88
89
90
# File 'lib/any_cache/adapters/active_support_dalli_store.rb', line 85

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) ⇒ 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)
  • raw (Hash)

    a customizable set of options

Since:

  • 0.3.0



98
99
100
101
102
103
104
105
# File 'lib/any_cache/adapters/active_support_dalli_store.rb', line 98

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

  # NOTE: ActiveSupport::Cache::DalliStore does not support #write_multi :   entries.each_pair do |key, value|
    write(key, value, expires_in: NO_EXPIRATION_TTL, raw: raw)
  end
end