From 49ca2481866906b38ce093a436b8085669f85855 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Wed, 10 Jun 2015 06:08:06 +1000 Subject: [PATCH] FEATURE: allow distributed cache to handle Set as value --- lib/distributed_cache.rb | 7 ++++-- spec/components/distributed_cache_spec.rb | 28 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/distributed_cache.rb b/lib/distributed_cache.rb index 777c9b780ea..58974b77ad2 100644 --- a/lib/distributed_cache.rb +++ b/lib/distributed_cache.rb @@ -31,7 +31,7 @@ class DistributedCache hash = current.hash(message.site_id) case payload["op"] - when "set" then hash[payload["key"]] = payload["value"] + when "set" then hash[payload["key"]] = payload["marshalled"] ? Marshal.load(payload["value"]) : payload["value"] when "delete" then hash.delete(payload["key"]) when "clear" then hash.clear end @@ -69,7 +69,10 @@ class DistributedCache end def self.set(hash, key, value) - publish(hash, { op: :set, key: key, value: value }) + # special support for set + marshal = Set === value + value = Marshal.dump(value) if marshal + publish(hash, { op: :set, key: key, value: value, marshalled: marshal }) end def self.delete(hash, key) diff --git a/spec/components/distributed_cache_spec.rb b/spec/components/distributed_cache_spec.rb index 68a0c259a4f..a26f6fbbd32 100644 --- a/spec/components/distributed_cache_spec.rb +++ b/spec/components/distributed_cache_spec.rb @@ -11,6 +11,34 @@ describe DistributedCache do DistributedCache.new("test") end + it 'allows us to store Set' do + c1 = DistributedCache.new("test1") + c2 = DistributedCache.new("test1") + + set = {a: 1, b: 1} + set = Set.new + set << 1 + set << "b" + + c1["cats"] = set + + wait_for do + c2["cats"] == set + end + + expect(c2["cats"]).to eq(set) + + set << 5 + + c2["cats"] == set + + wait_for do + c1["cats"] == set + end + + expect(c1["cats"]).to eq(set) + end + it 'does not leak state across caches' do c2 = DistributedCache.new("test1") c3 = DistributedCache.new("test1")