oscarotero / psr7-middlewares

[DEPRECATED] Collection of PSR-7 middlewares
MIT License
668 stars 56 forks source link

Question about Cache Middleware #68

Closed llaville closed 7 years ago

llaville commented 7 years ago

Hello Oscar,

I've used the cache middleware of release 3.19, and I've a question about multi-device usage !

The cache key is specified there https://github.com/oscarotero/psr7-middlewares/blob/master/src/Middleware/Cache.php#L121

That means the cache in PSR-6 pool is shared between all clients (desktop / mobile / tablet)

But the 304 is sent back to client device to tell it to use its device cache version. If a desktop client create a cache in PSR-6 pool (server side) it then use it by mobile client (that sould not probably have a cache version, if it's first call)

Perharps an issue ? Or did I'm wrong and missed something !

Thanks in advance for your answer. Laurent

oscarotero commented 7 years ago

Hi. The cache validation is done here: https://github.com/micheh/psr7-cache/blob/master/src/CacheUtil.php#L260

As you can see, the validation is done using the headers Last-Modified (from response) and If-Modified-Since (from request). If a device has not a cached version of a page it doesn't send the If-Modified-Since header, the cache validation is not passed and no 304 response will be sent.

Regards.

llaville commented 7 years ago

Hello again,

I think there is an issue in multi users environment. Let's me explains. Suppose we have at least two users with two browsers that send the same UserAgent header !

In such case, first user will generate a normal response and save it in cache (pool). Then after, the second user call the same URI but the response is already in cache pool (generated by first user). So as both users have the same UserArgent (headers sent), we reply a 304 to second user that does not have yet a cache copy in his browser.

Hope my explains are enough clear ?

If i'm right we should fix this situation : either with IP (ClientIp Middleware required) or with differenct cache key hash !

Tell me what you think about that !

Laurent

oscarotero commented 7 years ago

I think you're misunderstanding this middleware: