Skip to content
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

Cache item throw an error on reading with DateTimeImmutable date objects #860

Closed
jacekkarczmarczyk opened this issue Apr 4, 2022 · 12 comments · Fixed by #863
Closed

Cache item throw an error on reading with DateTimeImmutable date objects #860

jacekkarczmarczyk opened this issue Apr 4, 2022 · 12 comments · Fixed by #863

Comments

@jacekkarczmarczyk
Copy link

Configuration

  • PhpFastCache version: 9.1
  • PhpFastCache API version: 4.1.0
  • PHP version: 8.1
  • Operating system: win10

Describe the bug

When updating cache item like this:

        $item = $cacheItemPool->getItem('key');
        $item->set('some data');
        $item->setExpirationDate(new DateTimeImmutable('+1 year'));
        $cacheItemPool->save($item);

(which is valid as setExpirationDate allows DateTimeInterface)

phpfastcache fails on reading data:

TypeError: DateTime::createFromFormat(): Argument #2 ($datetime) must be of type string, DateTimeImmutable given

in \Phpfastcache\Core\Pool\DriverBaseTrait::driverUnwrapEdate

Expected behavior
No error when expiration date set to DateTimeImmutable

@github-actions
Copy link

github-actions bot commented Apr 4, 2022

Hello curious contributor !
Since it seems to be your first contribution, make sure that you've been:

  • Reading and searching out our WIKI
  • Reading and agreed with our Code Of Conduct
  • Reading and understood our Coding Guideline
  • Reading our README
    If everything looks unclear to you, tell us what 😄
    The Phpfastcache Team

@Geolim4 Geolim4 self-assigned this Apr 4, 2022
@Geolim4 Geolim4 added the 9.1 label Apr 4, 2022
@Geolim4
Copy link
Member

Geolim4 commented Apr 4, 2022

Hello,

Nice catch, but I'm facing one potential problem with DateTimeImmutable object.

I'm not sure how atomic operations such as $item::expiresAfter() should react if Immutable object are set 🤔

I think that I should convert immutable object to mutable object before passing them to the core.

@Geolim4
Copy link
Member

Geolim4 commented Apr 4, 2022

After some code review, I've decided that Immutable DateTime will be automatically converted to Mutable DateTime for one simple reason: Very few backend support "plain DateTime" object storage. In fact for most of them I have to store the date as an ATOM formatted string .

So I cannot even persist the mutability status of the Datetime object along with other stored data.

@jacekkarczmarczyk
Copy link
Author

Every solution is fine as long as it doesn't throw error when reading data saved with DateTimeImmutable expiration date :)

@Geolim4
Copy link
Member

Geolim4 commented Apr 4, 2022

Indeed, however if I fix that error I will introduce an unintended side-effect that I'll need to fix too, so I need to fix both of them.

@Geolim4 Geolim4 changed the title expiration date (and similar) should accept DateTimeInterface Cache item thrown an error on save with DateTimeImmutable date objects Apr 4, 2022
@Geolim4 Geolim4 changed the title Cache item thrown an error on save with DateTimeImmutable date objects Cache item throw an error on save with DateTimeImmutable date objects Apr 4, 2022
Geolim4 added a commit that referenced this issue Apr 4, 2022
@Geolim4
Copy link
Member

Geolim4 commented Apr 4, 2022

Can you test the v9 branch please ?

If it's ok for you I'll push a fix for v8.1 tomorrow too.

Thanks.

@Geolim4 Geolim4 added the 8.1 label Apr 4, 2022
@jacekkarczmarczyk
Copy link
Author

jacekkarczmarczyk commented Apr 5, 2022

Thank you for the patch, I have some comments and problems:

  • how to test v9 branch? ;) When I did composer require phpfastcache/phpfastcache:v9 it installed version 9.0, when I did composer require phpfastcache/phpfastcache:dev-v9 I got an error Could not find package phpfastcache/phpfastcache in a version matching "dev-v9" and a stability matching "dev". (I have "minimum-stability": "dev", in my composer.json)
  • the issue title (as well as the description in the testcase) is wrong, the error was thrown on reading, not on saving
  • how about using \DateTime::createFromInterface instead of \DateTime::createFromImmutable?

@jacekkarczmarczyk
Copy link
Author

jacekkarczmarczyk commented Apr 5, 2022

Update: I've managed to install it with composer require phpfastcache/phpfastcache:v9.x-dev, I'm getting a different error though which now I'm investigating

Update2: new error was not related to phpfastcache, so looks good for me except these 2 small suggestions from the previous comment

@Geolim4
Copy link
Member

Geolim4 commented Apr 5, 2022

I will fix them, thanks.

@Geolim4 Geolim4 changed the title Cache item throw an error on save with DateTimeImmutable date objects Cache item throw an error on reading with DateTimeImmutable date objects Apr 5, 2022
@Geolim4
Copy link
Member

Geolim4 commented Apr 5, 2022

how about using \DateTime::createFromInterface instead of \DateTime::createFromImmutable

Finally, I will keep the code as is, since I will made the same fix on the v8.1 and the v8.1 is still php 7.3 compatible, however \DateTime::createFromInterface has been added in PHP 8.

Geolim4 added a commit that referenced this issue Apr 5, 2022
@Geolim4
Copy link
Member

Geolim4 commented Apr 5, 2022

Since the 9.1.0 have been released recently, please except the 9.1.1 to be released by the end of the week, maybe the next one.

@jacekkarczmarczyk
Copy link
Author

I've switched to DateTime as a workaround, when you release 9.1.1 I'll go back to DateTimeInterface, no hurry
Thanks!

@Geolim4 Geolim4 mentioned this issue Apr 15, 2022
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants