views:

378

answers:

2

Good day,

I am using CacheFilter to filter a certain path to my server (which outputs an image stream to the response stream). And I've configured it in my web.xml as follows:

<filter>
    <filter-name>imagesCache</filter-name>
    <filter-class>com.samaxes.cachefilter.presentation.CacheFilter</filter-class>
    <init-param>
        <param-name>privacy</param-name>
        <param-value>public</param-value>
    </init-param>
    <init-param>
        <param-name>expirationTime</param-name>
        <param-value>2592000</param-value>
    </init-param>
</filter>

...
<filter-mapping>
    <filter-name>imagesCache</filter-name>
    <url-pattern>/my/path/*</url-pattern>
</filter-mapping>

Using my firefox, if I access my url via the address bar, it hits the server the first time but uses the cache during succeeding calls. However, if the url is inside my page ( i.e. <img src="..."/> ), it seems to hit the server all the time.

[EDIT] After a few more testing, accessing my image via the address bar does not work all the time. But caching does seem to work more often with it than . As to whether it really, I am not sure.

Additional Info: my path is something like /my/path?then=some&query=strings. Notice that it doesn't have an extension (i.e. gif, png, jpeg ) but it's mimetype is set properly ( image/gif, image/png, image/jpeg ). I am not sure if the lack of extension or the presence of the query strings have any impact. (Also, another note. though my url have query strings, I am using the same uri + query string over and over again with my tests).

Any ideas why?

Thanks

+1  A: 

I would investigate the HTTP request being sent - particularly the HTTP headers being sent for that image request. You can use a Firefox plugin, and/or check the headers at the servlet end (in the HttpServletRequest object)

Brian Agnew
What should I be on the look out for? Are there any gotchas?
Franz See
HttpFox (https://addons.mozilla.org/en-US/firefox/addon/6647) can be even better for viewing headers. Look to see what's different between the URL-type-in that works and the IMG SRC fetch that doesn't. Something to watch out for: when you hit 'reload', firefox considers that a request to recheck its local copy, with at least an 'if-modified-since' request which your servlet may just treat as a fresh get. Also: you can view browser cache entries w/ expire times with 'about:cache' in the location bar.
gojomo
I would check all the headers to make sure they're what you expect. I don't know how your caching layer works, and hence how it determines whether a request is for something new, or for a previously requested entity.
Brian Agnew
Thanks. Does the browser check 'if-modified-since' before doing a request to the server? Or is 'if-modified-since' for my server's consumption only? Thanks
Franz See
I don't know off the top of my head. I would Google that.
Brian Agnew
+1  A: 

You want to especially look for any Cache-Control header in your request. If a request says something like Cache-Control: no-cache or Cache-Control: max-age=0, then caches can't serve up a cached copy. Or if the response has an Expires header that's too close in time, then it can't be cached for long.

The complete list of headers and explanations is in the HTTP 1.1 specification. See Caching in HTTP (13) and Header Field Definitions (14)

The Firebug plug-in is one good way to check request and response headers using Firefox.

Also watch out how you're using Firefox. Hitting the refresh button is equivalent to saying Cache-Control: no-cache -- it says you want the freshest copy possible, which takes you all the way back to your origin web server.

Jim Ferrans
Thanks, Currently, I am setting the Cache-Control and the Expires in the response headers. Should I set the Cache-Control in the request headers instead?Re: firefox: Wdym? I only test with the 'refresh button' or with 'F5' and not with Shift+RefreshButton or Ctrl+F5. ...Do you mean to say that firefox's RefreshButton is a hard refresh by itself?Thanks,
Franz See
Jim Ferrans