2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-05-13 04:04:03 -04:00
|
|
|
require 'cache'
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe Cache do
|
2013-05-13 04:04:03 -04:00
|
|
|
|
|
|
|
let :cache do
|
|
|
|
Cache.new
|
|
|
|
end
|
|
|
|
|
2019-11-27 00:11:49 -05:00
|
|
|
it "supports exist?" do
|
|
|
|
cache.write("testing", 1.1)
|
|
|
|
expect(cache.exist?("testing")).to eq(true)
|
|
|
|
expect(cache.exist?(SecureRandom.hex)).to eq(false)
|
|
|
|
end
|
|
|
|
|
2019-11-26 20:35:14 -05:00
|
|
|
it "supports float" do
|
|
|
|
cache.write("float", 1.1)
|
|
|
|
expect(cache.read("float")).to eq(1.1)
|
|
|
|
end
|
|
|
|
|
2014-01-07 01:36:47 -05:00
|
|
|
it "supports fixnum" do
|
|
|
|
cache.write("num", 1)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(cache.read("num")).to eq(1)
|
2014-01-07 01:36:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "supports hash" do
|
|
|
|
hash = { a: 1, b: [1, 2, 3] }
|
|
|
|
cache.write("hash", hash)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(cache.read("hash")).to eq(hash)
|
2014-01-07 01:36:47 -05:00
|
|
|
end
|
|
|
|
|
2014-01-06 00:50:04 -05:00
|
|
|
it "can be cleared" do
|
2019-12-03 04:05:53 -05:00
|
|
|
Discourse.redis.set("boo", "boo")
|
2014-01-06 00:50:04 -05:00
|
|
|
cache.write("hello0", "world")
|
|
|
|
cache.write("hello1", "world")
|
|
|
|
cache.clear
|
2013-05-13 04:04:03 -04:00
|
|
|
|
2019-12-03 04:05:53 -05:00
|
|
|
expect(Discourse.redis.get("boo")).to eq("boo")
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(cache.read("hello0")).to eq(nil)
|
2014-01-06 00:50:04 -05:00
|
|
|
end
|
|
|
|
|
2013-05-13 04:04:03 -04:00
|
|
|
it "can delete correctly" do
|
2019-11-27 00:11:49 -05:00
|
|
|
cache.delete("key")
|
|
|
|
|
2014-01-06 00:50:04 -05:00
|
|
|
cache.fetch("key", expires_in: 1.minute) do
|
2013-05-13 04:04:03 -04:00
|
|
|
"test"
|
|
|
|
end
|
|
|
|
|
2019-11-27 00:11:49 -05:00
|
|
|
expect(cache.fetch("key")).to eq("test")
|
|
|
|
|
2013-05-13 04:04:03 -04:00
|
|
|
cache.delete("key")
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(cache.fetch("key")).to eq(nil)
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
|
|
|
|
2014-01-07 01:36:47 -05:00
|
|
|
it "calls setex in redis" do
|
|
|
|
cache.delete("key")
|
2015-02-19 00:58:05 -05:00
|
|
|
cache.delete("bla")
|
2014-01-07 01:36:47 -05:00
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
key = cache.normalize_key("key")
|
2013-05-13 04:04:03 -04:00
|
|
|
|
2014-01-07 01:36:47 -05:00
|
|
|
cache.fetch("key", expires_in: 1.minute) do
|
2013-05-13 04:04:03 -04:00
|
|
|
"bob"
|
|
|
|
end
|
2015-02-19 00:58:05 -05:00
|
|
|
|
2019-12-03 04:05:53 -05:00
|
|
|
expect(Discourse.redis.ttl(key)).to be_within(2.seconds).of(1.minute)
|
2015-02-19 00:58:05 -05:00
|
|
|
|
|
|
|
# we always expire withing a day
|
|
|
|
cache.fetch("bla") { "hi" }
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
key = cache.normalize_key("bla")
|
2019-12-03 04:05:53 -05:00
|
|
|
expect(Discourse.redis.ttl(key)).to be_within(2.seconds).of(1.day)
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can store and fetch correctly" do
|
2014-01-07 01:36:47 -05:00
|
|
|
cache.delete "key"
|
2013-05-13 04:04:03 -04:00
|
|
|
|
|
|
|
r = cache.fetch "key" do
|
|
|
|
"bob"
|
|
|
|
end
|
2019-11-27 00:11:49 -05:00
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(r).to eq("bob")
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can fetch existing correctly" do
|
2014-01-07 01:36:47 -05:00
|
|
|
cache.write "key", "bill"
|
2013-05-13 04:04:03 -04:00
|
|
|
|
|
|
|
r = cache.fetch "key" do
|
|
|
|
"bob"
|
|
|
|
end
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(r).to eq("bill")
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
2018-05-03 09:41:41 -04:00
|
|
|
|
|
|
|
it "can fetch keys with pattern" do
|
|
|
|
cache.write "users:admins", "jeff"
|
|
|
|
cache.write "users:moderators", "bob"
|
|
|
|
|
|
|
|
expect(cache.keys("users:*").count).to eq(2)
|
|
|
|
end
|
2021-01-04 04:34:44 -05:00
|
|
|
|
|
|
|
it "can fetch namespace" do
|
|
|
|
expect(cache.namespace).to eq("_CACHE")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the defined expires_in" do
|
|
|
|
cache.write "foo:bar", "baz", expires_in: 3.minutes
|
|
|
|
|
|
|
|
expect(cache.redis.ttl("#{cache.namespace}:foo:bar")).to eq(180)
|
|
|
|
end
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|