class Sir::Backends::Base

CONFIG: a template of the default configuration hash for this module

Constants

EXPORTS

Public Class Methods

able?() click to toggle source

Pings the backend, if applicable @returns true if backend is correctly configured and able to accept requests

# File lib/sir/backends/Base.rb, line 47
def self.able?
  raise NotImplementedError
end
arity(func) click to toggle source
# File lib/sir/backends/Base.rb, line 143
def self.arity(func)
  method(func).arity
end
configure(options_hash) click to toggle source

Sets configuration properties. @param options_hash [hash] should be a (modified, or not) copy of the backend's CONFIG constant

# File lib/sir/backends/Base.rb, line 137
def self.configure(options_hash)
  Sir.annoy("configure #{options_hash}")
  @@config = options_hash
  self.post_configure
end
dump() click to toggle source

Dumps all keys to console @raise NotImplementedError if function is not implemented in selected backend module @raise TypeError if function is not supported/needed by the backend @return true if success

# File lib/sir/backends/Base.rb, line 77
def self.dump
  raise NotImplementedError
end
flush() click to toggle source

Ask backend to flush to disk, if supported @raise NotImplementedError if function is not implemented in selected backend module @raise TypeError if function is not supported/needed by the backend @return true if success

# File lib/sir/backends/Base.rb, line 107
def self.flush
  raise NotImplementedError
end
get(key) { |key| ... } click to toggle source

Gets a copy of the key from the cache. Yields to a block that should return the value you were looking for. (see put)

Backends implementing get() must call super if the object is not found (let Base handle that stuff!)

@todo 1: Mutiple calls to the same key will return a reference to the original object @param key [symbol] name of your key @return

value if the object was found in the cache, or
value returned by supplied block if the object expired or was not found and a block was supplied, or
nil if object was expired or not found, and no block was supplied
# File lib/sir/backends/Base.rb, line 30
def self.get(key)

  Sir.annoy("Cache miss on #{key}")

  if block_given?
    Sir.annoy("Block given, yielding to #{key}")
    return yield(key)
  else
    Sir.annoy("No Block given")
    return nil
  end

end
keys(mask = "*") click to toggle source

List all keys in the cache WARNING: This is slow! @return [array] List of keys

# File lib/sir/backends/Base.rb, line 123
def self.keys(mask = "*")
  raise NotImplementedError
end
kill(key) click to toggle source

Deletes a key in the cache @param key [symbol] name of your key @return true if deleted @raise NotImplementedError if function is not implemented in selected backend module

# File lib/sir/backends/Base.rb, line 68
def self.kill(key)
  raise NotImplementedError
end
length() click to toggle source

Gets the number of keys in the cache @return [integer] number of keys in cache @raise NotImplementedError if function is not implemented in selected backend module @raise TypeError if function is not supported/needed by the backend

# File lib/sir/backends/Base.rb, line 116
def self.length
  raise NotImplementedError
end
nuke() click to toggle source

Deletes all keys in backend. CAREFUL!!! (FLUSHDB in Redis) @return [void] @raise NotImplementedError if function is not implemented in selected backend module @raise TypeError if function is not supported/needed by the backend @return true if success

# File lib/sir/backends/Base.rb, line 87
def self.nuke
  raise NotImplementedError
end
post_configure() click to toggle source

Called immediately after @@config has been set. Child backends should inherit this to react to configuration changes

# File lib/sir/backends/Base.rb, line 128
def self.post_configure
end
put(key, value, expiry) click to toggle source

Puts a key in the cache. Overwrites existing value if already exist @note Recommended to use this function inside a block from get (see get) @note It would be a good idea to call put whenever the object is saved (like :after_save) @caveat Be careful about storing nil values – nil is returned by get when an object is not found @todo 1: Will invalidate any stored references @param key [symbol] name of your key @return value @raise NotImplementedError if function is not implemented in selected backend module

# File lib/sir/backends/Base.rb, line 59
def self.put(key, value, expiry)
  raise NotImplementedError
end
sweep(include_nil_expiry) click to toggle source

Sweeps all keys and invalidates (ie. kill) any that have expired. This is only required for backends that do not facilitate automatic key expiration. @note It is important to run clean() periodically to avoid leaks. (Put this in a cronjob in multiprocess/cluster scenarios) @todo Run this function in a new thread every so often @param include_nil_expiry [Boolean] Also invalidate nil expiry if true @return true if success @raise NotImplementedError if function is not implemented in selected backend module @raise TypeError if function is not supported/needed by the backend

# File lib/sir/backends/Base.rb, line 99
def self.sweep(include_nil_expiry)
  raise NotImplementedError
end

Private Class Methods

valid?(hash) click to toggle source
# File lib/sir/backends/Base.rb, line 149
def self.valid?(hash)
  return "Key must coerce to a symbol" unless hash[:key].respond_to?(:intern) if hash[:key]
  return "Time must coerce to a valid timestamp or timespan in seconds" unless (hash[:expiry].is_a?(Time) || hash[:expiry].is_a?(Integer) || (defined?(Rails) && hash[:expiry].is_a?(DateTime) )) if hash[:expiry]
end