1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
class Redis
class Store < self
module Serialization
def set(key, value, options = nil)
_marshal(value, options) { |v| super encode(key), encode(v), options }
end
def setnx(key, value, options = nil)
_marshal(value, options) { |v| super encode(key), encode(v), options }
end
def setex(key, expiry, value, options = nil)
_marshal(value, options) { |v| super encode(key), expiry, encode(v), options }
end
def get(key, options = nil)
_unmarshal super(key), options
end
def mget(*keys, &blk)
options = keys.pop if keys.last.is_a?(Hash)
super(*keys) do |reply|
reply.map! { |value| _unmarshal value, options }
blk ? blk.call(reply) : reply
end
end
def mset(*args)
options = args.pop if args.length.odd?
updates = []
args.each_slice(2) do |(key, value)|
updates << encode(key)
_marshal(value, options) { |v| updates << encode(v) }
end
super(*updates)
end
private
def _marshal(val, options)
yield marshal?(options) ? @serializer.dump(val) : val
end
def _unmarshal(val, options)
unmarshal?(val, options) ? @serializer.load(val) : val
end
def marshal?(options)
!(options && options[:raw])
end
def unmarshal?(result, options)
result && result.size > 0 && marshal?(options)
end
if defined?(Encoding)
def encode(string)
key = string.to_s.dup
key.force_encoding(Encoding::BINARY)
end
else
def encode(string)
string
end
end
end
end
end
|