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.



37
38
39
# File 'lib/active_cached_resource/caching_strategies/base.rb', line 37

def clear(pattern)
  clear_raw(pattern)
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)


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

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.



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

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