ThinkPHP 中使用Redis
环境.env
[app]
app_debug = "1"
app_trace = ""
[database]
database = ""
hostname = "127.0.0.1"
hostport = ""
password = ""
prefix = "ls_"
username = ""
[redis]
hostname = "127.0.0.1"
hostport = "6379"
password = "123456"
prefix = "redis_"
[project]
env_name = ""
file_domain = "xxx.xxx.xxx.xxx"
配置 config
<?php
// +----------------------------------------------------------------------
// | 缓存设置
// +----------------------------------------------------------------------
use think\facade\Env;
return [
// 缓存配置为复合类型
'type' => 'complex',
'default' => [
'type' => Env::get('cache.type','File'),
// 全局缓存有效期(0为永久有效)
'expire'=> 0,
// 缓存前缀
'prefix'=> 'shop_',
// 缓存目录
'path' => '',
],
'redis' => [
'type' => 'redis',
'host' => Env::get('redis.hostname'),
'port' => Env::get('redis.hostport'),
'password' => Env::get('redis.password'),
'expire'=> 0,
'prefix'=> 'redis_', // 缓存前缀
],
// 添加更多的缓存类型设置
];
Redis缓存处理类
<?php
// +----------------------------------------------------------------------
// | Redis缓存处理类
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// | author: 007
// +----------------------------------------------------------------------
namespace app\common\logic;
use think\facade\Env;
use think\facade\Cache;
class RedisLogic
{
private $rc = null; //连接实例
protected $module = ''; // 模块标识
public function __construct($module = '')
{
$this->rc = Cache::store('redis');
$this->module = $module;
}
public function getkeys($key)
{
return $this->rc->keys($key);
}
public function setCache($key, $val)
{
if($this->module) $key = $this->module.":".$key;
return $this->rc->set($key, $val);
}
public function getCache($key)
{
if($this->module) $key = $this->module.":".$key;
return $this->rc->get($key);
}
public function delete($key)
{
if($this->module) $key = $this->module.":".$key;
$prefix = Env::get('redis.prefix','');
$key = $prefix.$key;
return $this->rc->del($key);
}
/**
* 删除指定key的缓存
*
* 若 `$key===true` 则表示删除全部
*
* // 支持下面的方式
* $this->delete('abc');
* $this->delete('abc', 'abc2');
* $this->delete(['abc', 'abc2']);
*
* // 删除全部
* $this->delete(true);
* // 也可使用
* $this->delete_all();
*
* @param string|array|true $key
*/
public function delCache($key)
{
// $this->_connect();
if (true === $key) {
return $this->rc->flushAll();
} else {
if (is_array($key)) {
$keys = $key;
} else {
$keys = func_get_args();
}
return $this->rc->del($keys);
}
}
public function hGetAllCache($key)
{
if($this->module) $hash= $this->module.":".$key;
return $this->rc->hGetAll($hash);
}
// 判断hash表中字段是否存在,存在返回true,失败返回false
public function hExistsCache($key, $field)
{
if($this->module) $hash= $this->module.":".$key;
return $this->rc->hExists($hash, $field);
}
public function hSetCache($key, $field, $val)
{
if($this->module) $hash= $this->module.":".$key;
return $this->rc->hSet($hash, $field, $val);
}
public function hGetCache($key, $field)
{
if($this->module) $hash= $this->module.":".$key;
return $this->rc->hGet($hash, $field);
}
public function hMgetCache($key, $fields)
{
// $fields = ['name', 'age']
if($this->module) $hash= $this->module.":".$key;
return $this->rc->hMget($hash, $fields);
}
public function hMsetCache($key, $entry)
{
// $entry = ['name' => 'jet', 'age' => 18]
if($this->module) $hash= $this->module.":".$key;
return $this->rc->hMset($hash, $entry);
}
public function hIncrByCache($key, $field, $add)
{
if($this->module) $hash= $this->module.":".$key;
return $this->rc->hIncrBy($hash, $field, $add);
}
public function hIncrByFloatCache($key, $field, $add)
{
if($this->module) $hash= $this->module.":".$key;
return $this->rc->hIncrByFloat($hash, $field, $add);
}
}
?>
控制器中使用
<?php
namespace app\data\controller;
use app\common\logic\RedisLogic;
class Test extends Base
{
public function redis()
{
if(class_exists('Redis')){
echo '<br>服务器支持Redis服务<br>';
$redis = new RedisLogic();
$redis->setCache('key',12365478);
echo '<br>key:',$redis->getCache('key');
echo $redis->delete('key');
}
else{
echo '服务器不支持Redis服务';
}
}
}
使用redis的场景和对应示例代码https://www.cnblogs.com/liuxinyustu/articles/13504257.html