Innate::Cache::API

This is the API every Cache has to conform to.

The default behaviour is tailored for the Memory cache, override any behaviour as you need.

key may be a String or Symbol value is a Hash of serializable (as according to Marshal) objects

Every cache instance has to respond to:

  ::new()
  #cache_setup(hostname, username, appname, cachename)
  #cache_clear()
  #cache_delete(*keys)
  #cache_fetch(key, default = nil)
  #cache_store(key, value, options = {})

We are prefixing cache_ to make the intent clear and implementation easier, as there may be existing behaviour associated with the non-prefixed version.

Also note that we create one instance per cache name-space.

Public Instance Methods

cache_clear() click to toggle source

Remove all key/value pairs from the cache. Should behave as if # had been called with all keys as argument.

@author manveru

    # File lib/innate/cache/api.rb, line 44
44:       def cache_clear
45:         clear
46:       end
cache_delete(key, *keys) click to toggle source

Remove the corresponding key/value pair for each key passed. If removing is not an option it should set the corresponding value to nil.

If only one key was deleted, answer with the corresponding value. If multiple keys were deleted, answer with an Array containing the values.

NOTE: Due to differences in the underlying implementation in the

      caches, some may not return the deleted value as it would mean
      another lookup before deletion. This is the case for caches on
      memcached or any database system.

@param [Object] key the key for the value to delete @param [Object] keys any other keys to delete as well @return [Object Array nil] @author manveru

    # File lib/innate/cache/api.rb, line 63
63:       def cache_delete(key, *keys)
64:         if keys.empty?
65:           if value = yield(key)
66:             value[:value]
67:           end
68:         else
69:           [key, *keys].map{|element| cache_delete(element) }
70:         end
71:       end
cache_fetch(key, default = nil) click to toggle source

Answer with the value associated with the key, nil if not found or expired.

@param [Object] key the key for which to fetch the value @param [Object] default will return this if no value was found @return [Object] @see Innate::Cache#fetch Innate::Cache#[] @author manveru

    # File lib/innate/cache/api.rb, line 81
81:       def cache_fetch(key, default = nil)
82:         value = default
83: 
84:         if entry = yield(key)
85:           if expires = entry[:expires]
86:             if expires > Time.now
87:               value = entry[:value]
88:             else
89:               cache_delete(key)
90:             end
91:           else
92:             value = entry[:value]
93:           end
94:         end
95: 
96:         return value
97:       end
cache_setup(hostname, username, appname, cachename) click to toggle source

Executed after # and before any other method.

Some parameters identifying the current process will be passed so caches that act in one global name-space can use them as a prefix.

@param [String #] hostname the hostname of the machine @param [String #] username user executing the process @param [String #] appname identifier for the application @param [String #] cachename namespace, like ‘session’ or ‘action’ @author manveru

    # File lib/innate/cache/api.rb, line 37
37:       def cache_setup(hostname, username, appname, cachename)
38:       end
cache_store(key, value, options = {}) click to toggle source

Set key to value.

options may be one of:

  :ttl => time to live in seconds if given in Numeric
                       infinite or maximum if not given

Usage:

  Cache.value.store(:num, 3, :ttl => 20)
  Cache.value.fetch(:num) # => 3
  sleep 21
  Cache.value.fetch(:num) # => nil

@param [Object] key the value is stored with this key @param [Object] value the key points to this value @param [Hash] options for now, only :ttl => Fixnum is used. @see Innate::Cache#store Innate::Cache#[]= @author manveru

     # File lib/innate/cache/api.rb, line 116
116:       def cache_store(key, value, options = {})
117:         ttl = options[:ttl]
118: 
119:         value_hash = {:value => value}
120:         value_hash[:expires] = Time.now + ttl if ttl
121: 
122:         yield(key, value_hash)
123: 
124:         return value
125:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.