FIX: DistributedCache would fail serialization in some cases

This commit is contained in:
Sam Saffron 2016-01-30 09:01:15 +11:00
parent 96380bfd38
commit 1d27b33100
2 changed files with 6 additions and 4 deletions

View File

@ -3,6 +3,7 @@
# fill it up # fill it up
require 'weakref' require 'weakref'
require 'base64'
class DistributedCache class DistributedCache
@subscribers = [] @subscribers = []
@ -31,7 +32,7 @@ class DistributedCache
hash = current.hash(message.site_id) hash = current.hash(message.site_id)
case payload["op"] case payload["op"]
when "set" then hash[payload["key"]] = payload["marshalled"] ? Marshal.load(payload["value"]) : payload["value"] when "set" then hash[payload["key"]] = payload["marshalled"] ? Marshal.load(Base64.decode64(payload["value"])) : payload["value"]
when "delete" then hash.delete(payload["key"]) when "delete" then hash.delete(payload["key"])
when "clear" then hash.clear when "clear" then hash.clear
end end
@ -70,8 +71,8 @@ class DistributedCache
def self.set(hash, key, value) def self.set(hash, key, value)
# special support for set # special support for set
marshal = Set === value marshal = (Set === value || Hash === value)
value = Marshal.dump(value) if marshal value = Base64.encode64(Marshal.dump(value)) if marshal
publish(hash, { op: :set, key: key, value: value, marshalled: marshal }) publish(hash, { op: :set, key: key, value: value, marshalled: marshal })
end end

View File

@ -15,10 +15,11 @@ describe DistributedCache do
c1 = DistributedCache.new("test1") c1 = DistributedCache.new("test1")
c2 = DistributedCache.new("test1") c2 = DistributedCache.new("test1")
set = {a: 1, b: 1}
set = Set.new set = Set.new
set << 1 set << 1
set << "b" set << "b"
set << 92803984
set << 93739739873973
c1["cats"] = set c1["cats"] = set