Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache refresh rate #580

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

## 1.6.4

- Add `cache_refresh_period` to allow for cache refresh after a period of time. (#579)

## 1.6.3

- Split the `with_deferred_parent_expiration` and `with_deferred_parent_expiration`. (#578)
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ GEM
PLATFORMS
arm64-darwin-22
arm64-darwin-23
arm64-darwin-24
x64-mingw-ucrt
x86_64-linux

DEPENDENCIES
Expand Down
13 changes: 12 additions & 1 deletion lib/identity_cache/cache_fetcher.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# frozen_string_literal: true

require "time"
require "securerandom"

module IdentityCache
mattr_accessor :cache_refresh_period
self.cache_refresh_period = 60

class CacheFetcher
attr_accessor :cache_backend

Expand Down Expand Up @@ -93,7 +97,10 @@ def fetch_without_fill_lock(key)
data = nil
upsert(key) do |value|
value = nil if value == IdentityCache::DELETED || FillLock.cache_value?(value)
unless value.nil?
unless value.nil? ||
(value&.is_a?(Hash) &&
value[:cached_at] &&
((Time.now - value[:cached_at]) / 60).round > IdentityCache.cache_refresh_period)
return value
end

Expand Down Expand Up @@ -316,6 +323,10 @@ def add_multi(keys)
def add(key, value, expiration_options = EMPTY_HASH)
return false unless IdentityCache.should_fill_cache?

if value&.is_a?(Hash)
value.merge(cached_at: Time.now.to_s)
end

@cache_backend.write(key, value, { unless_exist: true, **expiration_options })
end
end
Expand Down
28 changes: 28 additions & 0 deletions test/cache_fetcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,31 @@ def other_cache_fetcher
@other_cache_fetcher ||= IdentityCache::CacheFetcher.new(backend)
end
end

def test_fetch_without_fill_lock_with_stale_cached_data
identity_cache = IdentityCache
identity_cache.cache_refresh_period = 60
stale_time = Time.now - (identity_cache.cache_refresh_period + 1) * 60
backend.write(key, { cached_at: stale_time, data: :old_data })
assert_memcache_operations(2) do
assert_equal(:new_data, cache_fetcher.fetch_without_fill_lock(key) { :new_data })
end
end

def test_fetch_without_fill_lock_with_fresh_cached_data
identity_cache = IdentityCache
identity_cache.cache_refresh_period = 60
fresh_time = Time.now - (identity_cache.cache_refresh_period - 1) * 60
cached_data = { cached_at: fresh_time, data: :fresh_data }
backend.write(key, cached_data)
assert_memcache_operations(1) do
assert_equal(cached_data, cache_fetcher.fetch_without_fill_lock(key) { :new_data })
end
end

def test_fetch_without_fill_lock_with_non_hash_value
backend.write(key, "string_value")
assert_memcache_operations(1) do
assert_equal("string_value", cache_fetcher.fetch_without_fill_lock(key) { :new_data })
end
end
1 change: 1 addition & 0 deletions test/helpers/database_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def create_tables
"host" => ENV["MYSQL_HOST"] || "127.0.0.1",
"username" => "root",
"port" => ENV["MYSQL_PORT"] ? Integer(ENV["MYSQL_PORT"]) : 3306,
"password" => ENV["MYSQL_PASSWORD"] || "my-secret-pw",
},
"postgresql" => {
"adapter" => "postgresql",
Expand Down
Loading