Skip to content

[V5˖] Why calling getInstance() each time is a bad practice ?

Georges.L edited this page Feb 20, 2020 · 3 revisions

⁉️ Why

CacheManager::getInstance() will return a driver instance and if not exists will create it.

This has a significant impact on performances due to cr32/serialization for seeking existing driver instances.

🔴 The wrong way

 $cacheItem = CacheManager::getInstance('driverName')->getItem('myKey')->get();  
 $cacheItem2 = CacheManager::getInstance('driverName')->getItem('myKey2')->get(); 
 $cacheItem3 = CacheManager::getInstance('driverName')->getItem('myKey3')->get();

✅ The right way

 $driverInstance = CacheManager::getInstance('driverName');
 $cacheItem = $driverInstance->getItem('myKey')->get();  
 $cacheItem2 = $driverInstance->getItem('myKey2')->get(); 
 $cacheItem3 = $driverInstance->getItem('myKey3')->get();