-
Notifications
You must be signed in to change notification settings - Fork 2
Add pluggable cache adapters with Redis support #82
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
Open
Copilot
wants to merge
3
commits into
master
Choose a base branch
from
copilot/add-redis-cache-adapter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or 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
Copilot
AI
changed the title
[WIP] Introduce pluggable cache client with Redis adapter
Add pluggable cache adapters with Redis support
Nov 11, 2025
26156c9 to
cdb0a77
Compare
tagliala
requested changes
Nov 11, 2025
Member
tagliala
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remember to run bundle exec rubocop
spec/hawk/http/cache_adapters_spec.rb:18:9: C: RSpec/StubbedMock: Prefer allow over expect when configuring a response.
expect(dalli_client).to receive(:get).with('key1').and_return('value1')
^^^^^^^^^^^^^^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:18:33: C: RSpec/MessageSpies: Prefer have_received for setting message expectations. Setup dalli_client as a spy using allow or instance_spy.
expect(dalli_client).to receive(:get).with('key1').and_return('value1')
^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:25:33: C: RSpec/MessageSpies: Prefer have_received for setting message expectations. Setup dalli_client as a spy using allow or instance_spy.
expect(dalli_client).to receive(:set).with('key1', 'value1', 60)
^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:32:33: C: RSpec/MessageSpies: Prefer have_received for setting message expectations. Setup dalli_client as a spy using allow or instance_spy.
expect(dalli_client).to receive(:delete).with('key1')
^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:59:7: C: RSpec/AnyInstance: Avoid stubbing using allow_any_instance_of.
allow_any_instance_of(described_class).to receive(:load_redis_library)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:72:7: C: RSpec/NestedGroups: Maximum example group nesting exceeded [4/3].
context 'with redis:// URL' do
^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:74:28: C: RSpec/MessageSpies: Prefer have_received for setting message expectations. Setup Redis as a spy using allow or instance_spy.
expect(Redis).to receive(:new).with(url: 'redis://localhost:6379')
^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:79:7: C: RSpec/NestedGroups: Maximum example group nesting exceeded [4/3].
context 'with rediss:// URL' do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:81:28: C: RSpec/MessageSpies: Prefer have_received for setting message expectations. Setup Redis as a spy using allow or instance_spy.
expect(Redis).to receive(:new).with(url: 'rediss://localhost:6379')
^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:86:7: C: RSpec/NestedGroups: Maximum example group nesting exceeded [4/3].
context 'with host:port format' do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:88:28: C: RSpec/MessageSpies: Prefer have_received for setting message expectations. Setup Redis as a spy using allow or instance_spy.
expect(Redis).to receive(:new).with(host: 'localhost', port: 6380)
^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:93:7: C: RSpec/NestedGroups: Maximum example group nesting exceeded [4/3].
context 'with just host' do
^^^^^^^^^^^^^^^^^^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:95:28: C: RSpec/MessageSpies: Prefer have_received for setting message expectations. Setup Redis as a spy using allow or instance_spy.
expect(Redis).to receive(:new).with(host: 'myredis', port: 6379)
^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:103:9: C: RSpec/StubbedMock: Prefer allow over expect when configuring a response.
expect(redis_client).to receive(:get).with('hawk:key1').and_return('value1')
^^^^^^^^^^^^^^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:103:33: C: RSpec/MessageSpies: Prefer have_received for setting message expectations. Setup redis_client as a spy using allow or instance_spy.
expect(redis_client).to receive(:get).with('hawk:key1').and_return('value1')
^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:107:7: C: RSpec/NestedGroups: Maximum example group nesting exceeded [4/3].
context 'without namespace' do
^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:111:11: C: RSpec/StubbedMock: Prefer allow over expect when configuring a response.
expect(redis_client).to receive(:get).with('key1').and_return('value1')
^^^^^^^^^^^^^^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:111:35: C: RSpec/MessageSpies: Prefer have_received for setting message expectations. Setup redis_client as a spy using allow or instance_spy.
expect(redis_client).to receive(:get).with('key1').and_return('value1')
^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:119:33: C: RSpec/MessageSpies: Prefer have_received for setting message expectations. Setup redis_client as a spy using allow or instance_spy.
expect(redis_client).to receive(:set).with('hawk:key1', 'value1', ex: 60)
^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:124:33: C: RSpec/MessageSpies: Prefer have_received for setting message expectations. Setup redis_client as a spy using allow or instance_spy.
expect(redis_client).to receive(:set).with('hawk:key1', 'value1')
^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:129:33: C: RSpec/MessageSpies: Prefer have_received for setting message expectations. Setup redis_client as a spy using allow or instance_spy.
expect(redis_client).to receive(:set).with('hawk:key1', 'value1')
^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:133:7: C: RSpec/NestedGroups: Maximum example group nesting exceeded [4/3].
context 'without namespace' do
^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:137:35: C: RSpec/MessageSpies: Prefer have_received for setting message expectations. Setup redis_client as a spy using allow or instance_spy.
expect(redis_client).to receive(:set).with('key1', 'value1', ex: 60)
^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:145:33: C: RSpec/MessageSpies: Prefer have_received for setting message expectations. Setup redis_client as a spy using allow or instance_spy.
expect(redis_client).to receive(:del).with('hawk:key1')
^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:149:7: C: RSpec/NestedGroups: Maximum example group nesting exceeded [4/3].
context 'without namespace' do
^^^^^^^^^^^^^^^^^^^^^^^^^^^
spec/hawk/http/cache_adapters_spec.rb:153:35: C: RSpec/MessageSpies: Prefer have_received for setting message expectations. Setup redis_client as a spy using allow or instance_spy.
expect(redis_client).to receive(:del).with('key1')
Fix all those offences without disabling the cop. Also, please split dalli and redis client to new files and use "autoload"
…op offenses Co-authored-by: tagliala <556268+tagliala@users.noreply.github.com>
Author
Fixed in commit 8d5972e:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implementation Plan for Pluggable Cache Adapters
This PR adds support for pluggable cache clients (Redis and Dalli/Memcached) while maintaining backward compatibility.
Completed:
lib/hawk/http/cache_adapters.rbwith DalliAdapter and RedisAdapter classeslib/hawk/http/caching.rbto support driver selection and auto-detectionSummary:
All 70 tests pass (39 existing + 31 new). Changes are minimal and backward-compatible. Default behavior remains Dalli/Memcached. Redis support is opt-in via configuration.
Structure:
lib/hawk/http/cache_adapters.rb- Main file with autoload declarationslib/hawk/http/cache_adapters/dalli_adapter.rb- Dalli/Memcached adapterlib/hawk/http/cache_adapters/redis_adapter.rb- Redis adapterOriginal prompt
Summary
Introduce a pluggable cache client for Hawk::HTTP caching and add a Redis adapter while preserving the current default behavior with Dalli/Memcached. This allows users to opt-in to Redis by configuration without changing their application code.
Goals
Changes
Update lib/hawk/http/caching.rb to:
Add lib/hawk/http/cache_adapters.rb with:
How to use Redis after this change
In the host application, add to Gemfile: gem 'redis'
Configure Hawk to use Redis:
Hawk::SomeModel.http_options(
cache: {
driver: :redis,
server: 'redis://localhost:6379',
namespace: 'hawk',
expires_in: 120
}
)
Proposed code updates