Force caching data on Client Side
I am writing a script that will load custom javascript and images based on details entered by user (using php on server). Is there a way to force these JS and images to be cached on browser on client side to reduce loa开发者_如何学Cd on server when similar data is requested next time?
yes there is a way you can set expire headers to a longer period. i.e. if you are using .htaccess write this in your .htaccess file
<IfModule mod_expires.c>
AddType image/x-icon .ico
ExpiresActive On
ExpiresByType image/* A86400
ExpiresByType image/x-icon A2592000
ExpiresByType application/javascript A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType text/javascript A2592000
ExpiresByType text/css A2592000
SetEnvIfNoCase Request_URI ".*/(uploads|weather)/.*\.(jpeg|jpg|png|gif)$" is_monthly=true
Header set cache-control: max-age=604800 env=is_monthly
You'll need to look at your servers caching defaults, and see how they can be controlled, if its apache there is mod_cache and mod_expire which you can use via .htaccess:
http://httpd.apache.org/docs/2.2/mod/mod_cache.html
http://httpd.apache.org/docs/2.0/mod/mod_expires.html
Or.. you could serve the files up as PHP and use headers for cache-control. But that would be crazy...
精彩评论