|
|
<?php
|
|
|
|
|
|
namespace common\components\caching;
|
|
|
|
|
|
use yii\caching\Cache;
|
|
|
use yii\base\InvalidConfigException;
|
|
|
|
|
|
class ShmCache extends Cache
|
|
|
{
|
|
|
|
|
|
//memory size bytes,default 10MB
|
|
|
public $memSize = 10485760;
|
|
|
|
|
|
protected $shm;
|
|
|
|
|
|
/**
|
|
|
* Initializes this application component.
|
|
|
* It checks if extension required is loaded.
|
|
|
*/
|
|
|
public function init()
|
|
|
{
|
|
|
parent::init();
|
|
|
if (!extension_loaded('sysvshm')) {
|
|
|
throw new InvalidConfigException("ShmCache requires PHP sysvshm extension to be loaded.");
|
|
|
}
|
|
|
|
|
|
$key = ftok(__FILE__, 'p');
|
|
|
$this->shm = shm_attach($key,10000);
|
|
|
}
|
|
|
|
|
|
public function exists($key)
|
|
|
{
|
|
|
$key = $this->buildKey($key);
|
|
|
|
|
|
return shm_has_var($this->shm,$key);
|
|
|
}
|
|
|
|
|
|
protected function getValue($key)
|
|
|
{
|
|
|
return @shm_get_var($this->shm,$key);
|
|
|
}
|
|
|
|
|
|
protected function getValues($keys)
|
|
|
{
|
|
|
$results = [];
|
|
|
foreach ($keys as $key) {
|
|
|
$results[$key] = @shm_get_var($this->shm,$key);
|
|
|
}
|
|
|
return $results;
|
|
|
}
|
|
|
|
|
|
protected function setValue($key, $value, $duration)
|
|
|
{
|
|
|
return shm_put_var($this->shm,$key,$value);
|
|
|
}
|
|
|
|
|
|
protected function addValue($key, $value, $duration)
|
|
|
{
|
|
|
return shm_has_var($this->shm,$key) ? false : shm_put_var($this->shm,$key,$value);
|
|
|
}
|
|
|
|
|
|
protected function deleteValue($key)
|
|
|
{
|
|
|
return @shm_remove_var($this->shm,$key);
|
|
|
}
|
|
|
|
|
|
protected function flushValues()
|
|
|
{
|
|
|
return shm_remove($this->shm);
|
|
|
}
|
|
|
|
|
|
public function buildKey($key)
|
|
|
{
|
|
|
return crc32(parent::buildKey($key));
|
|
|
}
|
|
|
} |
...
|
...
|
|