PHP code example of noga-ng / ncache
1. Go to this page and download the library: Download noga-ng/ncache library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
noga-ng / ncache example snippets
use NCache\NCache;
NCache::config(
__DIR__ . '/ncache.config.json'
)->use('default');
NCache::key('user.42')
->set([
'id' => 42,
'name' => 'Noga',
])
->put();
$user = NCache::key('user.42')->get();
if (NCache::key('user.42')->has()) {
// Cache exists and is still valid.
}
NCache::key('user.42')->delete();
$users = loadUsers();
NCache::key('users')
->set($users)
->put();
NCache::key('users')
->set(
fn () => loadUsers()
)
->put();
NCache::key('users')
->append(
fn () => loadMoreUsers()
)
->put();
NCache::key('users')
->signature(
fn () => getUsersVersion()
)
->set(
fn () => loadUsers()
)
->put();
$valid = NCache::key('users')
->hasValidSignature(
fn () => getUsersVersion()
);
use NCache\Psr\SimpleCache\SimpleCache;
$cache = new SimpleCache(
__DIR__ . '/ncache.config.json',
'default',
);
$cache->set(
'user.42',
['id' => 42],
3600,
);
$user = $cache->get(
'user.42',
);
use NCache\Psr\PsrCache\CacheItemPool;
$pool = new CacheItemPool(
__DIR__ . '/ncache.config.json',
'default',
);
$item = $pool->getItem(
'user.42',
);
if (!$item->isHit()) {
$item
->set([
'id' => 42,
])
->expiresAfter(
3600,
);
$pool->save(
$item,
);
}
$user = $item->get();
$item = $pool->getItem(
'user.42',
);
$item->set([
'id' => 42,
]);
$pool->saveDeferred(
$item,
);
$pool->commit();
NCache::config(
__DIR__ . '/ncache.config.json'
)->use('users');
$config = NCache::config(
__DIR__ . '/ncache.config.json'
);
$config->use('users');
$userCache = NCache::key(
'user.42'
);
$config->use('admin');
$adminCache = NCache::key(
'dashboard'
);
NCache::key('users')
->set($users)
->put();
use NCache\Enum\CType;
use NCache\NCache;
NCache::key(
'users',
CType::REDIS
)
->set($users)
->put();
NCache::key(
'routes',
CType::ARRAY_PHP
)
->set([
'home' => '/',
'users' => '/users',
])
->put();
return [
'home' => '/',
'users' => '/users',
];
NCache::key('settings')
->set($settings)
->put();
NCache::key('users')
->ttl()
->set($users)
->put();
NCache::key('users')
->ttl(3600)
->set($users)
->put();
NCache::key('profile')
->dir('api')
->set($data)
->put();
NCache::key('user.42')
->tags('users')
->set($user)
->put();
NCache::key('article.42')
->tags([
'articles',
'homepage',
])
->set($article)
->put();
[
'state' => true,
'entries' => [
'articles',
'homepage',
],
]
NCache::invalidateTag(
'articles'
);
[
'state' => false,
'entries' => [
'articles',
'homepage',
],
]
NCache::key('users')
->signature(
'users-v2'
)
->set($users)
->put();
$valid = NCache::key('users')
->hasValidSignature(
'users-v2'
);
NCache::key('users')
->signature(
fn () => getUsersVersion()
)
->set($users)
->put();
$valid = NCache::key('users')
->hasValidSignature(
fn () => getUsersVersion()
);
NCache::key('users')
->set([
['id' => 1],
])
->append([
['id' => 2],
])
->put();
NCache::key('users')
->append(
fn () => [
['id' => 3],
]
)
->put();
$remaining = NCache::key('users')
->ttlRemaining();
$state = NCache::key('users')
->ttlState();
$registry = NCache::key('users')
->getRegistry();
NCache::clear();
NCache::clear(
type: CType::JSON
);
NCache::clear(
dir: 'users',
type: CType::JSON
);
json
{
"default": {
"cachePath": "./cache",
"defaultDriver": "JSON",
"namespace": "default",
"extensions": {
"JSON": "json",
"SERIALIZE": "nc",
"STRING": "txt",
"ARRAY_PHP": "php"
},
"defaultTtl": "hours(1)"
}
}
json
{
"extensions": {
"ARRAY_PHP": "php"
}
}