Memcached store uses one or several Memcached servers for caching. It’s flexible and can be used for fragment caching, action caching, page caching or object caching.
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 82 def clone twin = super twin.memcached = @memcached.clone twin end
Establishes connection to Memcached.
Use :buffer_requests option to use bufferring, :no_block to use non-blocking async I/O. :support_cas to support CAS
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 93 def connect(config = {}) @memcached = ::Memcached.new(@servers, config.only(:buffer_requests, :no_block, :support_cas).merge(:namespace => @namespace)) end
Deletes entry from cached by key.
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 69 def delete(key, parameters = {}) begin @memcached.delete(normalize(key, parameters)) rescue Memcached::NotFound nil end end
Flushes the cache.
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 78 def delete_all @memcached.flush end
returns true/false/nil based on if data identified by the key & parameters is persisted in the store.
With Memcached 1.2 protocol the only way to find if key exists in the cache is to read it. It is very fast and shouldn’t be a concern.
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 58 def exists?(key, parameters = {}) begin @memcached.get(normalize(key, parameters)) && true rescue Memcached::Stored true rescue Memcached::NotFound nil end end
Returns expiration timestamp if :expire_in key is given.
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 105 def expire_time(conditions = {}) if t = conditions[:expire_in] Time.now + t else 0 end end
Fetches cached data by key if it exists. If it does not, uses passed block to get new cached value and writes it using given key.
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 48 def fetch(key, parameters = {}, conditions = {}, &blk) read(key, parameters) || (writable?(key, parameters, conditions) && write(key, value = blk.call, parameters, conditions) && value) end
Returns cache key calculated from base key and SHA2 hex from parameters.
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 99 def normalize(key, parameters = {}) parameters.empty? ? "#{key}" : "#{key}--#{parameters.to_sha2}" end
Reads key from the cache.
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 25 def read(key, parameters = {}) begin @memcached.get(normalize(key, parameters)) rescue Memcached::NotFound, Memcached::Stored nil end end
Memcached store consideres all keys and parameters writable.
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 20 def writable?(key, parameters = {}, conditions = {}) true end
Writes data to the cache using key, parameters and conditions.
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 34 def write(key, data = nil, parameters = {}, conditions = {}) if writable?(key, parameters, conditions) begin @memcached.set(normalize(key, parameters), data, expire_time(conditions)) true rescue nil end end end
Generated with the Darkfish Rdoc Generator 2.