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

Fixes #36329: Read UI notification cache as JSON #9792

Merged
merged 1 commit into from
Aug 8, 2023
Merged
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
2 changes: 1 addition & 1 deletion app/services/ui_notifications/cache_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(user_id = User.current.try(:id))

# JSON Payload
def payload
result = cache.read(cache_key)
result = cache.read(cache_key, raw: true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading https://guides.rubyonrails.org/caching_with_rails.html#low-level-caching it makes me wonder if the following code wouldn't be the same:

cache.fetch(cache_key, raw: true, expires_in: cache_expiry) do
  { notifications: notifications }.to_json
end

You don't have the debug info about cache hits and misses, but I wonder how many people care about that. I'd just trust the fetch API to do the right thing.

https://api.rubyonrails.org/classes/ActiveSupport/Cache/Store.html#method-i-fetch is the exact API for that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And just after I post it I think I realize the reason: expiry does a DB call.

Now that I think about it more: this has been on the agenda to replace with Action Cable: https://edgeguides.rubyonrails.org/action_cable_overview.html can use websockets to keep the connection open. It would mean you no longer need to poll, which reduces the need to cache. We still do a lot of full page loads so it's not completely gone, but helps a lot. Probably an interesting area for optimization.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find, I'm not sure all that debug info is necessarily worth it either vs. leaning into using the conventions outlined by Rails guide itself.

if result
logger.debug("Cache Hit: notification, reading cache for #{cache_key}")
return result
Expand Down