PHP Classes

Simple Cache Class: Store cache values in different container types

Recommend this page to a friend!
  Info   View files Documentation   View files View files (60)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 59%Total: 191 This week: 1All time: 8,574 This week: 571Up
Version License PHP version Categories
voku_simple-cache 2.1.18MIT/X Consortium ...5.3PHP 5, Cache
Collaborate with this project 

Author

simple-cache - github.com

Description

This class can store cache values in different container types.

It can store and retrieve data values in storage containes like MemCache, Redis, files, arrays, and APC.

The cache expiry time is also a configurable value.

Picture of Lars Moelleken
  Performance   Level  
Name: Lars Moelleken <contact>
Classes: 25 packages by
Country: Germany Germany
Age: 36
All time rank: 62340 in Germany Germany
Week rank: 50 Up3 in Germany Germany Up
Innovation award
Innovation award
Nominee: 11x

Winner: 1x

Recommendations

What is the best PHP cache api response class?
Cache REST API response and use it dynamically

Documentation

Build Status FOSSA Status Coverage Status Scrutinizer Code Quality Codacy Badge Latest Stable Version Total Downloads License Donate to this project using Paypal Donate to this project using Patreon

:zap: Simple Cache 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:

  1. Memcached / Memcache
  2. Redis
  3. Xcache
  4. APC / APCu
  5. OpCache (via PHP-files)
  6. Static-PHP-Cache

Get "Simple Cache"

You can download it from here, or require it using composer.

{
  "require": {
    "voku/simple-cache": "4.*"
  }
}

Install via "composer require"

composer require voku/simple-cache

Quick Start

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');

Usage

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).

No-Cache for the admin or a specific ip-address

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()".

Overwrite the auto-connection option

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
);

Support

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.

Thanks

  • Thanks to GitHub (Microsoft) for hosting the code and a good infrastructure including Issues-Managment, etc.
  • Thanks to IntelliJ as they make the best IDEs for PHP and they gave me an open source license for PhpStorm!
  • Thanks to Travis CI for being the most awesome, easiest continous integration tool out there!
  • Thanks to StyleCI for the simple but powerfull code style check.
  • Thanks to PHPStan && Psalm for relly great Static analysis tools and for discover bugs in the code!

License

FOSSA Status


  Files folder image Files  
File Role Description
Files folder image.github (3 files)
Files folder imagesrc (1 directory)
Files folder imagetests (17 files)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .styleci.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpcs.php_cs Example Example script
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file renovate.json Data Auxiliary data

  Files folder image Files  /  .github  
File Role Description
  Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
  Accessible without login Plain text file FUNDING.yml Data Auxiliary data
  Accessible without login Plain text file ISSUE_TEMPLATE.md Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imagevoku (1 directory)

  Files folder image Files  /  src  /  voku  
File Role Description
Files folder imagecache (22 files, 1 directory)

  Files folder image Files  /  src  /  voku  /  cache  
File Role Description
Files folder imageException (6 files)
  Plain text file AdapterApc.php Class Class source
  Plain text file AdapterApcu.php Class Class source
  Plain text file AdapterArray.php Class Class source
  Plain text file AdapterFile.php Class Class source
  Plain text file AdapterFileAbstract.php Class Class source
  Plain text file AdapterFileSimple.php Class Class source
  Plain text file AdapterMemcache.php Class Class source
  Plain text file AdapterMemcached.php Class Class source
  Plain text file AdapterOpCache.php Class Class source
  Plain text file AdapterPredis.php Class Class source
  Plain text file AdapterXcache.php Class Class source
  Plain text file Cache.php Class Class source
  Plain text file CacheAdapterAutoManager.php Class Class source
  Plain text file CacheChain.php Class Class source
  Plain text file CachePsr16.php Class Class source
  Plain text file iAdapter.php Class Class source
  Plain text file iCache.php Class Class source
  Plain text file iSerializer.php Class Class source
  Plain text file SerializerDefault.php Class Class source
  Plain text file SerializerIgbinary.php Class Class source
  Plain text file SerializerMsgpack.php Class Class source
  Plain text file SerializerNo.php Class Class source

  Files folder image Files  /  src  /  voku  /  cache  /  Exception  
File Role Description
  Plain text file ChmodException.php Class Class source
  Plain text file FileErrorExceptionInterface.php Class Class source
  Plain text file InvalidArgumentException.php Class Class source
  Plain text file RenameException.php Class Class source
  Plain text file RuntimeException.php Class Class source
  Plain text file WriteContentException.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file ApcCacheTest.php Class Class source
  Plain text file ApcuCacheTest.php Class Class source
  Plain text file ArrayCacheTest.php Class Class source
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Plain text file CacheAutoInitOverwriteTest.php Class Class source
  Plain text file CacheAutoInitReplaceTest.php Class Class source
  Plain text file CacheAutoInitTest.php Class Class source
  Plain text file CacheChainTest.php Class Class source
  Plain text file CachePsr16Test.php Class Class source
  Plain text file CacheTest.php Class Class source
  Plain text file FileCacheTest.php Class Class source
  Plain text file FileSimpleCacheTest.php Class Class source
  Plain text file MemcacheCacheTest.php Class Class source
  Plain text file MemcachedCacheTest.php Class Class source
  Plain text file OpCacheTest.php Class Class source
  Plain text file OpCacheVarExporterTest.php Class Class source
  Plain text file RedisCacheTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:191
This week:1
All time:8,574
This week:571Up
User Ratings User Comments (1)
 All time
Utility:87%StarStarStarStarStar
Consistency:75%StarStarStarStar
Documentation:81%StarStarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:59%StarStarStar
Rank:1300
 
nice
7 years ago (muabshir)
70%StarStarStarStar