All Projects → voku → simple-cache

voku / simple-cache

Licence: MIT license
⚡ Simple Cache Abstraction Layer for PHP

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to simple-cache

ColdStorage
Lightweight data loading and caching library for android
Stars: ✭ 39 (+39.29%)
Mutual labels:  cache-storage, caching-library
json-caching-proxy
Node caching HTTP proxy built on top of express-http-proxy. Persists requests and responses to an in-memory HAR-like data structure based on HAR1.2 . Caches JSON content-type responses by default with the ability to cache an entire site; including content-types describing images. Useful for testing front end code, mocking api, and saving the cac…
Stars: ✭ 31 (+10.71%)
Mutual labels:  cache-storage, caching-library
mysqly
Full-featured opensource small-overhead PHP data framework for Mysql built for fast and efficient development
Stars: ✭ 18 (-35.71%)
Mutual labels:  cache-storage
Cache
📦 Nothing but Cache.
Stars: ✭ 2,491 (+8796.43%)
Mutual labels:  cache-storage
Olric
Distributed cache and in-memory key/value data store. It can be used both as an embedded Go library and as a language-independent service.
Stars: ✭ 2,067 (+7282.14%)
Mutual labels:  cache-storage
DTC
DTC is a high performance Distributed Table Cache system designed by JD.com that offering hotspot data cache for databases in order to reduce pressure of database and improve QPS.
Stars: ✭ 21 (-25%)
Mutual labels:  cache-storage
incache
Powerful key/value in-memory storage or on disk to persist data
Stars: ✭ 16 (-42.86%)
Mutual labels:  cache-storage
easy cache
Caching and invalidation have never been so easy
Stars: ✭ 28 (+0%)
Mutual labels:  cache-storage
quill-cache
Caching layer overtop Quill for Scala
Stars: ✭ 24 (-14.29%)
Mutual labels:  cache-storage
kesho
store cache any data type string, boolean, jsonObject, jsonArray, .....
Stars: ✭ 19 (-32.14%)
Mutual labels:  cache-storage
cache-all
Fast, efficient cache engines for both express routes & native nodeJs (redis, in-memory & file cache)
Stars: ✭ 22 (-21.43%)
Mutual labels:  cache-storage
SwiftlyCache
SwiftlyCache is a thread safe IOS general cache Library
Stars: ✭ 84 (+200%)
Mutual labels:  cache-storage
micro-cacheable
A micro utility for data caching
Stars: ✭ 35 (+25%)
Mutual labels:  cache-storage
affilicats
🐈 Progressive Web App demo that showcases flaky network resilience measures (📶 or 🚫📶).
Stars: ✭ 65 (+132.14%)
Mutual labels:  cache-storage
cache
Laravel & Lumen Cache Service | File and Redis cache system
Stars: ✭ 19 (-32.14%)
Mutual labels:  cache-storage

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

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].