1. 在宝塔环境下可直接进行安装2. 安装完成后需要进入指定的php环境下进行安装memcached扩展3. 在thinkphp下进行自定义封装Memcached类
<?php
namespace app\lib\cache;
// 封装memcached类
use think\Config;
class Memcached
{
private static $_instance = null;
public function __construct()
{
return self::getInstance();
}
public static function getInstance()
{
$option = [
'host' => Config::get('cache.memcached_host') ?? '127.0.0.1',
'port' => Config::get('cache.memcached_port') ?? 11211,
];
if (is_null(self::$_instance)) {
self::$_instance = new \Memcached();
self::$_instance->addServer($option['host'], $option['port']);
}
return self::$_instance;
}
/**
* @param $key 键
* @param $value 值
* @param int $expire 有效期
* @return bool
*/
public static function set($key, $value, $expire = 0)
{
return self::getInstance()->set($key, $value, $expire);
}
/**
* 查询缓存值,单个或数组
* @param $key 键
* @return mixed
*/
public static function get($key)
{
$func = is_array($key) ? 'getMulti' : 'get';
return self::getInstance()->{$func}($key);
}
/**
* 自增
* @param $key
* @param int $number
* @return bool
*/
public static function incr($key, $number = 1)
{
return self::getInstance()->increment($key, $number);
}
/**
* 自减
* @param $key
* @param int $number
* @return int
*/
public static function decr($key, $number = 1)
{
return self::getInstance()->decrement($key,$number);
}
/**
* 删除
* @param $key
* @return bool
*/
public static function delete($key){
return self::getInstance()->delete($key);
}
}