Recommend this page to a friend! |
![]() ![]() |
Info | ![]() |
![]() |
![]() ![]() |
Reputation | Support forum (1) | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2023-01-18 (2 days ago) ![]() | ![]() ![]() ![]() | Total: 186 This week: 3 | All time: 8,589 This week: 56![]() |
Version | License | PHP version | Categories | |||
voku_simple-cache 2.1.18 | MIT/X Consortium ... | 5.3 | PHP 5, Cache |
Collaborate with this project | Author | |
simple-cache - github.com Description This class can store cache values in different container types. Recommendations What is the best PHP cache api response class? |
|
This is a simple Cache Abstraction Layer for PHP >= 7.0 that provides a simple interaction with your cache-server. You can define the Adapter / Serializer in the "constructor" or the class will auto-detect you server-cache in this order:
You can download it from here, or require it using composer.
{
"require": {
"voku/simple-cache": "4.*"
}
}
composer require voku/simple-cache
use voku\cache\Cache;
require_once 'composer/autoload.php';
$cache = new Cache();
$ttl = 3600; // 60s * 60 = 1h
$cache->setItem('foo', 'bar', $ttl);
$bar = $cache->getItem('foo');
use voku\cache\Cache;
$cache = new Cache();
if ($cache->getCacheIsReady() === true && $cache->existsItem('foo')) {
return $cache->getItem('foo');
} else {
$bar = someSpecialFunctionsWithAReturnValue();
$cache->setItem('foo', $bar);
return $bar;
}
If you have an heavy task e.g. a really-big-loop, then you can also use static-cache. But keep in mind, that this will be stored into PHP (it needs more memory).
use voku\cache\Cache;
$cache = new Cache();
if ($cache->getCacheIsReady() === true && $cache->existsItem('foo')) {
for ($i = 0; $i <= 100000; $i++) {
echo $this->cache->getItem('foo', 3); // use also static-php-cache, when we hit the cache 3-times
}
return $cache->getItem('foo');
} else {
$bar = someSpecialFunctionsWithAReturnValue();
$cache->setItem('foo', $bar);
return $bar;
}
PS: By default, the static cache is also used by >= 10 cache hits. But you can configure this behavior via $cache->setStaticCacheHitCounter(INT).
If you use the parameter "$checkForUser" (=== true) in the constructor, then the cache isn't used for the admin-session.
-> You can also overwrite the check for the user, if you add a global function named "checkForDev()".
You can overwrite the cache auto-detect via "CacheAdapterAutoManager" and the "$cacheAdapterManagerForAutoConnect" option in the "Cache"-constructor. Additional you can also activate the "$cacheAdapterManagerForAutoConnectOverwrite" option in the "Cache"-constructor, so that you can implement your own cache auto-detect logic.
$cacheManager = new \voku\cache\CacheAdapterAutoManager();
// 1. check for "APCu" support first
$cacheManager->addAdapter(
\voku\cache\AdapterApcu::class
);
// 2. check for "APC" support
$cacheManager->addAdapter(
\voku\cache\AdapterApcu::class
);
// 3. try "OpCache"-Cache
$cacheManager->addAdapter(
\voku\cache\AdapterOpCache::class,
static function () {
$cacheDir = \realpath(\sys_get_temp_dir()) . '/simple_php_cache_opcache';
return $cacheDir;
}
);
// 4. try "File"-Cache
$cacheManager->addAdapter(
\voku\cache\AdapterFileSimple::class,
static function () {
$cacheDir = \realpath(\sys_get_temp_dir()) . '/simple_php_cache_file';
return $cacheDir;
}
);
// 5. use Memory Cache as final fallback
$cacheManager->addAdapter(
\voku\cache\AdapterArray::class
);
$cache = new \voku\cache\CachePsr16(
null, // use auto-detection
null, // use auto-detection
false, // do not check for usage
true, // enable the cache
false, // do not check for admin session
false, // do not check for dev
false, // do not check for admin session
false, // do not check for server vs. client ip
'', // do not use "_GET"-parameter for disabling
$cacheManager, // new auto-detection logic
true // overwrite the auto-detection logic
);
For support and donations please visit Github | Issues | PayPal | Patreon.
For status updates and release announcements please visit Releases | Twitter | Patreon.
For professional support please contact me.
![]() |
File | Role | Description | ||
---|---|---|---|---|
![]() |
||||
![]() |
||||
![]() |
||||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Lic. | License text | ||
![]() ![]() |
Example | Example script | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Data | Auxiliary data | ||
![]() ![]() |
Doc. | Documentation | ||
![]() ![]() |
Data | Auxiliary data |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
User Ratings | User Comments (1) | ||||||||||||||||||||||||||||||||||
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.