Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8700085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:06:22+00:00 2026-06-13T02:06:22+00:00

I have an application that is setup to cache a json call. I’ve tried

  • 0

I have an application that is setup to cache a json call. I’ve tried various code syntaxes and it seems to only cache correctly when I have the following:

    caches_page :index, :gzip => true
    cache_sweeper :institutes_sweeper

    respond_to :json, :js

    def index
      @institutes = Institute.select([:id, :name]).all
      render :json => { :institutes => @institutes }
    end

Now when this gets hit, it renders an html file and the caching works as expected. The Rails stack doesn’t get hit.

If I would throw a format block and have the render respond to json, the caching will create the files; however, it will create another file the next time around and ignore the file already there.

Does apache have to be configured differently to work as expected?

Also, I would like to use public/cache/ as the directory to store the cache. I have rails configured correctly; however, I was wondering how to configure apache to serve up those cached files (the same problem above happens where the files get created, but not served).

Thanks for all your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:06:23+00:00Added an answer on June 13, 2026 at 2:06 am

    Just wanted to leave the solution to my problem here.

    Apache needs to be told where the cached files reside. The reason it works when just stored in the public area is because that is how apache acts by default. Add the following config to whereever you define your virtualhost

    RailsAllowModRewrite On
    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} ^(GET|HEAD)
    RewriteCond %{REQUEST_URI} ^/([^.]+)$
    RewriteCond %{DOCUMENT_ROOT}/cache/%1.json -f
    RewriteRule ^/[^.]+$ /cache/%1.json [QSA,L]
    
    RewriteCond %{THE_REQUEST} ^(GET|HEAD)
    RewriteCond %{DOCUMENT_ROOT}/cache/index.json -f
    RewriteRule ^/$ /cache/index.json [QSA,L]
    
    RewriteCond %{THE_REQUEST} ^(GET|HEAD)
    RewriteCond %{REQUEST_URI} ^/([^.]+)$
    RewriteCond %{DOCUMENT_ROOT}/cache/%1.html -f
    RewriteRule ^/[^.]+$ /cache/%1.html [QSA,L]
    
    RewriteCond %{THE_REQUEST} ^(GET|HEAD)
    RewriteCond %{DOCUMENT_ROOT}/cache/index.html -f
    RewriteRule ^/$ /cache/index.html [QSA,L]
    

    Now, tackling the problem of the json rendering html, I figured out that it was because of my calls in the application controller which execute before the actual function executes. If I use a skip_before_filter on those functions, I get one step closer.

    I also figured out that it would be better to wrap the response in a format block.

    respond_to do |format| 
      format.json { json: { institutes: @institutes } }
    end
    

    This will result in the json being cache and the server config will result in it being served correctly.

    I believe for those using Nginx, the following config should be used (not 100% sure though)

        if (-f $request_filename/index.html) {
          rewrite (.*) $1/cache/index.html break;
        }
    
        if (-f $request_filename/index.json) {
          rewrite (.*) $1/cache/index.json break;
        }
    

    If anyone has any feedback on the code above, I’d love to hear it. Thanks!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have setup memcached (unix socket) for my django application. However it seems that
I have a setup application that if user clicks the same setup twice, they
I have a NamedPipeClientStream instance in my application that is setup for duplex communication
I have an application installer created with Inno Setup that deploys multiple binaries and
All, I have my application setup so that i can use a specific username
ISS and ASP.NET. In my setup I have a web application that must be
So my setup for my web application is that I have a general header
I have a Inno Setup project that I want to check if the application
I have a dependency injection container setup in my application and I'm thinking that
I have a named scope set up in my rails application that is used

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.