Class: AnyCache::Adapters::ActiveSupportNaiveStore Private

Inherits:
Delegator show all
Defined in:
lib/any_cache/adapters/active_support_naive_store.rb,
lib/any_cache/adapters/active_support_naive_store/expire.rb,
lib/any_cache/adapters/active_support_naive_store/persist.rb,
lib/any_cache/adapters/active_support_naive_store/decrement.rb,
lib/any_cache/adapters/active_support_naive_store/increment.rb,
lib/any_cache/adapters/active_support_naive_store/operation.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

Defined Under Namespace

Classes: Decrement, Expire, Increment, Operation, Persist

Instance Attribute Summary

Attributes inherited from Basic

#driver

Instance Method Summary collapse

Methods inherited from Delegator

supported_driver?

Methods inherited from Basic

supported_driver?

Constructor Details

#initialize(driver) ⇒ 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.

Parameters:

  • driver (Object)

Since:

  • 0.1.0



18
19
20
21
22
23
24
25
# File 'lib/any_cache/adapters/active_support_naive_store.rb', line 18

def initialize(driver)
  super
  @lock = Concurrent::ReentrantReadWriteLock.new
  @incr_operation = self.class::Increment.new(driver)
  @decr_operation = self.class::Decrement.new(driver)
  @expr_operation = self.class::Expire.new(driver)
  @pers_operation = self.class::Persist.new(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



76
77
78
# File 'lib/any_cache/adapters/active_support_naive_store.rb', line 76

def clear(**options)
  lock.with_write_lock { super }
end

#decrement(key, amount = self.class::Decrement::DEFAULT_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: self.class::Decrement::DEFAULT_AMOUNT)
  • expires_in (Hash)

    a customizable set of options

Returns:

  • (Integer, Float)

Since:

  • 0.1.0



172
173
174
175
176
177
178
# File 'lib/any_cache/adapters/active_support_naive_store.rb', line 172

def decrement(key, amount = self.class::Decrement::DEFAULT_AMOUNT, **options)
  lock.with_write_lock do
    expires_in = options.fetch(:expires_in, self.class::Operation::NO_EXPIRATION_TTL)

    decr_operation.call(key, amount, expires_in: expires_in)
  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



57
58
59
# File 'lib/any_cache/adapters/active_support_naive_store.rb', line 57

def delete(key, **options)
  lock.with_write_lock { super }
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



67
68
69
# File 'lib/any_cache/adapters/active_support_naive_store.rb', line 67

def delete_matched(pattern, **options)
  lock.with_write_lock { super }
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



206
207
208
# File 'lib/any_cache/adapters/active_support_naive_store.rb', line 206

def exist?(key, **options)
  lock.with_read_lock { super }
end

#expire(key, expires_in: self.class::Operation::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: self.class::Operation::DEAD_TTL)

    a customizable set of options

Options Hash (expires_in:):

  • (NilClass, Integer)

Since:

  • 0.1.0



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

def expire(key, expires_in: self.class::Operation::DEAD_TTL)
  lock.with_write_lock { expr_operation.call(key, expires_in: 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)
  • fallback (Proc)
  • expires_in (Hash)

    a customizable set of options

  • force (Hash)

    a customizable set of options

Returns:

  • (Object)

Since:

  • 0.2.0



117
118
119
120
121
122
123
124
125
# File 'lib/any_cache/adapters/active_support_naive_store.rb', line 117

def fetch(key, **options, &fallback)
  lock.with_write_lock do
    force_rewrite = options.fetch(:force, false)
    force_rewrite = force_rewrite.call(key) if force_rewrite.respond_to?(:call)
    expires_in    = options.fetch(:expires_in, self.class::Operation::NO_EXPIRATION_TTL)

    super(key, force: force_rewrite, expires_in: expires_in, &fallback)
  end
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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/any_cache/adapters/active_support_naive_store.rb', line 135

def fetch_multi(*keys, **options, &fallback)
  lock.with_write_lock do
    force_rewrite = options.fetch(:force, false)
    expires_in    = options.fetch(:expires_in, self.class::Operation::NO_EXPIRATION_TTL)

    # NOTE:
    #   use own #fetch_multi implementation cuz original #fetch_multi
    #   does not support :force option
    keys.each_with_object({}) do |key, dataset|
      force = force_rewrite.respond_to?(:call) ? force_rewrite.call(key) : force_rewrite
      dataset[key] = driver.fetch(key, force: force, expires_in: expires_in, &fallback)
    end
  end
end

#increment(key, amount = self.class::Increment::DEFAULT_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: self.class::Increment::DEFAULT_AMOUNT)
  • expires_in (Hash)

    a customizable set of options

Returns:

  • (Integer, Float)

Since:

  • 0.1.0



157
158
159
160
161
162
163
# File 'lib/any_cache/adapters/active_support_naive_store.rb', line 157

def increment(key, amount = self.class::Increment::DEFAULT_AMOUNT, **options)
  lock.with_write_lock do
    expires_in = options.fetch(:expires_in, self.class::Operation::NO_EXPIRATION_TTL)

    incr_operation.call(key, amount, expires_in: expires_in)
  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



196
197
198
# File 'lib/any_cache/adapters/active_support_naive_store.rb', line 196

def persist(key, **options)
  lock.with_write_lock { pers_operation.call(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



33
34
35
# File 'lib/any_cache/adapters/active_support_naive_store.rb', line 33

def read(key, **options)
  lock.with_read_lock { super }
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



43
44
45
46
47
48
49
# File 'lib/any_cache/adapters/active_support_naive_store.rb', line 43

def read_multi(*keys, **options)
  lock.with_read_lock do
    super.tap do |res|
      res.merge!(Hash[(keys - res.keys).zip(Operation::READ_MULTI_EMPTY_KEYS_SET)])
    end
  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)
  • options (Hash)

Since:

  • 0.1.0



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

def write(key, value, **options)
  lock.with_write_lock do
    expires_in = options.fetch(:expires_in, self.class::Operation::NO_EXPIRATION_TTL)

    super(key, value, expires_in: expires_in)
  end
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
106
107
# File 'lib/any_cache/adapters/active_support_naive_store.rb', line 101

def write_multi(entries, **options)
  lock.with_write_lock do
    expires_in = self.class::Operation::NO_EXPIRATION_TTL

    super(entries, expires_in: expires_in)
  end
end