PHPSocialNetwork / phpfastcache

A high-performance backend cache system. It is intended for use in speeding up dynamic web applications by alleviating database load. Well implemented, it can drops the database load to almost nothing, yielding faster page load times for users, better resource utilization. It is simple yet powerful.
https://www.phpfastcache.com
MIT License
2.36k stars 452 forks source link

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

Closed jacekkarczmarczyk closed 2 years ago

jacekkarczmarczyk commented 2 years ago

Configuration

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[bot] commented 2 years ago

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

Geolim4 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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

Geolim4 commented 2 years ago

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 commented 2 years ago

Can you test the v9 branch please ?

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

Thanks.

jacekkarczmarczyk commented 2 years ago

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

jacekkarczmarczyk commented 2 years ago

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 commented 2 years ago

I will fix them, thanks.

Geolim4 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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