FEATURE: allow distributed cache to handle Set as value

This commit is contained in:
Sam Saffron 2015-06-10 06:08:06 +10:00
parent e3fa27a01c
commit 49ca248186
2 changed files with 33 additions and 2 deletions

View File

@ -31,7 +31,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["value"] when "set" then hash[payload["key"]] = payload["marshalled"] ? Marshal.load(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
@ -69,7 +69,10 @@ class DistributedCache
end end
def self.set(hash, key, value) 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 end
def self.delete(hash, key) def self.delete(hash, key)

View File

@ -11,6 +11,34 @@ describe DistributedCache do
DistributedCache.new("test") DistributedCache.new("test")
end 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 it 'does not leak state across caches' do
c2 = DistributedCache.new("test1") c2 = DistributedCache.new("test1")
c3 = DistributedCache.new("test1") c3 = DistributedCache.new("test1")