Created
January 5, 2009 03:10
-
-
Save fightinjoe/43245 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Merb::Cache | |
# The installed cache stores don't serialize the data before | |
# caching it. The MarshalStore serializes the data using Marshal.dump | |
# and Marshal.load. | |
# | |
# NOTE: to be useful with DataMapper, make sure to use the edge version | |
# of dm-core 0.9.8 or later, otherwise you'll be left with an error that | |
# looks like "no marshal_dump is defined for class Thread". | |
# (thanks dkubb: http://github.com/sam/dm-core/commit/83ea752d5604726b00ff11c55c0fbd2c39607c16) | |
class MarshalStore < AbstractStrategyStore | |
def writable?(key, parameters = {}, conditions = {}) | |
true | |
end | |
def read(key, parameters = {}) | |
deserialize(@stores.capture_first {|c| c.read(key, parameters)}) | |
end | |
def write(key, data = nil, parameters = {}, conditions = {}) | |
if writable?(key, parameters, conditions) | |
@stores.capture_first {|c| c.write(key, serialize(data), parameters, conditions)} | |
end | |
end | |
def write_all(key, data = nil, parameters = {}, conditions = {}) | |
if writable?(key, parameters, conditions) | |
@stores.map {|c| c.write_all(key, serialize(data), parameters, conditions)}.all? | |
end | |
end | |
def fetch(key, parameters = {}, conditions = {}, &blk) | |
wrapper_blk = lambda { serialize(blk.call) } | |
deserialize(read(key, parameters) || @stores.capture_first {|s| s.fetch(key, parameters, conditions, &wrapper_blk)}) | |
end | |
def exists?(key, parameters = {}) | |
@stores.capture_first {|c| c.exists?(key, parameters)} | |
end | |
def delete(key, parameters = {}) | |
@stores.map {|c| c.delete(key, parameters)}.any? | |
end | |
def delete_all! | |
@stores.map {|c| c.delete_all! }.all? | |
end | |
def serialize(data) | |
return if data.nil? | |
Marshal.dump(data) | |
end | |
def deserialize(data) | |
return if data.nil? | |
Marshal.load(data) | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment