Class: Ramaze::Cache::Sequel
- Inherits:
-
Object
- Object
- Ramaze::Cache::Sequel
- Includes:
- Cache::API, Innate::Traited
- Defined in:
- lib/ramaze/cache/sequel.rb
Overview
The Sequel cache is a cache system that uses the Sequel database toolkit to store the data in a DBMS supported by Sequel. Examples of these databases are MySQL, SQLite3 and so on. In order to use this cache you’d have to do the following:
Ramaze::Cache..view = Ramaze::Cache::Sequel.using(
:connection => Sequel.mysql(
:host => 'localhost',
:user => 'user',
:password => 'password',
:database => 'blog'
),
:table => :blog_sessions
)
If you already have an existing connection you can just pass the object to the :connection option instead of creating a new connection manually.
Massive thanks to Lars Olsson for patching the original Sequel cache so that it supports multiple connections and other useful features.
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#options ⇒ Object
Hash containing all the default options merged with the user specified ones.
Class Method Summary collapse
-
.using(options = {}) ⇒ Object
This method returns a subclass of Ramaze::Cache::Sequel with the provided options set.
Instance Method Summary collapse
-
#cache_clear ⇒ Object
Remove all key/value pairs from the cache.
-
#cache_delete(key, *keys) ⇒ Object/Array/nil
Remove the corresponding key/value pair for each key passed.
-
#cache_fetch(key, default = nil) ⇒ Object
Answer with the value associated with the
key
,nil
if not found or expired. -
#cache_setup(hostname, username, appname, cachename) ⇒ Object
Executed after #initialize and before any other method.
-
#cache_store(key, value, options = {}) ⇒ Object
Sets the given key to the given value.
-
#deserialize(value) ⇒ Object nil
Deserialize method, adapted from Sequels serialize plugin This method will try to deserialize a value using Marshal.load.
-
#initialize(options = {}) ⇒ Sequel
constructor
Creates a new instance of the cache class.
-
#namespaced(key) ⇒ Object
Prefixes the given key with current namespace.
-
#serialize(value) ⇒ Object nil
Serialize method, adapted from Sequels serialize plugin This method will try to serialize a value using Marshal.dump.
Constructor Details
Class Attribute Details
.options ⇒ Object
66 67 68 |
# File 'lib/ramaze/cache/sequel.rb', line 66 def @options end |
Instance Attribute Details
#options ⇒ Object
Hash containing all the default options merged with the user specified ones
63 64 65 |
# File 'lib/ramaze/cache/sequel.rb', line 63 def @options end |
Class Method Details
.using(options = {}) ⇒ Object
This method returns a subclass of Ramaze::Cache::Sequel with the provided options set. This is necessary because Ramaze expects a class and not an instance of a class for its cache option.
You can provide any parameters you want, but those not used by the cache will not get stored. No parameters are mandatory. Any missing parameters will be replaced by default values.
121 122 123 124 |
# File 'lib/ramaze/cache/sequel.rb', line 121 def using( = {}) merged = Ramaze::Cache::Sequel.trait[:default].merge() Class.new(self) { @options = merged } end |
Instance Method Details
#cache_clear ⇒ Object
Remove all key/value pairs from the cache. Should behave as if #delete had been called with all keys
as argument.
180 181 182 |
# File 'lib/ramaze/cache/sequel.rb', line 180 def cache_clear @dataset.delete end |
#cache_delete(key, *keys) ⇒ Object/Array/nil
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.
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/ramaze/cache/sequel.rb', line 197 def cache_delete(key, *keys) # Remove a single key if keys.empty? nkey = namespaced(key) result = @dataset.select(:value).filter(:key => nkey).limit(1) # Ramaze expects nil values if result.empty? result = nil else result = deserialize(result.first[:value]) end @dataset.filter(:key => nkey).delete # Remove multiple keys else nkeys = [key, keys].flatten.map! { |n| namespaced(n) } result = dataset.select(:value).filter(:key => nkeys) result.map! do |row| deserialize(row[:value]) end @dataset.filter(:key => nkeys).delete end return result end |
#cache_fetch(key, default = nil) ⇒ Object
Answer with the value associated with the key
, nil
if not found or expired.
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/ramaze/cache/sequel.rb', line 236 def cache_fetch(key, default = nil) nkey = namespaced(key) # Delete expired rows @dataset.select.filter(:key => nkey) do expires < Time.now end.delete # Get remaining row (if any) result = @dataset.select(:value).filter(:key => nkey).limit(1) if result.empty? return default else return deserialize(result.first[:value]) end end |
#cache_setup(hostname, username, appname, cachename) ⇒ Object
Executed after #initialize 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.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/ramaze/cache/sequel.rb', line 156 def cache_setup(hostname, username, appname, cachename) @namespace = [hostname, username, appname, cachename].compact.join(':') # Create the table if it's not there yet if ![:connection].table_exists?([:table]) [:connection].create_table( [:table]) do primary_key :id String :key , :null => false, :unique => true String :value, :text => true Time :expires end end @dataset = [:connection][[:table].to_sym] end |
#cache_store(key, value, options = {}) ⇒ Object
Sets the given key to the given value.
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/ramaze/cache/sequel.rb', line 269 def cache_store(key, value, = {}) nkey = namespaced(key) # Get the time after which the cache should be expired if [:ttl] ttl = [:ttl] else ttl = Ramaze::Cache::Sequel.[:ttl] end expires = Time.now + ttl if ttl # The row already exists, update it. if @dataset.filter(:key => nkey).count == 1 serialized_value = serialize(value) if serialized_value @dataset.filter(:key => nkey) \ .update(:value => serialize(value), :expires => expires) end # The row doesn't exist yet. else serialized_value = serialize(value) if serialized_value @dataset.insert( :key => nkey, :value => serialize(value), :expires => expires ) end end # Try to deserialize the value. If this fails we'll return a different # value deserialized = deserialize(@dataset.select(:value) \ .filter(:key => nkey) \ .limit(1).first[:value]) if deserialized return deserialized else return value end end |
#deserialize(value) ⇒ Object nil
Deserialize method, adapted from Sequels serialize plugin This method will try to deserialize a value using Marshal.load
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/ramaze/cache/sequel.rb', line 334 def deserialize(value) begin ::Marshal.load(value.unpack('m')[0]) rescue begin ::Marshal.load(value) rescue # Log the error? if [:display_warnings] === true Ramaze::Log::warn("Failed to deserialize #{value.inspect}") end return nil end end end |
#namespaced(key) ⇒ Object
Prefixes the given key with current namespace.
321 322 323 |
# File 'lib/ramaze/cache/sequel.rb', line 321 def namespaced(key) return [@namespace, key].join(':') end |
#serialize(value) ⇒ Object nil
Serialize method, adapted from Sequels serialize plugin This method will try to serialize a value using Marshal.dump
360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/ramaze/cache/sequel.rb', line 360 def serialize(value) begin [::Marshal.dump(value)].pack('m') rescue if [:display_warnings] === true Ramaze::Log::warn("Failed to serialize #{value.inspect}") end return nil end end |