1
0
mirror of https://github.com/danog/dns.git synced 2024-12-02 17:38:05 +01:00
dns/lib/Addr/Cache/APCCache.php

60 lines
1.2 KiB
PHP

<?php
namespace Addr\Cache;
use Addr\Cache;
class APCCache implements Cache {
/**
* @param string $prefix A prefix to prepend to all keys.
*/
function __construct($prefix = 'AddrCache\Cache\APCCache')
{
$this->prefix = $prefix;
}
/**
* Attempt to retrieve a value from the cache
*
* Returns an array [$cacheHit, $value]
* [true, $valueFromCache] - if it existed in the cache
* [false, null] - if it didn't already exist in the cache
*
* @param $key
* @return array
*/
public function get($key)
{
$key = $this->prefix.$key;
$value = apc_fetch($key, $success);
if ($success) {
return [true, $value];
}
return [false, null];
}
/**
* Stores a value in the cache. Overwrites the previous value if there was one.
*
* @param $key
* @param $value
* @param null $ttl
*/
public function store($key, $value, $ttl = null)
{
$key = $this->prefix.$key;
apc_store($key, $value, $ttl);
}
/**
* Deletes an entry from the cache.
* @param $key
*/
public function delete($key)
{
$key = $this->prefix.$key;
apc_delete($key);
}
}