Class: Sir::Backends::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sir/backends/Base.rb

Overview

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

Direct Known Subclasses

RamCache, RedisCache

Constant Summary collapse

EXPORTS =
[:get, :put, :kill, :dump, :nuke, :sweep, :flush, :able?, :length, :keys]

Class Method Summary collapse

Class Method Details

.able?Boolean

Pings the backend, if applicable

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/sir/backends/Base.rb', line 47

def self.able?
  raise NotImplementedError
end

.arity(func) ⇒ Object



143
144
145
# File 'lib/sir/backends/Base.rb', line 143

def self.arity(func)
  method(func).arity
end

.configure(options_hash) ⇒ Object

Sets configuration properties.

Parameters:

  • options_hash (hash)

    should be a (modified, or not) copy of the backend’s CONFIG constant



137
138
139
140
141
# 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

.dumpObject

Dumps all keys to console

Returns:

  • true if success

Raises:

  • NotImplementedError if function is not implemented in selected backend module

  • TypeError if function is not supported/needed by the backend



77
78
79
# File 'lib/sir/backends/Base.rb', line 77

def self.dump
  raise NotImplementedError
end

.flushObject

Ask backend to flush to disk, if supported

Returns:

  • true if success

Raises:

  • NotImplementedError if function is not implemented in selected backend module

  • TypeError if function is not supported/needed by the backend



107
108
109
# File 'lib/sir/backends/Base.rb', line 107

def self.flush
  raise NotImplementedError
end

.get(key) ⇒ Object

TODO:

1: Mutiple calls to the same key will return a reference to the original object

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!)

Parameters:

  • key (symbol)

    name of your key

Returns:

  • 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



30
31
32
33
34
35
36
37
38
39
40
41
42
# 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 = "*") ⇒ array

List all keys in the cache WARNING: This is slow!

Returns:

  • (array)

    List of keys

Raises:

  • (NotImplementedError)


123
124
125
# File 'lib/sir/backends/Base.rb', line 123

def self.keys(mask = "*")
  raise NotImplementedError
end

.kill(key) ⇒ Object

Deletes a key in the cache

Parameters:

  • key (symbol)

    name of your key

Returns:

  • true if deleted

Raises:

  • NotImplementedError if function is not implemented in selected backend module



68
69
70
# File 'lib/sir/backends/Base.rb', line 68

def self.kill(key)
  raise NotImplementedError
end

.lengthinteger

Gets the number of keys in the cache

Returns:

  • (integer)

    number of keys in cache

Raises:

  • NotImplementedError if function is not implemented in selected backend module

  • TypeError if function is not supported/needed by the backend



116
117
118
# File 'lib/sir/backends/Base.rb', line 116

def self.length
  raise NotImplementedError
end

.nukevoid

Deletes all keys in backend. CAREFUL!!! (FLUSHDB in Redis)

Returns:

  • (void)
  • true if success

Raises:

  • NotImplementedError if function is not implemented in selected backend module

  • TypeError if function is not supported/needed by the backend



87
88
89
# File 'lib/sir/backends/Base.rb', line 87

def self.nuke
  raise NotImplementedError
end

.post_configureObject

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



128
129
# File 'lib/sir/backends/Base.rb', line 128

def self.post_configure
end

.put(key, value, expiry) ⇒ Object

TODO:

1: Will invalidate any stored references

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)

Puts a key in the cache. Overwrites existing value if already exist

Parameters:

  • key (symbol)

    name of your key

Returns:

  • value

Raises:

  • NotImplementedError if function is not implemented in selected backend module



59
60
61
# File 'lib/sir/backends/Base.rb', line 59

def self.put(key, value, expiry)
  raise NotImplementedError
end

.sweep(include_nil_expiry) ⇒ Object

TODO:

Run this function in a new thread every so often

Note:

It is important to run #clean() periodically to avoid leaks. (Put this in a cronjob in multiprocess/cluster scenarios)

Sweeps all keys and invalidates (ie. #kill) any that have expired. This is only required for backends that do not facilitate automatic key expiration.

Parameters:

  • include_nil_expiry (Boolean)

    Also invalidate nil expiry if true

Returns:

  • true if success

Raises:

  • NotImplementedError if function is not implemented in selected backend module

  • TypeError if function is not supported/needed by the backend



99
100
101
# File 'lib/sir/backends/Base.rb', line 99

def self.sweep(include_nil_expiry)
  raise NotImplementedError
end