$ composer require nazg/hcache
supported apc, memcached, map, void, filesystem
<?hh
use Nazg\HCache\Element;
use Nazg\HCache\CacheManager;
$manager = new CacheManager();
$cache = $manager->createCache('memcached');
$mc = new \Memcached('mc');
$mc->addServers([['127.0.0.1', 11211]]);
$cache->setMemcached($mc);
$cache->createCache('map')?->save('cache', new Element('testing', 0));
cache name | cache driver |
---|---|
apc | \Nazg\HCache\Driver\ApcCache |
void | \Nazg\HCache\Driver\VoidCache |
map | \Nazg\HCache\Driver\MapCache |
file | \Nazg\HCache\Driver\FileSystemCache |
memcached | \Nazg\HCache\Driver\MemcachedCache |
redis | \Nazg\HCache\Driver\RedisCache |
<?hh
<<__Override>>
public function fetch(string $id): mixed {
}
<?hh
<<__Override>>
public function contains(string $id): bool {
}
<?hh
<<__Override>>
public function save(string $id, Element $element): bool {
}
remove item
<?hh
<<__Override>>
public function delete(string $id): bool {
}
flush
<?hh
<<__Override>>
public function flushAll(): bool {
}
<?hh
use Nazg\HCache\CacheManager;
use Nazg\HCache\CacheProvider;
$manager = new CacheManager();
$manager->addCache('null', () ==> new NullCache());
$manager->createCache('null');
class NullCache extends CacheProvider {
<<__Override>>
public function fetch(string $id): mixed {
return;
}
<<__Override>>
public function contains(string $id): bool {
return false;
}
<<__Override>>
public function save(string $id, Element $element): bool {
return true;
}
<<__Override>>
public function delete(string $id): bool {
return true;
}
<<__Override>>
public function flushAll(): bool {
return true;
}
}
example
<?hh
use Nazg\HCache\Element;
use Nazg\HCache\Driver\FileSystemCache;
$cache = new FileSystemCache();
$cache->setDirectory(__DIR__ . '/../storages');
$cache->save('file', new Element('testing', 0));
$cache->fetch('file');
$cache->contains('file')
$cache->flushAll();
<?hh
use Nazg\HCache\Element;
use Nazg\HCache\Driver\MapCache;
$cache = new MapCache();
$cache->save('map', new Element('testing', 0));
<?hh
use Nazg\HCache\Element;
use Nazg\HCache\Driver\MemcachedCache;
$cache = new MemcachedCache();
$mc = new \Memcached('mc');
$mc->addServers([['127.0.0.1', 11211]]);
$cache->setMemcached($mc);
$cache->save("qwerty", new Element('testing:cache', 0));