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

Disable 'securityKey' and adapter directory. #902

Closed Tommuh closed 1 year ago

Tommuh commented 1 year ago

What type of feature would you like?

Other (Please Specify)

The feature request

I would like options to disable the creation of the securityKey directory and the adapter directory for the file adapter. There is no need for extra directories when the directory is set to a custom directory and not written inside a tmp directory.

Code sample (optional)

No response

References (optional)

No response

Do you have anything more you want to share? (optional)

No response

github-actions[bot] commented 1 year ago

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

Geolim4 commented 1 year ago

Hello,

The sub-directory is necessary for security reason, so it won't be removed/.

Its purpose is to avoid hosts/shared website cache collisions.

Cheers, Georges

Tommuh commented 1 year ago

The host/shared website collisions is only when the tmp directory is used?

What I mean is. In mine setup each project/website has it's own cache directory already. Having those extra 2 directories seems off when a custom path is set.

I totally understand it when the default tmp directory is used.

Tommuh commented 1 year ago

I did overwrite the Files driver and changed it the way I needed. I will leave example behind if anyone else ever need it.

Removed by @Geolim4 for security concerns.

Using it:

CacheManager::addCoreDriverOverride(
    'Files', 
    \Cache\Files::class
);

$cache = new Psr16Adapter('Files', new FilesConfig([
    'path'            => '/some/directory',
    'autoTmpFallback' => false
]);  

What I did notice is that there might be a bug with incorrectly calling "getItemClass". When I set this hard coded to the existing Files\Item class like I did with the config class it keeps searching in the directory were my custom files driver is saved.

I think in CacheItemPoolTrait line 74 and 135 the calling with "self" need to be changed to "static". self::getItemClass() => static::getItemClass()

Geolim4 commented 1 year ago

The host/shared website collisions is only when the tmp directory is used?

What I mean is. In mine setup each project/website has it's own cache directory already. Having those extra 2 directories seems off when a custom path is set.

I totally understand it when the default tmp directory is used.

No, I've already seen collision risks when people are using multiple files driver-based in the same directory: Ex: Sqlite and Files. If you don't make a sub-directory, some CRUD operations (e.g: clear cache) will impact others drivers.

That's why the sub-directory is necessary.

Geolim4 commented 1 year ago

I think in CacheItemPoolTrait line 74 and 135 the calling with "self" need to be changed to "static". self::getItemClass() => static::getItemClass()

Its not needed as this is a core feature not meant to be overridden. Also static keyword have a performance issue since it is JIT-evaluated while self is lexer-evaluated.

Tommuh commented 1 year ago

I do understand your concern. I'm ok with my work around because I only use one adapter for one specific job.

Did you notice any performance problems with self/static? Isn't it called once when creating the driver? Just curious..

Geolim4 commented 1 year ago

I do understand your concern. I'm ok with my work around because I only use one adapter for one specific job.

Did you notice any performance problems with self/static? Isn't it called once when creating the driver? Just curious..

It is also called here: lib/Phpfastcache/Core/Pool/CacheItemPoolTrait.php:135 lib/Phpfastcache/Cluster/ClusterPoolAbstract.php:193

So it is called every time you need a cache item. On big project this can have a significant impact. I'm on philosophy anyway to have any php engine optimizations: isset instead of array_key_exists, fully qualified php core functions, self instead of static where it applies, static closure where it applies, etc.

It's all those micro-optimizations that end to noticeable optimization in the end.

Tommuh commented 1 year ago

Thank you for your feedback. Can't agree more on that.

I removed my static config/item functions and added my own config and item classes to stay with the design choices that are made.