Open samnang opened 6 years ago
After I investigate the issue, it seems JSONAPI::Renderer::CachedResourcesProcessor::JSONString#to_json
is never run.
(byebug) { data: JSONAPI::Renderer::CachedResourcesProcessor::JSONString.new('{"id": "1"}') }.to_json
"{\"data\":\"{\\\"id\\\": \\\"1\\\"}\"}"
What we expect the return value should be "{\"data\":{\"id\": \"1\"}}"
I am on Rails 5.1.4.
Hi @samnang – you are right. There is a fix for Rails 5 in https://github.com/jsonapi-rb/jsonapi-rails/pull/70, and I'll try to cut new releases of all gems next week. In the meantime, in order to use caching, you should add the following to your Gemfile:
gem 'jsonapi-renderer', github: 'jsonapi-rb/jsonapi-renderer'
gem 'jsonapi-rails', github: 'jsonapi-rb/jsonapi-rails', branch: 'json-caching'
Thank @beauby, you saved my day. I have been tried different options to get it to work, but none of them are good.
@beauby I have few questions that try to understand how the caching work here:
cache
option in my index action for rendering collection, does it cache individual resource within collection separately or cache the entire collection?Hi @samnang
When I render a resource that includes relationships, are nested relationships required to override cache key, or we just only define in top/root resource only?
Not sure I understand, but what is cached is a fragment of JSON representing a resource (along with its relationships' linkage data).
When I use cache for a top resource and include relationship resources, so if the cache hasn't expired yet, will fetch just a key for top resource and put the entire data from cache store in the response? Or it needs to go each resource in included relationship to fetch one by one?
Each resource will be fetched (though using fetch_multi
).
When I use cacheoption in my index action for rendering collection, does it cache individual resource within collection separately or cache the entire collection?
It caches individual resources.
When I start implementing caching in my app, I noticed CachedResourcesProcessor#process_resources returns an array of strings, but SimpleResourcesProcessor#process_resources returns an array of hashes.
I assume the api should expect to return an array of hashes, so that when Rails' controllers calls
render
in jsonapi-rails, it converts that data hash into json correctly, but if it's a string, it willto_json
again on string which I don't think we want that behaviour.Option 1
Change
CachedResourcesProcessor#process_resources
to return array of hash:Pros: Consistency API. Cons: It's a bit slow if we do with big data because it has to convert json data back and forth.
Option 2
Change
jsonapi-rails
to not callingto_json
again if it's already in string.Pros: It's fast because we don't need to do double works on converting json data. Cons: Inconsistency API between
CachedResourcesProcessor#process_resources
vsSimpleResourcesProcessor#process_resources
.Edit: Option 2 is not working because
renderer.render(resources, options, self)
return jsonapi respone data hash not justdata
attribute.