mazz / global-static-cache-manager
A global static cache manager for php
Installs: 5 253
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: >=5.3.0
- psr/container: ^1.0@dev
- psr/simple-cache: ^1.0
Requires (Dev)
- phpunit/phpunit: ^8.0@dev
This package is auto-updated.
Last update: 2024-04-29 03:54:11 UTC
README
About
In some applications it is very useful to initialize a set of data and store it in a static variable to be availbale applicationwide without having to initialize it everytime it is needed. Having a lot of such static cache variables can lead to bad code quality.
The Global-Static-Cache-Manager
introduces a central manager to handle multiple static caches.
The StaticCacheContainer uses the PSR-11 Interface
The StaticCache uses the PSR-16 Interface
Install
Just add the composer package
composer require mazz/php-global-static-cache-manager
Usage
Get instance of the global StaticCacheContainer
and get the Cache with Key currency_cache
. If no cache with this key is available a new StaticCache
for the given key is created and returned.
// Receive the Cache with the currency_cache $currencyCache = StaticCacheContainer::getInstance()->get('currency_cache'); // Fill the cache one by one $currencyCache->set(1, 'currency1'); $currencyCache->set(2, 'currency2'); $currencyCache->set(3, 'currency3'); // It is also possible to fill multiple at once $currencyCache->setMultiple([ 4 => 'currency4', 5 => 'currency5', ]);
You then can simply access the cache in any part of your application
// Get the currency which is saved on index 4 $currency = StaticCacheContainer::getInstance()->get('currency_cache')->get(4); // it is also possible to set a default value $currency = StaticCacheContainer::getInstance()->get('currency_cache')->get(42, 'defaultCurrency'); // or load multiple keys at once $currency = StaticCacheContainer::getInstance()->get('currency_cache')->get([1,2,3);
You may also check if the cache is already initialized. Usefull if you want to fill the cache only once.
// Returns true if cache with the key my_cache_key exists, false otherwise if(!StaticCacheContainer::getInstance()->has('my_cache_key')) { // you may want to fill the cache here }
The StaticCache
introduces also other useful functions: has($key)
, clear()
, delete($key)
License
This project is open-sourced software licensed under the MIT license