Apache cache does not work

Asked 2 years ago, Updated 2 years ago, 77 views

Place apache2 in Homebrew in MacOSX Sierra and place png images and html.

I wanted to cache the image, so I set httpd.conf as follows.
Importing modules to force disk caching, configuring disk caching, and configuring headers.

I have added this because I saw (somewhere) that Last-modefied and Expire would not be cached if they were not on the response header, and I have verified that both are on the response header successfully.

LoadModule cache_module modules/mod_cache.so
LoadModule disk_cache_module modules/mod_disk_cache.so

<IfModule mod_cache.so>
    <IfModule mod_disk_cache.so>
        CacheRoot/tmp/cache
        CacheEnable disk/
        CacheIgnoreCacheControl On
        CacheIgnoreNoLastMod On
        CacheDefaultExpire86400
        CacheMaxExpire 172800
    </IfModule>
</IfModule>

<IfModule mod_expires.c>
  ExpiresActive on
  ExpiresDefault 
  ExpiresByType image/png 
  <IfModule mod_headers.c>
    Header append Cache-Control "public, max-age=86400"
    Header set Expires "Mon, 26 Jul 2017 05:00 GMT"
  </IfModule>
</IfModule>

After that, I created /tmp/cache, set permission to 777, restart apache, open the network of chrome developer tools, access the image from the a tag in html, and verify that it shows Status 200 for the first time and from disk cache, but there are no files left in /tmp/cache.Also, restart chrome and access the image again to get Status 200.

Is there a way to view the local cache even if I restart the browser in the web server configuration?

Response header on first access.

First time

It's after the second time.
Second or later


After you pointed it out, I found out that the way you set up the apache was wrong.

Header append Cache-Control "public, max-age=86400"

Header set Cache-Control "public, max-age=86400"

Therefore, I have confirmed that the access log does not appear in apache when I restart Chrome.
However, it was accessed in safari, and it seemed to be a unique bug.
I'm thinking about using local cache, but what I want to do is to reduce the number of unnecessary image acquisition communications as much as possible, and avoid having the same image downloaded from apache again and again, but if there are any updates, I'll let them communicate.
I don't want it to work offline, but I would like to use local storage from ApplicationCache, which specifies images in manifest files, and clearCache if I want to update the cache.

I thought so, but I found out from the API that it is bad to put a flag to clear the cache.
I'm going to change the image URL because I don't know how long to set the flag to true, and when the flag is true, every time I start the app, I delete the cache → reacquire the flag is true.

apache mod-cache

2022-09-30 21:22

2 Answers

Server-side cache and client-side cache are separate.You have to think differently."I pointed it out once, but even after correcting it, they still ask me that ""I set up the server side cache, but the client side cache is not done,"" and I get the impression that the questioner himself doesn't know what he wants."

First of all, what do you want from the /tmp/cache directory? You can read from the questionnaire as if you were expecting an image file to be copied under this directory in a different area of the disk, but there is no point in copying it.
You should think of it as a place where dynamically generated content or compressed content such as Content-Encoding:gzip is placed (IIS had the latter, but Apache didn't... I remember dimly).

Next, regarding the client-side cache, I don't know unless you provide not only the settings but also the response headers received by the browser.
There is also a mechanism called application cache that is completely different from the response header.Please consider this as an option.

I checked the response header image, but

Header append Cache-Control "public, max-age=86400"
Header set Expires "Mon, 26 Jul 2017 05:00 GMT"

The header is not included.First of all, you should make sure that the settings are reflected correctly, and if they are not, all the settings will have no meaning.

I heard that it is difficult to update the application cache on the server side (it is difficult to update immediately if the wrong image is delivered), so I am thinking of prioritizing local storage.

It must have been a question of not communicating with the web server.Without communication, we will not be able to know the updates on the web server side in any way.
As I pointed out first, I recommend you reconsider where and what you want to cache.


2022-09-30 21:22

Why do you do Header set manually in addition to mod_expires?

mod_expires is a module that can be configured for Expires or max-age depending on the file format and with easy-to-understand specifications such as 日n days later とFor example, the following statement should print a header with a cache expiration of 1 month from access to the PNG image:

<IfModule mod_expires.c>
  ExpiresActive on
  ExpiresByType image/png "access plus 1 months"
</IfModule mod_expires.c>

If you specify such a long Expires, how do you discard the cache? A common method is to change the URL of the image when updating the image.You can simply rename the file or embed a timestamp or hash value in the query string.

Example: foo.png?v=20170210 < foo.png?v=20170308

This is a different URL than it was before the update, so the existing cache will not be used.

By the way, there is no update confirmation until Expires expires, so Last-Modified or ETag is not used.If updating URLs is troublesome, it is also a good idea to compromise with If-None-Match or If-Modified-Since by setting Expires to 0.You cannot completely lose communication.

Next, localStorage does not have an asynchronous API, so both save and eject block UI threads.The amount of data that can be stored is about 5MB, which is not very large.IndexedDB is a little better, but

You can't use the browser cache deletion feature or super reload, which is often used for cache-related issues, and you can also talk about user-side storage consuming non-cache space.

Also, ApplicationCache using manifest files is already being discontinued, but JavaScript gives you flexibility to control the cache functionality of ServiceWorker, but browser support is not good.

With these in mind, you should consider whether Expires (or If-**) is still insufficient.


2022-09-30 21:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.