Skip to content
Merged
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
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,25 @@ The default cache options used when interacting with the cache can be specified
ActiveRemote::Cached.default_options(:expires_in => 1.hour)
```

In Rails apps, the :race_condition_ttl option defaults to 5 seconds.
In Rails apps, the railtie sets `:expires_in` to 5 minutes and `:race_condition_ttl` to 5 seconds. Change them with `config.active_remote_cached.expires_in` and `config.active_remote_cached.race_condition_ttl`.

`default_options` merges the given options into the current options. An initializer that adds one option keeps the railtie's TTL:

```ruby
# config/initializers/active_remote_cached.rb
ActiveRemote::Cached.default_options(:active_remote_cached_replace_characters => true)
ActiveRemote::Cached.default_options
# => { :expires_in => 5.minutes, :race_condition_ttl => 5.seconds, :active_remote_cached_replace_characters => true }
```

To replace all the options, use `default_options_overwrite`. Pass an empty hash to clear them:

```ruby
ActiveRemote::Cached.default_options_overwrite(:expires_in => 1.hour)
ActiveRemote::Cached.default_options_overwrite({})
```

Without `:expires_in`, a cached finder writes an entry that never expires.

#### Local overrides

Expand Down Expand Up @@ -149,6 +167,30 @@ CI runs this matrix on Ruby 3.1, Ruby 3.4, JRuby 9.4, and JRuby 10.0.
`active_remote` 8.0 requires Ruby 3.2 or later. CI does not run that
version on Ruby 3.1 or JRuby 9.4.

## Upgrading to 1.3.0

### default_options merges

Before 1.3.0, each call to `default_options` replaced the options. In a
Rails app, the railtie sets `:expires_in` and `:race_condition_ttl` before the
app initializers run. An initializer that called `default_options` with other
options removed the TTL, and every cached finder call with no `:expires_in`
wrote an entry that never expired:

```ruby
# railtie: { :expires_in => 5.minutes, :race_condition_ttl => 5.seconds }
ActiveRemote::Cached.default_options(:active_remote_cached_replace_characters => true)
# before: { :active_remote_cached_replace_characters => true }
# now: { :expires_in => 5.minutes, :race_condition_ttl => 5.seconds, :active_remote_cached_replace_characters => true }
```

After the upgrade, an app like this gets a TTL again. Its finders call the
remote service more often, because entries now expire.

`default_options({})` no longer clears the options. Use
`default_options_overwrite({})`. Use `default_options_overwrite` to keep the
old replace behavior.

## Upgrading to 1.2.0

### Every cache key changes
Expand Down
11 changes: 10 additions & 1 deletion lib/active_remote/cached.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,21 @@ def self.cache(cache_provider = nil)
@cache_provider
end

# Merges the given options into the current options, and returns the
# result. A later call adds to the options, so an app initializer keeps the
# :expires_in and :race_condition_ttl that the Rails railtie set before it.
def self.default_options(options = nil)
@default_options = options if options
@default_options = default_options.merge(options) if options

@default_options || {}
end

# Replaces the current options with the given options, and returns them.
# Pass an empty hash to clear the options.
def self.default_options_overwrite(options)
@default_options = options.dup
end

module ClassMethods
def cached_methods
@cached_methods ||= []
Expand Down
2 changes: 1 addition & 1 deletion lib/active_remote/cached/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module ActiveRemote
module Cached
VERSION = '1.2.0'
VERSION = '1.3.0'
end
end
2 changes: 1 addition & 1 deletion spec/active_remote/cached_delete_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def self.search(*)
end

after do
::ActiveRemote::Cached.default_options({})
::ActiveRemote::Cached.default_options_overwrite({})
end

it 'deletes the find cache key' do
Expand Down
4 changes: 2 additions & 2 deletions spec/active_remote/cached_exist_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def self.search(*)
end

after do
::ActiveRemote::Cached.default_options({})
::ActiveRemote::Cached.default_options_overwrite({})
end

it 'returns false before the find cache key is written' do
Expand Down Expand Up @@ -186,7 +186,7 @@ def self.search(*)
end

after do
::ActiveRemote::Cached.default_options({})
::ActiveRemote::Cached.default_options_overwrite({})
end

it 'returns false before the search cache key is written' do
Expand Down
4 changes: 2 additions & 2 deletions spec/active_remote/cached_find_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def self.search
end

after do
::ActiveRemote::Cached.default_options({})
::ActiveRemote::Cached.default_options_overwrite({})
end

it 'executes find_by_guid when cached_find with guid called' do
Expand Down Expand Up @@ -117,7 +117,7 @@ def self.search
end

after do
::ActiveRemote::Cached.default_options({})
::ActiveRemote::Cached.default_options_overwrite({})
end

it 'overrides the default options with cached_finder options for the fetch call' do
Expand Down
8 changes: 4 additions & 4 deletions spec/active_remote/cached_search_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def self.search
end

after do
::ActiveRemote::Cached.default_options({})
::ActiveRemote::Cached.default_options_overwrite({})
end

it 'executes the search block when a block is passed' do
Expand Down Expand Up @@ -203,7 +203,7 @@ def self.search
end

after do
::ActiveRemote::Cached.default_options({})
::ActiveRemote::Cached.default_options_overwrite({})
end

it 'overrides the default options with cached_finder options for the fetch call' do
Expand Down Expand Up @@ -232,7 +232,7 @@ def self.search
end

after do
::ActiveRemote::Cached.default_options({})
::ActiveRemote::Cached.default_options_overwrite({})
end

it 'and_return results when present' do
Expand Down Expand Up @@ -307,7 +307,7 @@ def self.search
end

after do
::ActiveRemote::Cached.default_options({})
::ActiveRemote::Cached.default_options_overwrite({})
end

it 'calls the underlying method with params in correct order' do
Expand Down
65 changes: 62 additions & 3 deletions spec/active_remote/cached_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class DurationChildClass < DurationOptionClass; end
end

after do
::ActiveRemote::Cached.default_options({})
::ActiveRemote::Cached.default_options_overwrite({})
end

describe '.cache' do
Expand All @@ -91,17 +91,76 @@ class DurationChildClass < DurationOptionClass; end
expect(::ActiveRemote::Cached.default_options).to eq(:expires_in => 100)
end

# Passing nil reads the options. Callers must pass an empty hash to clear.
it 'keeps the current options when nil is given' do
::ActiveRemote::Cached.default_options(:expires_in => 100)

expect(::ActiveRemote::Cached.default_options(nil)).to eq(:expires_in => 100)
end

it 'keeps the current options when an empty hash is given' do
::ActiveRemote::Cached.default_options(:expires_in => 100)

expect(::ActiveRemote::Cached.default_options({})).to eq(:expires_in => 100)
end

# The Rails railtie sets :expires_in and :race_condition_ttl. An app
# initializer that sets one more option must not remove them.
it 'merges the given options into the current options' do
::ActiveRemote::Cached.default_options(:expires_in => 100, :race_condition_ttl => 5)
::ActiveRemote::Cached.default_options(:active_remote_cached_replace_characters => true)

expect(::ActiveRemote::Cached.default_options).to eq(
:expires_in => 100,
:race_condition_ttl => 5,
:active_remote_cached_replace_characters => true
)
end

it 'lets the given options win over the current options' do
::ActiveRemote::Cached.default_options(:expires_in => 100)
::ActiveRemote::Cached.default_options(:expires_in => 200)

expect(::ActiveRemote::Cached.default_options).to eq(:expires_in => 200)
end

it 'does not change a hash that the caller passed' do
first = { :expires_in => 100 }
::ActiveRemote::Cached.default_options(first)
::ActiveRemote::Cached.default_options(:race_condition_ttl => 5)

expect(first).to eq(:expires_in => 100)
end

it 'gives each finder call the merged options' do
::ActiveRemote::Cached.default_options(:expires_in => 100)
::ActiveRemote::Cached.default_options(:active_remote_cached_replace_characters => true)

expect(::ActiveRemote::Cached.cache).to receive(:fetch).with(
[versioned_prefix, ConfigurationClass.name, '#find', 'guid.guid'],
{ :expires_in => 100, :active_remote_cached_replace_characters => true }
).and_return(:find_result)

ConfigurationClass.cached_find_by_guid(:guid)
end
end

describe '.default_options_overwrite' do
it 'replaces the current options' do
::ActiveRemote::Cached.default_options(:expires_in => 100, :race_condition_ttl => 5)
::ActiveRemote::Cached.default_options_overwrite(:active_remote_cached_replace_characters => true)

expect(::ActiveRemote::Cached.default_options).to eq(:active_remote_cached_replace_characters => true)
end

it 'clears the current options when an empty hash is given' do
::ActiveRemote::Cached.default_options(:expires_in => 100)
::ActiveRemote::Cached.default_options_overwrite({})

expect(::ActiveRemote::Cached.default_options).to eq({})
end

expect(::ActiveRemote::Cached.default_options({})).to eq({})
it 'returns the new options' do
expect(::ActiveRemote::Cached.default_options_overwrite(:expires_in => 100)).to eq(:expires_in => 100)
end
end

Expand Down
Loading