2012-07-04 07:23:08 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* APC CacheResource
|
|
|
|
* CacheResource Implementation based on the KeyValueStore API to use
|
|
|
|
* memcache as the storage resource for Smarty's output caching.
|
|
|
|
* *
|
2014-09-07 13:38:28 +02:00
|
|
|
*
|
2012-07-04 07:23:08 +02:00
|
|
|
* @package CacheResource-examples
|
2014-09-07 13:38:28 +02:00
|
|
|
* @author Uwe Tews
|
2012-07-04 07:23:08 +02:00
|
|
|
*/
|
2014-09-07 13:38:28 +02:00
|
|
|
class Smarty_CacheResource_Apc extends Smarty_CacheResource_KeyValueStore
|
|
|
|
{
|
2012-07-04 07:23:08 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
// test if APC is present
|
2014-09-07 13:38:28 +02:00
|
|
|
if (!function_exists('apc_cache_info')) {
|
2012-07-04 07:23:08 +02:00
|
|
|
throw new Exception('APC Template Caching Error: APC is not installed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read values for a set of keys from cache
|
|
|
|
*
|
2014-09-07 13:38:28 +02:00
|
|
|
* @param array $keys list of keys to fetch
|
|
|
|
*
|
|
|
|
* @return array list of values with the given keys used as indexes
|
2012-07-04 07:23:08 +02:00
|
|
|
* @return boolean true on success, false on failure
|
|
|
|
*/
|
|
|
|
protected function read(array $keys)
|
|
|
|
{
|
|
|
|
$_res = array();
|
|
|
|
$res = apc_fetch($keys);
|
|
|
|
foreach ($res as $k => $v) {
|
|
|
|
$_res[$k] = $v;
|
|
|
|
}
|
2014-09-07 13:38:28 +02:00
|
|
|
|
2012-07-04 07:23:08 +02:00
|
|
|
return $_res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save values for a set of keys to cache
|
|
|
|
*
|
2014-09-07 13:38:28 +02:00
|
|
|
* @param array $keys list of values to save
|
|
|
|
* @param int $expire expiration time
|
|
|
|
*
|
2012-07-04 07:23:08 +02:00
|
|
|
* @return boolean true on success, false on failure
|
|
|
|
*/
|
2014-09-07 13:38:28 +02:00
|
|
|
protected function write(array $keys, $expire = null)
|
2012-07-04 07:23:08 +02:00
|
|
|
{
|
|
|
|
foreach ($keys as $k => $v) {
|
|
|
|
apc_store($k, $v, $expire);
|
|
|
|
}
|
2014-09-07 13:38:28 +02:00
|
|
|
|
2012-07-04 07:23:08 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove values from cache
|
|
|
|
*
|
2014-09-07 13:38:28 +02:00
|
|
|
* @param array $keys list of keys to delete
|
|
|
|
*
|
2012-07-04 07:23:08 +02:00
|
|
|
* @return boolean true on success, false on failure
|
|
|
|
*/
|
|
|
|
protected function delete(array $keys)
|
|
|
|
{
|
|
|
|
foreach ($keys as $k) {
|
|
|
|
apc_delete($k);
|
|
|
|
}
|
2014-09-07 13:38:28 +02:00
|
|
|
|
2012-07-04 07:23:08 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove *all* values from cache
|
|
|
|
*
|
|
|
|
* @return boolean true on success, false on failure
|
|
|
|
*/
|
|
|
|
protected function purge()
|
|
|
|
{
|
|
|
|
return apc_clear_cache('user');
|
|
|
|
}
|
|
|
|
}
|