PHP扩展中的缓存Yac
说明:yac进程中可以共享一些数据。
安装:
打开URL:http://pecl.php.net/package/yac
下载一个yac的压缩包
我这是下载了一个yac-2.0.2.tgz压缩包
tar xzvf yac-2.0.2.tgz(说明:解压)
cd yac-2.0.2
phpize
./configure --prefix=/usr/local/yac --with-php-config=/usr/local/php/bin/php-config
make
make install
安装完毕!
根据命令 find / -name php.ini
找到php.ini的文件并打开
添加如下代码
[yac]
extension=yac.so
yac.enable = 1
yac.keys_memory_size = 4M
yac.values_memory_size = 64M
yac.compress_threshold = -1
yac.enable_cli = 0
查看php配置 命令:php -m
如果看到了yac,则说明安装成功
下边举例说明:
创建一个yacCache.php文件:
class YacCache
{
/**
* @var string
*/
public $prefix = ‘_yac:’;
private KaTeX parse error: Expected '}', got 'EOF' at end of input: …{ if(!(this->_cache instanceof \Yac)){
KaTeX parse error: Expected 'EOF', got '\Yac' at position 20: …->_cache = new \̲Y̲a̲c̲(this->prefix);
}
return $this->_cache;
}
/**
* 针对较长的key做has运算
* @param $key
* @return string
*/
protected function _formatKey($key){
if(strlen($this->prefix.$key) > 48){
return md5($key);
}
return $key;
}
/**
* @param string $key
* @param mixed $value
* @param int $timeOut
* @return mixed
*/
public function set($key,$value,$timeOut=0){
$key = $this->_formatKey($key);
return $this->getCache()->set($key,$value,$timeOut);
}
/**
* @param array $kvs
* @param int $timeOut
* @return mixed
*/
public function mSet(array $kvs,$timeOut=0){
$haskeys = [];
foreach ($kvs as $index => $kv) {
$hashkeys[$this->_formatKey($index)] = $kv;
}
return $this->getCache()->set($hashkeys,$timeOut);
}
/**
* @param string $keys
* @return mixed
*/
public function get($key){
$key = $this->_formatKey($key);
return $this->getCache()->get($key);
}
/**
* @param array $key
* @return mixed
*/
public function mget(array $keys){
$hashkeys = [];
foreach($keys as $values){
$hashkeys[$this->_formatKey($values)] = $values;
}
unset($keys);
$keyValues = $this->getCache()->get(array_keys($hashkeys));
$data = [];
foreach ($keyValues as $haskey => $value) {
$data[$hashkeys[$haskey]] = $value;
}
return $data;
}
/**
* @param $keys
* @param int $delay
* @return mixed
*/
public function delete($keys,$delay=0){
if(is_array($keys)){
$hashkeys = array_map(function ($value){
return $this->_formatKey($value);
},$keys);
}else{
$hashkeys = $this->_formatKey($keys);
}
return $this->getCache()->delete($hashkeys,$delay);
}
/**
* 所有缓存都失效
* @return mixed
*/
public function flush(){
return $this->getCache()->flush();
}
/**
* @return mixed
*/
public function info(){
return $this->getCache()->info();
}
}
下边开始调用缓存:
创建一个testYac.php文件
$cache = new Cache();
$cache->prefix = ‘test’;//定义一个key
$cache->set(‘key’,‘value’,0);
c
a
c
h
e
−
>
m
s
e
t
(
[
′
u
s
e
r
N
a
m
e
′
=
>
t
i
m
e
(
)
,
′
a
g
e
′
=
>
t
i
m
e
(
)
′
s
e
x
′
=
>
t
i
m
e
(
)
]
)
;
/
/
打
印
v
a
r
d
u
m
p
(
cache->mset([ 'userName'=>time(), 'age'=>time()%50, 'sex'=>time()%2 ]); //打印 var_dump(
cache−>mset([′userName′=>time(),′age′=>time()′sex′=>time()]);//打印vardump(cache->get(‘userName’));
var_dump(
c
a
c
h
e
−
>
g
e
t
(
′
a
g
e
′
)
)
;
v
a
r
d
u
m
p
(
cache->get('age')); var_dump(
cache−>get(′age′));vardump(cache->get(‘sex’));
var_dump(
c
a
c
h
e
−
>
g
e
t
(
′
k
e
y
′
)
)
;
v
a
r
d
u
m
p
(
cache->get('key')); var_dump(
cache−>get(′key′));vardump(cache->info());//获取所有的缓存
var_dump($cache->flush());//说有的缓存都失效
有想学习的同学们可以观看此视频:https://bbs.csdn.net/topics/392562522 https://edu.csdn.net/course/play/9933