From 1d27b331004d4d7a159fb6b27a9c64f7742df21a Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Sat, 30 Jan 2016 09:01:15 +1100 Subject: [PATCH] FIX: DistributedCache would fail serialization in some cases --- lib/distributed_cache.rb | 7 ++++--- spec/components/distributed_cache_spec.rb | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/distributed_cache.rb b/lib/distributed_cache.rb index 58974b77ad2..31753320364 100644 --- a/lib/distributed_cache.rb +++ b/lib/distributed_cache.rb @@ -3,6 +3,7 @@ # fill it up require 'weakref' +require 'base64' class DistributedCache @subscribers = [] @@ -31,7 +32,7 @@ class DistributedCache hash = current.hash(message.site_id) 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 "clear" then hash.clear end @@ -70,8 +71,8 @@ class DistributedCache def self.set(hash, key, value) # special support for set - marshal = Set === value - value = Marshal.dump(value) if marshal + marshal = (Set === value || Hash === value) + value = Base64.encode64(Marshal.dump(value)) if marshal publish(hash, { op: :set, key: key, value: value, marshalled: marshal }) end diff --git a/spec/components/distributed_cache_spec.rb b/spec/components/distributed_cache_spec.rb index 6f7c82886b9..f1deda95d28 100644 --- a/spec/components/distributed_cache_spec.rb +++ b/spec/components/distributed_cache_spec.rb @@ -15,10 +15,11 @@ describe DistributedCache do c1 = DistributedCache.new("test1") c2 = DistributedCache.new("test1") - set = {a: 1, b: 1} set = Set.new set << 1 set << "b" + set << 92803984 + set << 93739739873973 c1["cats"] = set