-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[WIP][HttpKernel] PSR6 Store for HttpCache #20061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
{ | ||
if (!$response->headers->has('X-Content-Digest')) { | ||
$contentDigest = $this->generateContentDigest($response); | ||
$item = $this->cacheBackend->getItem($contentDigest); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would make this a guard clause instead. Please note that it should be @return string|null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I should create a CacheItem instead. No need to query the backend as we're going to overwrite the value anyway (if it's there).
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class Psr6Store implements StoreInterface | ||
{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason to not make it final?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope. Good idea.
/** | ||
* @var CacheItemPoolInterface | ||
*/ | ||
private $cacheBackend; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about $cachePool
or even just $pool
. See https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Cache/DoctrineProvider.php#L24
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For being not in the Cache
namespace, i'd favor $cachePool
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed for consistency 👍
d0ee060
to
1ed4431
Compare
1ed4431
to
8aac80d
Compare
@jakzal yeah saw that issue as well when looking for the one mentioned. While this is not the best to use for production servers, it's a nice way to get started if you don't (yet) have a proper caching layer. I think this is a nice feature to have. |
7e548bb
to
479b3f2
Compare
479b3f2
to
8a4c89b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a good start :) Would it make sense to make an AbstractStorage to share a few things with other storages implems?
final class Psr6Store implements StoreInterface | ||
{ | ||
/** | ||
* @var CacheItemPoolInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not needed, already hinted in the constructor
private $cachePool; | ||
|
||
/** | ||
* List of locks acquired by the current process. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure this adds value
/** | ||
* List of locks acquired by the current process. | ||
* | ||
* @var array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nor that
private $locks = array(); | ||
|
||
/** | ||
* @param CacheItemPoolInterface $cachePool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
already in the constructor
} | ||
|
||
/** | ||
* Locates a cached Response for the Request provided. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{@inheritdoc}
} | ||
|
||
/** | ||
* Writes a cache entry to the store for the given Request and Response. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{@inheritdoc}
$headers = $response->headers->all(); | ||
unset($headers['age']); | ||
|
||
$this->save($key, serialize(array(array($request->headers->all(), $headers)))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
serializing is already done by the cache pool
} | ||
|
||
/** | ||
* Invalidates all cache entries that match the request. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
{@inheritdoc}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(same below :) )
return true; | ||
} | ||
|
||
return $this->cachePool->hasItem($this->getLockKey($request)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use $lockKey
* | ||
* @return CacheItem | ||
*/ | ||
private function createCacheItem($key, $value, $isHit = false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes the implementation work only with Symfony's Cache components: cache items aren't interoperable.
You must get the item from the pool first (and maybe keep this as an optimization when Symfony Cache is used, another idea being to keep track of fetched items in this storage object to use them here)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is kind of feedback i wanted to get (ignore comments etc, i left them there for my reference until I'm done and replace them with inheritdoc) ;)
You must get the item from the pool first
I did it first, perhaps I tried to optimise too early and replaced a call to pool with this. I'll revert back. Once it's all working we can run tests and see if it needs improving.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having the optimization is OK if you keep the non-optimized version for other pools
I thought about it, but the current implementation is very coupled to the filesystem. I'm currently mirroring the behaviour, but the current implementation is optimised for the filesystem. I expect the psr6 implementation diverge eventually to be optimised for the cache pool. I'll try this approach nevertheless, to see which one makes more sense. |
Just for the record, I'm very skeptical about this one. We explicitly tied the HTTP cache to the filesystem. |
@fabpot no worries. If it's rejected here, I'll create a separate library (so work won't be wasted). |
Now that there's a |
@jakzal any feedback here? Even if it's just a "sorry, no time, please open a new PR"? 😄 |
I'm closing this one as I think this is a bad idea for several reasons:
So, for these reasons, it won't be in Symfony core. |
Leaving it here for people looking for a solution without it being in Symfony core: https://packagist.org/packages/toflar/psr6-symfony-http-cache-store |
Relying on PSR-6 has a nice side-effect: the ability to use cache tags (see api-platform/core#1689). Is supporting cache tags something we may want in core? |
This is a WIP PR to get early feedback. I'd love to get this in for 3.2.