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 82: def clone 83: twin = super 84: twin.memcached = @memcached.clone 85: twin 86: 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 93: def connect(config = {}) 94: @memcached = ::Memcached.new(@servers, config.only(:buffer_requests, :no_block, :support_cas).merge(:namespace => @namespace)) 95: end
Deletes entry from cached by key.
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 69 69: def delete(key, parameters = {}) 70: begin 71: @memcached.delete(normalize(key, parameters)) 72: rescue Memcached::NotFound 73: nil 74: end 75: end
Flushes the cache.
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 78 78: def delete_all 79: @memcached.flush 80: 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 58: def exists?(key, parameters = {}) 59: begin 60: @memcached.get(normalize(key, parameters)) && true 61: rescue Memcached::Stored 62: true 63: rescue Memcached::NotFound 64: nil 65: end 66: end
Returns expiration timestamp if :expire_in key is given.
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 105 105: def expire_time(conditions = {}) 106: if t = conditions[:expire_in] 107: Time.now + t 108: else 109: 0 110: end 111: 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 48: def fetch(key, parameters = {}, conditions = {}, &blk) 49: read(key, parameters) || (writable?(key, parameters, conditions) && write(key, value = blk.call, parameters, conditions) && value) 50: end
Returns cache key calculated from base key and SHA2 hex from parameters.
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 99 99: def normalize(key, parameters = {}) 100: parameters.empty? ? "#{key}" : "#{key}--#{parameters.to_sha2}" 101: end
Reads key from the cache.
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 25 25: def read(key, parameters = {}) 26: begin 27: @memcached.get(normalize(key, parameters)) 28: rescue Memcached::NotFound, Memcached::Stored 29: nil 30: end 31: end
Memcached store consideres all keys and parameters writable.
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 20 20: def writable?(key, parameters = {}, conditions = {}) 21: true 22: end
Writes data to the cache using key, parameters and conditions.
# File lib/merb-cache/stores/fundamental/memcached_store.rb, line 34 34: def write(key, data = nil, parameters = {}, conditions = {}) 35: if writable?(key, parameters, conditions) 36: begin 37: @memcached.set(normalize(key, parameters), data, expire_time(conditions)) 38: true 39: rescue 40: nil 41: end 42: end 43: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.