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

No where to get instance ID #803

Closed mitmelon closed 3 years ago

mitmelon commented 3 years ago

Getting instance ID is impossible....

How do you know an instance has been called to prevent calling twice...

Lets assume i have two functions performing this below

function one(){
$this->internalCacheInstance = CacheManager::getInstance($driver, $config);
}

function two(){
$this->internalCacheInstance = CacheManager::getInstance($driver, $config);
}

Calling the two functions at the same time results in PHP Notice:  [Sqlite] Calling many times CacheManager::getInstance() for already instanced drivers is a bad practice and have a significant impact on performances.
           See https://github.com/PHPSocialNetwork/phpfastcache/wiki/[V5]-Why-calling-getInstance%28%29-each-time-is-a-bad-practice-%3F

Avoiding this error is impossible on two separate classes running those functions is hard...

The only method applicable is to create a function into CacheManager that let us find instanceID to cross check if an instance has been called or not... If called then return CacheManager object if not set it...

Please remove this functionality from CacheManager if the above could not be implemented...

github-actions[bot] commented 3 years ago

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

Geolim4 commented 3 years ago

Hello,

The issue is on your side: Why do you have two different functions calling phpfastcache in the exact same way ? It's an implementation design issue, not a phpfastcache issue.

This is how you should implement it:

function one(){
cache();
}

function two(){
cache();
}

function cache(){
$this->internalCacheInstance = CacheManager::getInstance($driver, $config);
}

Cheers, Georges

mitmelon commented 3 years ago

Hello,

The issue is on your side: Why do you have two different functions calling phpfastcache in the exact same way ? It's an implementation design issue, not a phpfastcache issue.

This is how you should implement it:

function one(){
cache();
}

function two(){
cache();
}

function cache(){
$this->internalCacheInstance = CacheManager::getInstance($driver, $config);
}

Cheers, Georges

Even after following your examples same error is still showing....

Calling the two functions at the same time results in PHP Notice: [Sqlite] Calling many times CacheManager::getInstance() for already instanced drivers is a bad practice and have a significant impact on performances. See https://github.com/PHPSocialNetwork/phpfastcache/wiki/[V5]-Why-calling-getInstance%28%29-each-time-is-a-bad-practice-%3F

When i called one() and two in two separate classes... The error above always show.... So its a bug...

You can try replicating the issue to see what am talking about... Call both one() and two() multiple times and show your error logs..

Geolim4 commented 3 years ago

No it's not a bug, it's on purpose.

You should not call for the same cache instance multiple times along your script.

You must store the cacheInstance somewhere in your project and re-use it depends your needs.

So no it's not a bug, but an implementation issue on your side.