-
-
Notifications
You must be signed in to change notification settings - Fork 7k
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 (back) rails-level JSON caching #11333
Conversation
e7a7124
to
dab1611
Compare
2682add
to
237d214
Compare
237d214
to
d62c710
Compare
def render_with_cache(**options) | ||
raise ArgumentError, 'only JSON render calls are supported' unless options.key?(:json) || block_given? | ||
|
||
key = options.delete(:key) || [[params[:controller], params[:action]].join('/'), options[:json].respond_to?(:cache_key) ? options[:json].cache_key : nil, options[:fields].nil? ? nil : options[:fields].join(',')].compact.join(':') |
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.
The cache key being dependent on the controller's action would mean that StatusesController#show
and StatusesController#activity
would have different cache keys, which isn't optimal. Not sure that would really be a problem in practice, though. What is StatusesController#activity
used for anyway?
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.
That would be the correct behavior because those actions return different json. Activity is used for the resolveable id of activities.
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.
Do they really return different json?
Hm, also… that's not directly related, but #activity
doesn't seem to require signatures even if authorized_fetch_mode?
is true
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.
The show returns a Note object, the activity returns a Create or Announce object.
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.
Oh, right! Sorry about that.
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.
I'm not super comfortable with how it moves the cache key logic in one place which tries to figure out a lot of cases by itself, but that's just my opinion. Otherwise it looks fine.
This is my idea for #11330 with prettier (and less) code, replacing
render_cached_json
withrender_with_cache
.The
:key
, unless supplied explicitly, is generated from the controller name, controller action, the cache key of the object being serialized, as well as fields chosen. For example, it could look like this:When no fields are passed (such as when all fields should be rendered), it's a lot shorter:
When there's no record, the key is even simpler:
The method supports being passed
:json
a record or hash, a method name as a symbol, or a block. The latter two will only get evaluated if there is no cache. Example:This means invocations of
render_with_cache
look almost identical to just usingrender
and you don't need to think about manually specifying cache keys.