Use ActiveSupport::Cache::RedisCacheStore As RedisRailsCache (#4676) [deploy]
This commit is contained in:
parent
caf54a70d0
commit
00e3ee811b
4 changed files with 13 additions and 121 deletions
|
|
@ -1,5 +0,0 @@
|
|||
# DEV uses the RedisCloud Heroku Add-On which comes with the predefined env variable REDISCLOUD_URL
|
||||
redis_url = ENV["REDISCLOUD_URL"]
|
||||
redis_url ||= ApplicationConfig["REDIS_URL"]
|
||||
|
||||
RedisClient = Redis.new(url: redis_url)
|
||||
13
config/initializers/redis_rails.rb
Normal file
13
config/initializers/redis_rails.rb
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
# DEV uses the RedisCloud Heroku Add-On which comes with the predefined env variable REDISCLOUD_URL
|
||||
redis_url = ENV["REDISCLOUD_URL"]
|
||||
redis_url ||= ApplicationConfig["REDIS_URL"]
|
||||
DEFAULT_EXPIRATION = 24.hours.to_i.freeze
|
||||
|
||||
if Rails.env.test?
|
||||
RedisRailsCache = ActiveSupport::Cache::NullStore.new
|
||||
# Uncomment these lines to use MemoryStory in development
|
||||
# elsif Rails.env.development?
|
||||
# RedisRailsCache = ActiveSupport::Cache::MemoryStore.new
|
||||
else
|
||||
RedisRailsCache = ActiveSupport::Cache::RedisCacheStore.new(url: redis_url, expires_in: DEFAULT_EXPIRATION)
|
||||
end
|
||||
|
|
@ -1,40 +0,0 @@
|
|||
# Temporary and will be removed after all cache keys have been moved over to Redis
|
||||
class RedisRailsCache
|
||||
DEFAULT_EXPIRATION = 24.hours.to_i.freeze
|
||||
|
||||
class << self
|
||||
def fetch(key_name, opts = {})
|
||||
# Default expire all keys after 24 hours if expiration is not given
|
||||
expires_in = opts[:expires_in] || DEFAULT_EXPIRATION
|
||||
|
||||
if block_given?
|
||||
entry = client.get(key_name)
|
||||
return entry if entry.present?
|
||||
|
||||
save_block_result_to_cache(key_name, expires_in) { |name| yield name }
|
||||
else
|
||||
client.get(key_name)
|
||||
end
|
||||
end
|
||||
|
||||
def read(key_name)
|
||||
client.get(key_name)
|
||||
end
|
||||
|
||||
def write(key_name, value, opts = {})
|
||||
client.set(key_name, value, ex: opts[:expires_in] || DEFAULT_EXPIRATION)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def save_block_result_to_cache(key_name, expires_in)
|
||||
result = yield
|
||||
client.set(key_name, result, ex: expires_in)
|
||||
result
|
||||
end
|
||||
|
||||
def client
|
||||
RedisClient
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,76 +0,0 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe RedisRailsCache do
|
||||
let(:redis_client) { class_double("RedisClient") }
|
||||
|
||||
before do
|
||||
allow(described_class).to receive(:client) { redis_client }
|
||||
allow(redis_client).to receive(:set).and_return("OK")
|
||||
allow(redis_client).to receive(:get)
|
||||
end
|
||||
|
||||
describe "#fetch" do
|
||||
context "when block is present" do
|
||||
it "returns key value if present" do
|
||||
allow(redis_client).to receive(:get).with("five").and_return(5)
|
||||
result = described_class.fetch("five") do
|
||||
5
|
||||
end
|
||||
expect(redis_client).to have_received(:get).with("five")
|
||||
expect(result).to eq(5)
|
||||
end
|
||||
|
||||
it "when key is missing sets key value to result of the block and expiration" do
|
||||
result = described_class.fetch("five") do
|
||||
5
|
||||
end
|
||||
expect(redis_client).to have_received(:get).with("five")
|
||||
expect(redis_client).to have_received(:set).with(
|
||||
"five", 5, ex: described_class::DEFAULT_EXPIRATION
|
||||
)
|
||||
expect(result).to eq(5)
|
||||
end
|
||||
|
||||
it "when key is missing sets key value and custom expiration" do
|
||||
result = described_class.fetch("five", expires_in: 100) do
|
||||
5
|
||||
end
|
||||
expect(redis_client).to have_received(:get).with("five")
|
||||
expect(redis_client).to have_received(:set).with("five", 5, ex: 100)
|
||||
expect(result).to eq(5)
|
||||
end
|
||||
end
|
||||
|
||||
context "without block present" do
|
||||
it "returns the value of the key from the cache" do
|
||||
allow(redis_client).to receive(:get).with("five").and_return(5)
|
||||
result = described_class.fetch("five")
|
||||
expect(redis_client).to have_received(:get).with("five")
|
||||
expect(result).to eq(5)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#read" do
|
||||
it "gets the key from RedisClient" do
|
||||
described_class.read("foo")
|
||||
expect(redis_client).to have_received(:get).with("foo")
|
||||
end
|
||||
end
|
||||
|
||||
describe "#write" do
|
||||
it "sets the key in RedisClient with default expiration" do
|
||||
described_class.write("foo", 2)
|
||||
expect(redis_client).to have_received(:set).with(
|
||||
"foo", 2, ex: described_class::DEFAULT_EXPIRATION
|
||||
)
|
||||
end
|
||||
|
||||
it "sets the key in RedisClient with custom expiration" do
|
||||
described_class.write("foo", 2, expires_in: 100)
|
||||
expect(redis_client).to have_received(:set).with(
|
||||
"foo", 2, ex: 100
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue