Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+5 votes
4.5k views
in Q2A Core by
edited by

I remember that gidgreen once said all blobs get cached by the browser, correct headers are sent. Looking to the Firefox Developer console, I see that this is not the case instead code "304" (yellow bubble) they get loaded completely anew, code "200".

screenshot

So, how to turn on the blob cache?

 

Note: All my blobs are loaded from the database.

And I am using htacess to define the cache time:

    ExpiresByType image/gif         "access plus 1 month"
    ExpiresByType image/jpg         "access plus 1 month"
    ExpiresByType image/jpeg         "access plus 1 month"
    ExpiresByType image/png         "access plus 1 month"
 

 

PS: You can see the same effect here in this forum.

by
Ah, now I found the post again: http://www.question2answer.org/qa/13816/images-generated-getting-cached-browser-missing-something

and Scott's answer: "It uses the max-age directive, which tells the browser to cache the image for 30 days. In this case no Last Modified value is needed."

But still with my browser, the images get downloaded from the server again. Maybe the max-age directive depends on the browser? I'd like to see the "headeres" cache implemented.

1 Answer

+1 vote
by
selected by
 
Best answer

If I'm not wrong q2a blobs are cached, and yours not smiley

If I open a question page (e.g. this one) from the question list (clicking on the link, here on q2a) the image the first time is downloaded from the server, the second time (same operation, go on the question list and click on the question link) is downloaded from the cache (do not do refresh of the question page or it will be always downloaded)

In your site I see it's always downloaded from server.

From a quick look the only difference in the response I can see is these "suspicious" header that your server sends back :

Cache-Control : private, no-cache, no-store, proxy-revalidate, no-transform

and

Pragma : no-cache

 

by
While I was doing a break, I started to understand the cache thing better, I guess. Might it be that the blob cannot be cached because it is not a *file*?

All images *files* can be cached, i.e. saved onto the user's harddrive, but this not: ?qa=image&qa_blobid=3572015248780953596&qa_size=30

PS: Have a good trip!
by
edited by
It looks like the PHP  is smart enough to add these headers by himself...
Have a look at this parameter in your php.ini : "session.cache-limiter".
It should be equal to "nocache".
In the php manual :
http://www.php.net/manual/en/session.configuration.php#ini.session.cache-limiter
you can read :
"Setting the cache limiter to nocache disallows any client/proxy caching".
(you can verify it with your http://[q2a-website]/index.php page )
So letting the php to manage by himself the client caching issues of its pages, you can modify your directive to not manage anymore them (that is : exclude .php extensions from the match - they are already handled) :

<FilesMatch "\.(cgi|pl|htm|html)$">
    ExpiresActive Off
    Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
    Header set Pragma "no-cache"
</FilesMatch>

The http responses containing blobs ("special" php requests) should now be correctly handled by the qa-include/qa-blob.php that at line 55 forces the caching headers programmatically to allow browsers and proxies to cache the blobs.
It should work.....

Ps
to test always use "enter" or navigate "correctly" following the urls of your website, that is : never make a browser page refresh (F5). After every server side configuration you change, you should restart Apache and clear your browser cache before testing :-)
by
NOW it works, thank you so much! :)

The tip of *not refreshing* the site by F5 but just navigation through links showed me the caching, damn it! Why didn't I know before.

Furthermore I am not using the both directives from above anymore.

This is my caching now:

# CACHING
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css     "access plus 1 month"
   
    ExpiresByType text/javascript             "access plus 1 month"
    ExpiresByType application/javascript     "access plus 1 month"
    ExpiresByType application/x-javascript     "access plus 1 month"
   
    ExpiresByType application/json     "access plus 0 seconds"
    ExpiresByType application/xml     "access plus 0 seconds"
    ExpiresByType text/xml         "access plus 0 seconds"

    ExpiresByType application/x-shockwave-flash     "access plus 1 month"
   
    ExpiresByType image/ico         "access plus 1 month"
    ExpiresByType image/x-icon         "access plus 1 month"
    ExpiresByType image/gif         "access plus 1 month"
    ExpiresByType image/jpg         "access plus 1 month"
    ExpiresByType image/jpeg         "access plus 1 month"
    ExpiresByType image/png         "access plus 1 month"
   
    ExpiresByType video/mp4         "access plus 1 month"
    ExpiresByType video/ogg         "access plus 1 month"
    ExpiresByType video/webm         "access plus 1 month"
   
    ExpiresByType application/atom+xml        "access plus 1 hour"
    ExpiresByType application/rss+xml            "access plus 1 hour"

    ExpiresByType application/font-woff            "access plus 1 month"
    ExpiresByType application/vnd.ms-fontobject    "access plus 1 month"
    ExpiresByType application/x-font-ttf        "access plus 1 month"
    ExpiresByType font/opentype                "access plus 1 month"
    ExpiresByType image/svg+xml            "access plus 1 month"
</IfModule>

Thanks again :)
by
I reduced it now to:

# CACHING
<IfModule mod_expires.c>
    ExpiresActive On
   
    ExpiresByType text/css     "access plus 1 month"
    ExpiresByType text/javascript             "access plus 1 month"
    ExpiresByType application/javascript     "access plus 1 month"
    ExpiresByType application/x-javascript     "access plus 1 month"
   
    ExpiresByType image/ico         "access plus 1 month"
    ExpiresByType image/x-icon         "access plus 1 month"
    ExpiresByType image/gif         "access plus 1 month"
    ExpiresByType image/jpg         "access plus 1 month"
    ExpiresByType image/jpeg         "access plus 1 month"
    ExpiresByType image/png         "access plus 1 month"
   
    ExpiresByType application/x-shockwave-flash     "access plus 1 month"
   
    ExpiresByType video/mp4         "access plus 1 month"
    ExpiresByType video/ogg         "access plus 1 month"
    ExpiresByType video/webm         "access plus 1 month"
</IfModule>
...