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

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

#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



218
219
220
# File 'lib/any_cache/adapters/dalli.rb', line 218

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



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

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



146
147
148
# File 'lib/any_cache/adapters/dalli.rb', line 146

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



156
157
158
# File 'lib/any_cache/adapters/dalli.rb', line 156

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



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

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



198
199
200
201
# File 'lib/any_cache/adapters/dalli.rb', line 198

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



115
116
117
118
119
120
121
122
123
# File 'lib/any_cache/adapters/dalli.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)

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

    a customizable set of options

  • expires_in (Hash)

    a customizable set of options

Returns:

  • (Hash)

Since:

  • 0.3.0



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

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



167
168
169
170
171
172
173
174
# File 'lib/any_cache/adapters/dalli.rb', line 167

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



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

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

Returns:

  • (Object)

Since:

  • 0.1.0



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

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



75
76
77
78
79
# File 'lib/any_cache/adapters/dalli.rb', line 75

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



88
89
90
91
92
93
# File 'lib/any_cache/adapters/dalli.rb', line 88

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

  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



101
102
103
104
105
# File 'lib/any_cache/adapters/dalli.rb', line 101

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

  entries.each_pair { |key, value| write(key, value, raw: raw) }
end