yac概述

本文介绍了PHP扩展Yac(Yet Another Cache),用于进程间共享数据的缓存系统。详细阐述了Yac的安装步骤,包括从PECL下载、解压、编译、安装及配置php.ini。通过示例展示了如何创建YacCache类以及使用Yac进行缓存操作,如设置、获取、删除和查看缓存信息。此外,提供了学习资源链接。
摘要由CSDN通过智能技术生成

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 ( [ ′

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值