-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from mikeheft/7-implement-redis
7 implement redis
- Loading branch information
Showing
10 changed files
with
109 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require './lib/cache/store' | ||
redis_url = ENV["REDIS_URL"] || "redis://localhost:6379/1" | ||
::Cache::Store.cache(Redis.new(url: redis_url)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cache | ||
class Store | ||
def self.cache(store) | ||
@cache ||= store | ||
end | ||
|
||
def self.set(key, value, expires_in: 1.hour) | ||
@cache.set(key, value) | ||
@cache.expire(key, expires_in) | ||
end | ||
|
||
def self.get(key) | ||
@cache.get(key) | ||
end | ||
|
||
def self.del(key) | ||
@cache.del(key) | ||
end | ||
|
||
def self.exists?(key) | ||
@cache.exists(key) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
require Rails.root.join("lib/cache/store") | ||
|
||
RSpec.describe Cache::Store do | ||
describe ".set and .get" do | ||
it "stores and retrieves data from Redis cache" do | ||
key = "test_key" | ||
value = "test_value" | ||
|
||
# Set value in cache | ||
Cache::Store.set(key, value) | ||
|
||
# Get value from cache | ||
cached_value = Cache::Store.get(key) | ||
|
||
expect(cached_value).to eq(value) | ||
end | ||
end | ||
|
||
describe ".del" do | ||
it "deletes data from Redis cache" do | ||
key = "test_key" | ||
value = "test_value" | ||
|
||
# Set value in cache | ||
Cache::Store.set(key, value) | ||
|
||
# Delete value from cache | ||
Cache::Store.del(key) | ||
|
||
# Verify value is deleted | ||
cached_value = Cache::Store.get(key) | ||
|
||
expect(cached_value).to be_nil | ||
end | ||
end | ||
end |