Class: ActiveCachedResource::CachingStrategies::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_cached_resource/caching_strategies/base.rb

Direct Known Subclasses

ActiveSupportCache, SQLCache

Instance Method Summary collapse

Instance Method Details

#clear(pattern) ⇒ Boolean

Clears the cache based on the given pattern.

Parameters:

  • pattern (String)

    the pattern to match cache keys that need to be cleared.

Returns:

  • (Boolean)

    ‘true` if the cache was successfully cleared, `false` otherwise.



44
45
46
# File 'lib/active_cached_resource/caching_strategies/base.rb', line 44

def clear(pattern)
  clear_raw(pattern)
end

#delete(key) ⇒ void

This method returns an undefined value.

Deletes the cached value associated with the given key.

Parameters:

  • key (Object)

    the key whose associated cached value is to be deleted.



35
36
37
# File 'lib/active_cached_resource/caching_strategies/base.rb', line 35

def delete(key)
  delete_raw(hash_key(key))
end

#read(key) ⇒ Object?

Reads the value associated with the given key from the cache.

Parameters:

  • key (String, Symbol)

    the key to read from the cache.

Returns:

  • (Object, nil)

    the decompressed value associated with the key, or nil if the key is not found.

Raises:

  • (ArgumentError)


9
10
11
12
13
# File 'lib/active_cached_resource/caching_strategies/base.rb', line 9

def read(key)
  raise ArgumentError, "key must be a String or Symbol" unless key.is_a?(String) || key.is_a?(Symbol)
  raw_value = read_raw(hash_key(key))
  raw_value && decompress(raw_value)
end

#write(key, value, options) ⇒ Boolean

Writes an object to the cache with the specified key and options.

Parameters:

  • key (String, Symbol)

    The key used to store the object in the cache.

  • value (String)

    The value to be stored in the cache.

  • options (Hash)

    Options for the cache write operation. Must include ‘:expires_in`.

Options Hash (options):

  • :expires_in (Integer)

    The expiration time in seconds for the cached object. (required)

Returns:

  • (Boolean)

    ‘true` if the object was successfully written to the cache, `false` otherwise.

Raises:

  • (ArgumentError)

    If the ‘:expires_in` option is missing.



25
26
27
28
29
# File 'lib/active_cached_resource/caching_strategies/base.rb', line 25

def write(key, value, options)
  raise ArgumentError, "`expires_in` option is required" unless options[:expires_in]

  write_raw(hash_key(key), compress(value), options)
end