Skip to content

Expiration date bug with sqlite driver #560

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

Closed
wloske opened this issue Jan 10, 2018 · 7 comments
Closed

Expiration date bug with sqlite driver #560

wloske opened this issue Jan 10, 2018 · 7 comments

Comments

@wloske
Copy link

wloske commented Jan 10, 2018

Configuration:

PhpFastCache version: 6.0.8
PHP version: 7.0.6
Operating system: win

Issue description:

I think there's a severe error in expiration date handling somewhere. When I set my cacheTTL to 60 * 60 * 24 * 30 which is 30 days, everything works fine. However, when I ramp it up to 31 days, somehow the expiration date in the database is not correct anymore.

I am using the sqlite driver. For 30 days I get exp = 1518191956 but for 31 days I get exp = 3033878630.

When I dump the cacheItem object right after having set the TTL, it contains the right date. It also contains the right date, when having set the defaultTtl config value but still enters the wrong date into the database.

My code looks basically like this:

$config = array('storage'            => 'sqlite',
	          'htaccess'           => false,
	          'cacheFileExtension' => 'db',
	          'defaultTtl'         => 60 * 60 * 24 * 30,
	          'path'               => $cachePath);

CacheManager::setDefaultConfig($config);
$cache = CacheManager::getInstance("sqlite");

$cacheKey = 'TEST 1'; //increment here, we want only misses
$cacheItem = $cache->getItem($cacheKey);

var_dump($cacheItem);

$cacheValue = "TEST";

$cacheItem->set($cacheValue);
//$cacheItem->expiresAfter(60 * 60 * 24 * 30);

var_dump($cacheItem);

$cache->save($cacheItem);

$cacheContent = $cacheItem->get();
return $cacheContent;
@wloske
Copy link
Author

wloske commented Jan 11, 2018

Test data created with the code above, first with 30 days, second with 31. The first one has both dates, in the object and exp columns right, but the second one not. Something goes wrong when writing to the db.

idkeywordobjectexp
2TEST 1a:5:{s:1:"d";s:4:"TEST";s:1:"g";a:0:{}s:1:"e";O:8:"DateTime":3:{s:4:"date";s:26:"2018-02-10 12:49:44.929876";s:13:"timezone_type";i:3;s:8:"timezone";s:13:"Europe/Berlin";}s:1:"m";N;s:1:"c";N;}1518263384
3TEST 2a:5:{s:1:"d";s:4:"TEST";s:1:"g";a:0:{}s:1:"e";O:8:"DateTime":3:{s:4:"date";s:26:"2018-02-11 12:54:54.563876";s:13:"timezone_type";i:3;s:8:"timezone";s:13:"Europe/Berlin";}s:1:"m";N;s:1:"c";N;}3034021788

@wloske
Copy link
Author

wloske commented Jan 11, 2018

I think that function here this is the root cause of the problem.

vendor/phpfastcache/phpfastcache/src/phpFastCache/Core/Item/ItemExtendedTrait.php:167

 public function getTtl()
    {
        $ttl = $this->expirationDate->getTimestamp() - time();
        if ($ttl > 2592000) {
            $ttl = time() + $ttl;
        }

        return $ttl;
    }

@Geolim4
Copy link
Member

Geolim4 commented Jan 11, 2018 via email

@Geolim4
Copy link
Member

Geolim4 commented Jan 12, 2018

You right,

It seem that we are dealing with dead code here.
I'm gonna check the old drivers too (memcache(d), xcache etc).

Thanks.

@Geolim4
Copy link
Member

Geolim4 commented Jan 13, 2018

Okay I got it :)

Don't worry, this "expiration" column is no longer really used internally :)
We use the serialized object that contains the date as a DateTime object (the only one reliable format).

Currently the Sqlite "is working well" *, but it's still storing wrong (but unused data).

For compatibility reason I'll keep this column in the V6 && V5, but it will deleted in the V7.

However...
* You pointed out a very annoying bug that is related on the whole library for 31+days-based.
The code that you mentioned in /src/phpFastCache/Core/Item/ItemExtendedTrait.php:167 is actually wrong.
In the first time I was wondering why no one complained about this bug before.
Then I realized that this kind of bug cannot be easily spotted due to the fact that you need a month of delay before discovering the fact that the item is not expired as expected.

Really thanks for this report @wloske :)

Geolim4 added a commit that referenced this issue Jan 13, 2018
Geolim4 added a commit to Geolim4/phpfastcache that referenced this issue Jan 13, 2018
Geolim4 added a commit that referenced this issue Jan 13, 2018
(cherry picked from commit ee41c47)
(cherry picked from commit 5212893)
Geolim4 added a commit to Geolim4/phpfastcache that referenced this issue Jan 13, 2018
Geolim4 added a commit that referenced this issue Jan 13, 2018
(cherry picked from commit fcdcc3a)
@Geolim4
Copy link
Member

Geolim4 commented Jan 13, 2018

Thanks for the report.
The next releases (V5, V6, V7) will be dispatched shortly.

Cheers,
Georges

@Geolim4
Copy link
Member

Geolim4 commented Jan 30, 2018

All the majors versions just got a new release:
7.0.0-beta, 6.1.0, 5.0.20

Cheers,
Georges

Geolim4 added a commit to Geolim4/phpfastcache that referenced this issue Apr 7, 2018
Geolim4 added a commit that referenced this issue Apr 7, 2018
Fixed randomly failing test Github-#560
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants