Class: AnyCache::Adapters::Dalli Private

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

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:

  • (Integer)

Since:

  • 0.1.0

0
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:

  • (NilClass)

Since:

  • 0.1.0

nil
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
MIN_DECRESEAD_VAL =

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

Instance Attribute Summary

Attributes inherited from Basic

#driver

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Basic

#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.1.0



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

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

Instance Method Details

#cleanup(**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.4.0



243
244
245
# File 'lib/any_cache/adapters/dalli.rb', line 243

def cleanup(**options)
  # NOTE: manual removing is not suppored (memcached doing it by itself)
end

#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



234
235
236
# File 'lib/any_cache/adapters/dalli.rb', line 234

def clear(**options)
  flush(0) # NOTE: 0 is a flush delay
end

#decrement(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



199
200
201
202
203
204
205
206
# File 'lib/any_cache/adapters/dalli.rb', line 199

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

  # TODO: think about #cas and Concurrent::ReentrantReadWriteLock
  decr(key, amount, expires_in, MIN_DECRESEAD_VAL).tap do |new_amount|
    touch(key, expires_in) if new_amount && expires_in.positive?
  end
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



162
163
164
# File 'lib/any_cache/adapters/dalli.rb', line 162

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



172
173
174
# File 'lib/any_cache/adapters/dalli.rb', line 172

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



253
254
255
# File 'lib/any_cache/adapters/dalli.rb', line 253

def exist?(key, **options)
  !get(key).nil? # NOTE: can conflict with :cache_nils Dalli::Client's config
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



214
215
216
217
# File 'lib/any_cache/adapters/dalli.rb', line 214

def expire(key, expires_in: DEAD_TTL)
  is_alive = expires_in ? expires_in.positive? : false
  is_alive ? touch(key, expires_in) : driver.delete(key)
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



131
132
133
134
135
136
137
138
139
# File 'lib/any_cache/adapters/dalli.rb', line 131

def fetch(key, **options, &fallback)
  force_rewrite = options.fetch(:force, false)
  force_rewrite = force_rewrite.call(key) if force_rewrite.respond_to?(:call)

  # NOTE: can conflict with :cache_nils Dalli::Client's config
  read(key, **options).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>)
  • fallback (Proc)
  • force (Hash)

    a customizable set of options

  • expires_in (Hash)

    a customizable set of options

Returns:

  • (Hash)

Since:

  • 0.3.0



149
150
151
152
153
154
# File 'lib/any_cache/adapters/dalli.rb', line 149

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



183
184
185
186
187
188
189
190
# File 'lib/any_cache/adapters/dalli.rb', line 183

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

  # TODO: think about #cas and Concurrent::ReentrantReadWriteLock
  incr(key, amount, expires_in, amount).tap do |new_amount|
    touch(key, expires_in) if new_amount && expires_in.positive?
  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



225
226
227
# File 'lib/any_cache/adapters/dalli.rb', line 225

def persist(key, **options)
  touch(key, NO_EXPIRATION_TTL)
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)
  • raw (Hash)

    a customizable set of options

Returns:

  • (Object)

Since:

  • 0.1.0



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

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

  raw ? value : detransform_value(value)
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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/any_cache/adapters/dalli.rb', line 78

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

  entries = get_multi(*keys).tap do |res|
    # NOTE:
    #   dalli does not return nonexistent entries
    #   but we want to be consistent with another cache storages
    #   that returns nonexistent antries as { key => nil } pair
    res.merge!(Hash[(keys.map(&:to_s) - res.keys).zip(READ_MULTI_EMPTY_KEYS_SET)])

    # NOTE:
    #   dalli stringifies requred keys but we want to be consistent with symbol keys
    #   cuz another cache storages are already consistent
    keys.each { |key| res.key?(key) ? next : (res[key] = res.delete(key.to_s)) }
  end

  raw ? entries : detransform_pairset(entries)
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)
  • raw (Hash)

    a customizable set of options

  • expires_in (Hash)

    a customizable set of options

Since:

  • 0.1.0



105
106
107
108
109
110
111
# File 'lib/any_cache/adapters/dalli.rb', line 105

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

  set(key, value, 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.3.0



119
120
121
# File 'lib/any_cache/adapters/dalli.rb', line 119

def write_multi(entries, **options)
  entries.each_pair { |key, value| write(key, value, **options) }
end