在之前的 项目 里继续往下写
- 在 Lib/Cache/LibMemcached.php 里创建 LibMemcached 这个类来作为 Memcached 的封装
- 与之前封装文件缓存一样,实现 CacheInterface.php 接口类中的所有方法,从而达到规范调用缓存的目的
LibMemcached.php
<?php
/*
LibMemcached 类
*/
namespace Lib\Cache;
use Lib\Cache\Interfaces\CacheInterface;
class LibMemcached implements CacheInterface{
/**
* @var \Memcached
*/
protected $memcached; // 定义 memcached 实例
protected $preFix = 'asdf';
protected $persistentId = 'ASDF';
protected $servers = [
'192.168.1.214', 11211
];
protected $options; // 当前配置项
public function __construct($options){
if (!is_array($options)){
$options = [];
}
if (!isset($options['servers']) || empty($options['servers'])){
$options['servers'][] = $this->servers;
}
$this->options = $options;
$this->_connect();
}
/**
* 实例化 Memcached
*/
public function _connect(){
$options = $this->options;
$persistentId = isset($options['persistent_id']) ? $options['persistent_id'] : $this->persistentId;
$memcached = new \Memcached($persistentId);
if (!$memcached->getServerList()){
if (!is_array($options['servers']) || empty($options['servers'])){
throw new \Exception('servers must be an array');
}
$memcached->addServers($options['servers']);
}
//配置
$preFix = isset($options['pre_fix']) ? $options['pre_fix'] : $this->preFix;
$memcached->setOptions([
\Memcached::OPT_PREFIX_KEY => $preFix,
\Memcached::OPT_COMPRESSION => false
]
);
$this->memcached = $memcached;
}
/**
* 设置缓存
* @param $key
* @param $value
* @param $ttl
* @return mixed
*/
public function save($key, $value, $ttl = 3600){
if (!$key || !$value) return false;
$ttl += $_SERVER['REQUEST_TIME']; //当前时间戳
return $this->memcached->set($key, $value, $ttl);
}
/**
* 获取缓存
* @param $key
* @return mixed
*/
public function get($key){
if(!$key) return false;
return $this->memcached->get($key);
}
/**
* 删除缓存
* @param $key
* @return mixed
*/
public function delete($key){
if(!$key) return false;
return $this->memcached->delete($key);
}
/**
* 自增操作
* @param $key
* @param $value
* @return mixed
*/
public function increment($key, $value = 1)
{
if(!$key) return false;
return $this->memcached->increment($key, $value);
}
/**
* 自减操作
* @param $key
* @param $value
* @return mixed
*/
public function decrement($key, $value = 1)
{
if(!$key) return false;
return $this->memcached->decrement($key, $value);
}
/**
* 清空缓存
* @return mixed
*/
public function flush(){
return $this->memcached->flush();
}
/**
* Memcache add操作
* @param $key
* @param $value
* @param $ttl
* @return bool
*/
public function add($key, $value, $ttl){
if (!$key || !$value) return false;
$ttl += $_SERVER['REQUEST_TIME'];
return $this->memcached->add($key, $value, $ttl);
}
/**
* 获取 Memcached 类变量,通过类变量调用没有被封装的方法。
* @return \Memcached
*/
public function getMemcached(){
return $this->memcached;
}
}
然后在 index.php 里就可以调用了!
//调用libMemcached
$cache = Cache::Factory('libMemcached',[
'pre_fix' => 'asdf',
'servers' => [
['192.168.1.214', 11211, 50],
['192.168.1.214', 11311, 50]
]
]);
//
var_dump($cache->save('k1','v1',800));
var_dump($cache->get('k1'));
//
$memcached = $cache->getMemcached();
$ttl = $_SERVER['REQUEST_TIME'] + 50;
//hello memcached
$memcached->set('k1', ' ', $ttl);
$memcached->prepend('k1', 'hello');
$memcached->append('k1', 'memcached');
$cas = Memcached::GET_EXTENDED; // PHP7 写法
var_dump($memcached->get('k1', null, $cas), $cas);