diff --git a/README.md b/README.md index ee7950d..a08c96b 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ Solid Cache supports these options in addition to the standard `ActiveSupport::C - `error_handler` - a Proc to call to handle any `ActiveRecord::ActiveRecordError`s that are raises (default: log errors as warnings) - `expiry_batch_size` - the batch size to use when deleting old records (default: `100`) - `expiry_method` - what expiry method to use `thread` or `job` (default: `thread`) -- `max_age` - the maximum age of entries in the cache (default: `2.weeks.to_i`) +- `max_age` - the maximum age of entries in the cache (default: `2.weeks.to_i`). Can be set to `nil`, but this is not recommended unless using `max_entries` to limit the size of the cache. - `max_entries` - the maximum number of entries allowed in the cache (default: `nil`, meaning no limit) - `cluster` - a Hash of options for the cache database cluster, e.g `{ shards: [:database1, :database2, :database3] }` - `clusters` - and Array of Hashes for multiple cache clusters (ignored if `:cluster` is set) diff --git a/app/models/solid_cache/entry.rb b/app/models/solid_cache/entry.rb index feaa9f4..0d1f4f2 100644 --- a/app/models/solid_cache/entry.rb +++ b/app/models/solid_cache/entry.rb @@ -140,14 +140,25 @@ def to_binary(key) def expiry_candidate_ids(count, max_age:, max_entries:) cache_full = max_entries && max_entries < id_range - min_created_at = max_age.seconds.ago - uncached do - order(:id) - .limit(count * 3) - .pluck(:id, :created_at) - .filter_map { |id, created_at| id if cache_full || created_at < min_created_at } - .sample(count) + if max_age + min_created_at = max_age.seconds.ago + + uncached do + order(:id) + .limit(count * 3) + .pluck(:id, :created_at) + .filter_map { |id, created_at| id if cache_full || created_at < min_created_at } + .sample(count) + end + elsif cache_full + uncached do + order(:id) + .limit(count) + .pluck(:id) + end + else + [] end end end diff --git a/test/unit/expiry_test.rb b/test/unit/expiry_test.rb index f565dd0..eaf59cd 100644 --- a/test/unit/expiry_test.rb +++ b/test/unit/expiry_test.rb @@ -34,7 +34,7 @@ class SolidCache::ExpiryTest < ActiveSupport::TestCase end test "expires records when the cache is full (#{expiry_method})" do - @cache = lookup_store(expiry_batch_size: 3, max_age: 2.weeks, max_entries: 2, expiry_method: expiry_method) + @cache = lookup_store(expiry_batch_size: 3, max_age: nil, max_entries: 2, expiry_method: expiry_method) default_shard_keys = shard_keys(@cache, :default) @cache.write(default_shard_keys[0], 1) @cache.write(default_shard_keys[1], 2)