Cache warming with RABL for JSON templates

Well, this post is a bit verbose before I get to the actual question, so the shortened version mainly refers to heating the cache using RABL templates. When calling Rabl.render vs API calls, the generated caches do not have the same cache keys. When using Rabl.render directly, should you expect cache keys to match when the same template is called through the API?

K, now the completion ..

I have a Rails API server on Heroku. I did a lot of optimization using RABL using Russian doll caching to improve the reuse of base objects in collections. However, I still remain with caches for the collection, which when the user generates the first request, the burden depends on experience (for example, 1+ second api calls).

When debugging a sample API request, I get the following cache actions for this object.

... api / v1 / activities / 26600:

Cache read: rabl/activities/26600-20140423170223588554000//hash/d30440d18014c72014a05319af0626f7 
Cache generate: rabl/activities/26600-20140423170223588554000//hash/d30440d18014c72014a05319af0626f7 
Cache write: rabl/activities/26600-20140423170223588554000//hash/d30440d18014c72014a05319af0626f7

, so for the same object when calling ... api / v1 / actions (after calling above) I get the desired cache click:

Cache read: rabl/activities/26600-20140423170223588554000//hash/d30440d18014c72014a05319af0626f7 
Cache fetch_hit: rabl/activities/26600-20140423170223588554000//hash/d30440d18014c72014a05319af0626f7

. - , / API .
, . wget API (. qaru.site/questions/297126/...). - Heroku, , .

RABL (https://github.com/nesquena/rabl#rendering-templates-directly), , RABL- - API (, ).

, API rails , .

irb(main):002:0> @activity = Activity.find(26600)
irb(main):003:0> Rabl.render(@activity, 'api/v2/activities/show_no_root', :view_path => 'app/views', :format => :json) 
Cache read: rabl/activities/26600-20140423170223588554000//hash 
Cache generate: rabl/activities/26600-20140423170223588554000//hash 
Cache write: rabl/activities/26600-20140423170223588554000//hash

, -, , - -. , . RABL.

-, .

 Cache digest for api/v1/activities/_show.rabl: d30440d18014c72014a05319af0626f7

, , Rabl::Renderer .

+4
2

Rabl:: Render - - . , sidekiq, , , api , .

 class CacheWarmApi
   include Sidekiq::Worker
   sidekiq_options :queue => :warmers

   def perform( url_helper, args, params={},method='get')
     if method == 'get'
       session = ActionDispatch::Integration::Session.new(Rails.application)
       session.get(session.send(url_helper, *args), params)
     end
   end
 end

:

 CacheWarmApi.perform_async( :api_v2_expensiveapi_url, args_array , params_hash)

, , , Rabl:: Render.

+1

, Rabl.render (ref), (Redis ) :

:

#... do some long work to calculate object_to_render_with
#.
#.
#.

#render the result using Rabl 
render_result= Rabl.render(object_to_render_with, rabl_view_relative_path,
                 view_path: File.join(Rails.root, 'app/views'),
                 format: :json) #already the default, being explicit
Redis.current.set(cache_key, render_result, ex: (DEFAULT_CACHE_EXPIRY))

:

def request
    #check if we have a recent result
    res = Redis.current.get cache_key
    if res.nil?
      # No result in cache, start the worker
     Worker.perform_async
    end

    # use that cached result or an empty result
    render json: res || {status: 'in_progress'}
end

: ( , Redis ).

0

Source: https://habr.com/ru/post/1537786/


All Articles