Class: Elephas::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/elephas/cache.rb

Overview

This is the main class of the framework. Use only this class to access the cache.

Class Attribute Summary (collapse)

Class Method Summary (collapse)

Class Attribute Details

+ (Object) provider

Returns the value of attribute provider



13
14
15
# File 'lib/elephas/cache.rb', line 13

def provider
  @provider
end

Class Method Details

+ (String) default_prefix

Returns the default prefix for cache entries.

Returns:

  • (String)

    The default prefix for cache entries.



95
96
97
# File 'lib/elephas/cache.rb', line 95

def self.default_prefix
  "elephas-#{::Elephas::Version::STRING}-cache"
end

+ (Boolean) delete(key)

Deletes a value from the cache.

Parameters:

  • key (String)

    The key to delete.

Returns:

  • (Boolean)

    true if the key was in the cache, false otherwise.



80
81
82
# File 'lib/elephas/cache.rb', line 80

def self.delete(key)
  @provider.delete(key)
end

+ (Boolean) exists?(key)

Checks if a key exists in the cache.

Parameters:

  • key (String)

    The key to lookup.

Returns:

  • (Boolean)

    true if the key is in the cache, false otherwise.



88
89
90
# File 'lib/elephas/cache.rb', line 88

def self.exists?(key)
  @provider.exists?(key)
end

+ (Object|NilClass) read(key)

Reads a value from the cache.

Parameters:

  • key (String)

    The key to lookup.

Returns:

  • (Object|NilClass)

    The read value or nil.



61
62
63
# File 'lib/elephas/cache.rb', line 61

def self.read(key)
  @provider.read(key)
end

+ (Hash) setup_options(options, key)

Setups options for use into the framework. Valid options are:

  • :ttl: The TTL (time to live, in milliseconds) of the entry. It means how long will the value stay in cache. Setting it to 0 or less means never cache the entry.
  • :force: Setting it to true will always skip the cache.
  • :key: The key associated to this value. You should never set this option directly.
  • :prefix: The prefix used in cache. This is used to avoid conflicts with other caching frameworks.
  • :complete_key: The complete key used for computing the hash. By default is concatenation of :key and :prefix options.
  • :hash: The hash used to store the key in the cache. Should be unique
  • :as_entry: In Elephas::Cache.use, setting this to true will return the entire Entry object rather than the value only.

Parameters:

  • options (Object)

    An initial setup.

  • key (String)

    The key to associate to this options.

Returns:

  • (Hash)

    An options hash.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/elephas/cache.rb', line 113

def self.setup_options(options, key)
  options = {} if !options.is_a?(::Hash)
  options = {ttl: 1.hour * 1000, force: false, as_entry: false}.merge(options)

  # Sanitize options.
  options = sanitize_options(options, key)

  # Wrap the final key to ensure we don't have colliding namespaces.
  options[:complete_key] ||= "#{options[:prefix]}[#{options[:key]}]"

  # Compute the hash key used for referencing this value.
  options[:hash] ||= ::Elephas::Entry.hashify_key(options[:complete_key])

  options
end

+ (Object|Entry) use(key, options = {}, &block)

This is the main method of the framework.

It tries reading a key from the cache.

If it doesn’t find it, it uses the provided block (which receives options as argument) to compute its value and then store it into the cache for later usages.

```ruby value = Elephas::Cache.use(“KEY”) do |options| “VALUE” end

value # => “VALUE”

value = Elephas::Cache.use(“KEY”) do |options| “ANOTHER VALUE” end

value # => “VALUE” ```

Parameters:

  • key (String)

    The key to lookup.

  • options (Hash) (defaults to: {})

    A list of options for managing this key.

  • block (Proc)

    An optional block to run to compute the value for the key if nothing is found.

Returns:

  • (Object|Entry)

    The found or newly-set value associated to the key.

See Also:



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/elephas/cache.rb', line 43

def self.use(key, options = {}, &block)
  rv = nil

  # Get options
  options = ::Elephas::Cache.setup_options(options, key)

  # Check if the storage has the value (if we don't have to skip the cache)
  rv = @provider.read(options[:hash]) if options[:force] == false && options[:ttl] > 0
  rv = compute_value(options, &block) if rv.nil? && block # Try to compute the value from the block

  # Return value
  options[:as_entry] ? rv : rv.value.dup
end

+ (Object) write(key, value, options = {})

Writes a value to the cache.

Parameters:

  • key (String)

    The key to associate the value with.

  • value (Object)

    The value to write. Setting a value to nil doesn’t mean deleting the value.

  • options (Hash) (defaults to: {})

    A list of options for writing.

Returns:

  • (Object)

    The value itself.

See Also:



72
73
74
# File 'lib/elephas/cache.rb', line 72

def self.write(key, value, options = {})
  @provider.write(key, value, ::Elephas::Cache.setup_options(options, key))
end