Skip to content

Commit

Permalink
fix for Rails 7.1 caching
Browse files Browse the repository at this point in the history
  • Loading branch information
oldmoe committed Mar 1, 2024
1 parent 100abbe commit 951fdf5
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/active_support/cache/litecache.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "delegate"
require "active_support"
require "active_support/core_ext/enumerable"
require "active_support/core_ext/array/extract_options"
require "active_support/core_ext/numeric/time"
Expand Down Expand Up @@ -73,6 +74,14 @@ def stats

private

def serialize_entrys(entry, **options)
Marshal.dump(entry)
end

def deserialize_entrys(entry)
Marshal.load(entry.to_s)
end

# Read an entry from the cache.
def read_entry(key, **options)
deserialize_entry(@cache.get(key))
Expand All @@ -94,15 +103,15 @@ def write_entry(key, entry, **options)
def write_multi_entries(entries, **options)
return if entries.empty?
entries.each_pair {|k,v| entries[k] = serialize_entry(v, **options)}
expires_in = options[:expires_in].to_i
expires_in = options[:expires_in].to_i if options[:expires_in]
if options[:race_condition_ttl] && expires_in > 0 && !options[:raw]
expires_in += 5.minutes
end
@cache.set_multi(entries, expires_in)
end

def write_serialized_entry(key, payload, **options)
expires_in = options[:expires_in].to_i
expires_in = options[:expires_in].to_i if options[:expires_in]
if options[:race_condition_ttl] && expires_in > 0 && !options[:raw]
expires_in += 5.minutes
end
Expand Down
4 changes: 2 additions & 2 deletions lib/litestack/litecache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ def delete(key)
end

# increment an integer value by amount, optionally add an expiry value (in seconds)
def increment(key, amount, expires_in = nil)
def increment(key, amount=1, expires_in = nil)
expires_in ||= @expires_in
@conn.acquire { |cache| cache.stmts[:incrementer].execute!(key.to_s, amount, expires_in) }
end

# decrement an integer value by amount, optionally add an expiry value (in seconds)
def decrement(key, amount, expires_in = nil)
def decrement(key, amount=1, expires_in = nil)
increment(key, -amount, expires_in)
end

Expand Down
64 changes: 64 additions & 0 deletions test/test_cache.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require "minitest/autorun"
require_relative "../lib/litestack/litecache"

class TestCache < Minitest::Test
def setup
@cache = Litecache.new({path: ":memory:", sleep_interval: 1})
@cache.clear
end

def test_caceh_set
@cache.set("key", "value")
assert_equal "value", @cache.get("key")
@cache.set("key", "new_value")
assert_equal "new_value", @cache.get("key")
end

def test_caceh_set_unless_exists
@cache.set("key", "value")
assert_equal "value", @cache.get("key")
@cache.set_unless_exists("key", "new_value")
assert_equal "value", @cache.get("key")
end

def test_cache_set_multi
data = {k1: "v1", k2: "v2", k3: "v3"}
@cache.set_multi(data)
data.keys.each do |key|
assert_equal data[key], @cache.get(key)
end
end

def test_cache_get_multi
data = {k1: "v1", k2: "v2", k3: "v3"}
data.each_pair{|k, v| @cache.set(k, v) }
results = @cache.get_multi(*data.keys)
assert_equal data, results
end

def test_cache_expiry
@cache.set("key", "value", 1)
assert_equal "value", @cache.get("key")
sleep 1.1
assert_nil @cache.get("key")
end

def test_increment_decrement
@cache.increment("key")
assert_equal 1, @cache.get("key")
@cache.increment("key", 5)
assert_equal 6, @cache.get("key")
@cache.decrement("key", 4)
assert_equal 2, @cache.get("key")
end

def test_increment_decrement_expiry
@cache.increment("key", 2, 1)
assert_equal 2, @cache.get("key")
sleep 1.1
@cache.increment("key", 5)
assert_equal 5, @cache.get("key")
end

end

71 changes: 71 additions & 0 deletions test/test_cache_rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require "minitest/autorun"
require_relative "../lib/active_support/cache/litecache"

class TestCache < Minitest::Test
def setup
@cache = ActiveSupport::Cache::Litecache.new({path: ":memory:", sleep_interval: 1})
@cache.clear
end

def test_caceh_write
@cache.write("key", "value")
assert_equal "value", @cache.read("key")
@cache.write("key", "new_value")
assert_equal "new_value", @cache.read("key")
end

def test_cache_fetch
result = @cache.fetch("key") {"value"}
assert_equal "value", result
result = @cache.fetch("key") {"new_value"}
assert_equal "value", result
end

def test_cache_write_multi
data = {k1: "v1", k2: "v2", k3: "v3"}
@cache.write_multi(data)
data.keys.each do |key|
assert_equal data[key], @cache.read(key)
end
end

def test_cache_read_multi
data = {k1: "v1", k2: "v2", k3: "v3"}
data.each_pair{|k, v| @cache.write(k, v) }
results = @cache.read_multi(*data.keys)
assert_equal data, results
end

def test_cache_write_read_multi
data = {k1: "v1", k2: "v2", k3: "v3"}
@cache.write_multi(data)
results = @cache.read_multi(*data.keys)
assert_equal data, results
end

def test_cache_expiry
@cache.write("key", "value", expires_at: 1.second.from_now)
assert_equal "value", @cache.read("key")
sleep 1.1
assert_nil @cache.read("key")
end

def test_increment_decrement
@cache.increment("key")
assert_equal 1, @cache.read("key")
@cache.increment("key", 5)
assert_equal 6, @cache.read("key")
@cache.decrement("key", 4)
assert_equal 2, @cache.read("key")
end

def test_increment_decrement_expiry
@cache.increment("key", 2, expires_at: 1.second.from_now)
assert_equal 2, @cache.read("key")
sleep 1.1
@cache.increment("key", 5)
assert_equal 5, @cache.read("key")
end

end

0 comments on commit 951fdf5

Please sign in to comment.