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

Redis/Predis driver implementation error when using optPrefix #881

Closed MaximilianKresse closed 1 year ago

MaximilianKresse commented 1 year ago

What type of issue is this?

Incorrect/unexpected/unexplainable behavior

Connector/Database version (if applicable)

Redis and Predis

Phpfastcache version

9.1.2 ✅

Describe the issue you're facing

The redis and predis implementation of Driver::driverClear() method is currently incorrect when using the optPrefix setting. The current implementations just always call $this->instance->flushDB(); (redis and predis). This method is not effected by the prefix option and will simply wipe the current database clear.

Expected behavior

Driver::driverClear() should only clear the keys with the configured prefix.

Suggestion to fix the issue (optional)

The fixed driverClear method should look something like this:

if ($this->getConfig()->getOptPrefix() !== '') {
    return (bool) $this->instance->del(...$this->instance->keys('*'));
}
return $this->instance->flushDB();

Have you searched in our Wiki before posting ?

Geolim4 commented 1 year ago

The redis and predis implementation of Driver::driverClear() method is currently incorrect when using the optPrefix setting. The current implementations just always call $this->instance->flushDB(); (redis and predis). This method is not effected by the prefix option and will simply wipe the current database clear.

Unfortunately it's a known limitation of Redis/Predis with Flushdb, I can't really do something about it because of the reason below:

if ($this->getConfig()->getOptPrefix() !== '') {
    return (bool) $this->instance->del(...$this->instance->keys('*'));
}
return $this->instance->flushDB();

This code is a bit dangerous and not optimized because if the cache contains a lot of keys Phpfastcache will have a timeout/memory issue for sure. Retrieving all the keys from a cache is a red line that I always refused to do on Phpfastcache for performances reasons.

MaximilianKresse commented 1 year ago

Mhh ok I understand your point and also don't see any reliable way to do this without the risk of a memory issue. This behaviour should be documented as it may be unexpected. For now we'll try to use our own driver.

Geolim4 commented 1 year ago

You can also create a new database to deal with the optPrefix issue too !

Geolim4 commented 1 year ago

Here is the documentation to help you to create then register a new driver:

https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV7%CB%96%5D-Override-a-core-driver-%7C-Setup-a-custom-driver

You can extends the 3 classes and just override DriverClear() :)