akeb/cache

v1.1.6 2024-05-16 15:32 UTC

This package is auto-updated.

Last update: 2024-05-16 15:35:03 UTC


README

Install

composer project akeb/cache

Composer config

{
    "require": {
        "akeb/cache": "^1.1.6"
    },
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/AKEB/Framework-AKEB-cache"
        }
    ]
}

or

composer require akeb/cache

If you use memcached

global $CACHE_SERVERS;
$CACHE_SERVERS = [
    'default' => ['host'=>'localhost', 'port' => 11211]
];

require_once("../vendor/autoload.php");

If you use FileCache

define('USE_FILE_CACHE', true); // Forcing the use of the file cache

define('PATH_CACHE', '/opt/www/cache/'); // Cache file directory

require_once("../vendor/autoload.php");

Example use Cache

Memcached cache

global $CACHE_SERVERS;
$CACHE_SERVERS = [
    'default' => ['host'=>'localhost', 'port' => 11211], // Description memcached servers
];
require_once("../vendor/autoload.php");

$dateString = '';

$cache = new \Cache('testDate','default'); // Init Cache Object with name "testDate"

if (!$cache->isValid() && $cache->tryLock()) { // If cache not valid and we can lock cache
    // Do something
    $dateString = date("Y-m-d H:i:s", time());

    $cache->update($dateString,600); // Update cache data
    $cache->freeLock(); // Free lock
} else { // Cache valid, or we can't lock cache
    $dateString = $cache->get(); // get data from cache
}

echo $dateString.PHP_EOL;

File cache

define('PATH_CACHE', './tmp/'); // Cache file directory

require_once("../vendor/autoload.php");

$dateString = '';

$cache = new \Cache('testDate','default'); // Init Cache Object with name "testDate"

if (!$cache->isValid() && $cache->tryLock()) { // If cache not valid and we can lock cache
    // Do something
    $dateString = date("Y-m-d H:i:s", time());

    $cache->update($dateString,600); // Update cache data
    $cache->freeLock(); // Free lock
} else { // Cache valid, or we can't lock cache
    $dateString = $cache->get(); // get data from cache
}

echo $dateString.PHP_EOL;

See the examples folder for more details.

Run Unit Tests

./dockerTest.sh