Class: Sir::Backends::Base
- Inherits:
-
Object
- Object
- Sir::Backends::Base
- Defined in:
- lib/sir/backends/Base.rb
Overview
CONFIG: a template of the default configuration hash for this module
Direct Known Subclasses
Constant Summary collapse
- EXPORTS =
[:get, :put, :kill, :dump, :nuke, :sweep, :flush, :able?, :length, :keys]
Class Method Summary collapse
-
.able? ⇒ Boolean
Pings the backend, if applicable.
- .arity(func) ⇒ Object
-
.configure(options_hash) ⇒ Object
Sets configuration properties.
-
.dump ⇒ Object
Dumps all keys to console.
-
.flush ⇒ Object
Ask backend to flush to disk, if supported.
-
.get(key) ⇒ Object
Gets a copy of the key from the cache.
-
.keys(mask = "*") ⇒ array
List all keys in the cache WARNING: This is slow!.
-
.kill(key) ⇒ Object
Deletes a key in the cache.
-
.length ⇒ integer
Gets the number of keys in the cache.
-
.nuke ⇒ void
Deletes all keys in backend.
-
.post_configure ⇒ Object
Called immediately after @@config has been set.
-
.put(key, value, expiry) ⇒ Object
Puts a key in the cache.
-
.sweep(include_nil_expiry) ⇒ Object
Sweeps all keys and invalidates (ie. #kill) any that have expired.
Class Method Details
.able? ⇒ Boolean
Pings the backend, if applicable
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.
137 138 139 140 141 |
# File 'lib/sir/backends/Base.rb', line 137 def self.configure() Sir.annoy("configure #{}") @@config = self.post_configure end |
.dump ⇒ Object
Dumps all keys to console
77 78 79 |
# File 'lib/sir/backends/Base.rb', line 77 def self.dump raise NotImplementedError end |
.flush ⇒ Object
Ask backend to flush to disk, if supported
107 108 109 |
# File 'lib/sir/backends/Base.rb', line 107 def self.flush raise NotImplementedError end |
.get(key) ⇒ Object
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!)
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!
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
68 69 70 |
# File 'lib/sir/backends/Base.rb', line 68 def self.kill(key) raise NotImplementedError end |
.length ⇒ integer
Gets the number of keys in the cache
116 117 118 |
# File 'lib/sir/backends/Base.rb', line 116 def self.length raise NotImplementedError end |
.nuke ⇒ void
Deletes all keys in backend. CAREFUL!!! (FLUSHDB in Redis)
87 88 89 |
# File 'lib/sir/backends/Base.rb', line 87 def self.nuke raise NotImplementedError end |
.post_configure ⇒ Object
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
1: Will invalidate any stored references
Recommended to use this function inside a block from #get (see #get)
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
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
Run this function in a new thread every so often
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.
99 100 101 |
# File 'lib/sir/backends/Base.rb', line 99 def self.sweep(include_nil_expiry) raise NotImplementedError end |