-
Notifications
You must be signed in to change notification settings - Fork 11.2k
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
[5.8] Fix cache TTL compliance to PSR interface #27166
[5.8] Fix cache TTL compliance to PSR interface #27166
Conversation
The put method is an alias for the PSR Cache set method and should also accept the null type.
At the moment our Cache implementation implements the Psr\SimpleCache\CacheInterface which states that the TTL, if null, allows for either the library or the driver to set a default. At the moment it's impossible with Laravel to allow for the drivers/stores to handle defaults on their own because the method signature doesn't allows for this. By allowing it as a valid type, developers may choose to implement a cache driver which handles a default TTL for cache items. Our current cache stores are updated to handle the new null value as well to properly return false when null is passed. This is because our current cache stores don't provide a default TTL.
Same as previous commit.
The success should depend on the result of the put method call.
The way we currently handle null TTLs in our own cache repository is that we return false and don't save anything. But according to the PSR cache repository spec there should be support for handling defaults in drivers. At the moment the null TTL isn't cascaded to the cache stores so there's no way for them to implement a default. This commit allows for the TTL to be passed to the cache stores so they may set their default TTL is they have done so. It also still makes sure that for any TTL below or equal to 0 nothing will be set. No tests were adjusted (just a really minor adjustment to a Redis test) and more tests have been added to verify the new behavior. Also fixed some really small minor things here and there.
Thank you for the PR @driesvints . However, I still don't think this conforms to either PSR-6 or PSR-16. All that has changed here is that instead of
The first sentence, you've already addressed this in the description:
Which I am fine with, however the next parts I believe are important:
and
To sum up, I read this as: if This implementation, if I have understood it correctly, will: if you pass Cache::put('foo', 'bar'); is currently to no-op. The expectation of PSR-16 is that the value As |
Heya @asgrim, thanks for your feedback. I've read through the PSR and your remarks and mostly agree with them. I've tried to whip up a small example of the /**
* Store an item in the cache.
*
* @param string $key
* @param mixed $value
* @param \DateTimeInterface|\DateInterval|float|int|null $minutes
* @return bool
*/
public function put($key, $value, $minutes = null)
{
if (is_array($key)) {
return $this->putMany($key, $value);
}
if ($minutes === null) {
return $this->forever($key, $value);
}
$minutes = $this->getMinutes($minutes);
if ($minutes <= 0) {
return $this->delete($key);
}
$result = $this->store->put($this->itemKey($key), $value, $minutes);
if ($result) {
$this->event(new KeyWritten($key, $value, $minutes));
}
return $result;
} One thing still concerns me with the above implementation: this will prevent the
In my opinion this conflicts with the expiration part of the PSR. Wouldn't the following description be better?
Let me know what you think. |
I agree, it is a little confusing, and the "that" (which I bolded above) is the ambiguous article in this sentence. My interpretation is done with the assumption that most caching libraries have a "library" (that would typically implement PSR-16
That is to say, the However, this is of course based on an assumption about cache library design (that I've seen certainly in Doctrine Cache, as well as from what I've seen so far, the Laravel Cache too), which may indeed be incorrect. If this doesn't hold ground, then I'd really defer further detailing to the authors of PSR-16 for interpretation. |
@asgrim I see. Well in that case I believe I can revert my changes to the store implementations (because the @Ocramius do you have any more remarks about the above? |
@driesvints yes, I think your example above is a good approach. The other option is to implement the |
For fear of being too lazy, I went and checked |
@asgrim awesome. Thanks for your reply 👍 I'm going to close this PR and open up a new one in a few days as the new implementation will differ quite a bit. This will keep it easier and more clear for Taylor to review it once it's ready. Thanks for everyone's feedback! |
This PR does a couple of things:
add
method in the cache repository when a call cascaded to theput
method.The PSR spec for the TTL value goes as follows:
The way we currently handle
null
TTLs in our own cache repository is that we return false and don't save anything. But according to the PSR cache repository spec there should be support for handling defaults in drivers. At the moment thenull
TTL isn't cascaded to the cache stores so there's no way for them to implement a default. This commit allows for the TTL to be passed to the cache stores so they may set their default TTL is they have done so. I've updated our own cache stores to returnfalse
immediately when they receive anull
TTL because they don't offer a default at the moment.It also still makes sure that for any TTL below or equal to 0 nothing will be set.
No tests were adjusted (just a really minor adjustment to a Redis test) and more tests have been added to verify the new behavior.
All in all this PR should make us conform to the PSR spec better. This PR may later be extended to allow Laravel to offer a default from its own but that's a different PR.
Fixes #27160
PS.: there seems to be an additional problem that the TTL according to the PSR spec should be in seconds while we handle it in minutes. We should maybe have a look at that as wel.