安阳网站自然优化,营销模式方案,做散热网站,160 作者 网站建设这篇文章主要介绍了Thinkphp6 配置并使用redis的方法,结合实例形式详细分析了Redis的安装、配置以及thinkphp6操作Redis的基本技巧,需要的朋友可以参考下 一、安装redis
ThinkPHP内置支持的缓存类型包括file、memcache、wincache、sqlite。ThinkPHP默认使用自带的采用think\Ca… 这篇文章主要介绍了Thinkphp6 配置并使用redis的方法,结合实例形式详细分析了Redis的安装、配置以及thinkphp6操作Redis的基本技巧,需要的朋友可以参考下 一、安装redis
ThinkPHP内置支持的缓存类型包括file、memcache、wincache、sqlite。ThinkPHP默认使用自带的采用think\Cache类。
可以在小皮的软件管理中安装redis比较简单快捷也可以先参考安装redis文章 1下载php redis扩展.打开phpinfo 查看对应的信息
php添加redis扩展 根据以下链接查找符合php版本的扩展 注意是否为线性nts-表示否
redis: http://windows.php.net/downloads/pecl/releases/redis/
igbinary: http://windows.php.net/downloads/pecl/releases/igbinary/ 下载完后将php_igbinary.dll 和 php_redis.dll 复制进php ext 文件下面 2进入当前所使用的版本php目录下找到php的配置文件php.ini并配置如下参数 extensionphp_redis.dll 若不配置此项可能报错 不支持redis
3保存重启服务器 二、在thinkphp6中配置redis
进入config/cache.php 配置添加redis缓存
?php
// ----------------------------------------------------------------------
// | 缓存设置
// ----------------------------------------------------------------------
return [// 默认缓存驱动default env(cache.driver, file),// 缓存连接方式配置stores [file [// 驱动方式type File,// 缓存保存目录path ,// 缓存前缀prefix ,// 缓存有效期 0表示永久缓存expire 0,// 缓存标签前缀tag_prefix tag:,// 序列化机制 例如 [serialize, unserialize]serialize [],],// 配置Reidsredis [type redis,host 127.0.0.1,port 6379,password ,select 0,// 全局缓存有效期0为永久有效expire 0,// 缓存前缀prefix ,//默认缓存周期timeout 3600,],],
]; 三、在TP6框架中简单使用redis
1在方法中使用 set、get
use think\cache\driver\Redis; //需要使用到的类
use think\facade\Cache;
use think\facade\Config;
public function test()
{$redis new Redis(Config::get(cache.stores.redis));$redis-set(key,value1);echo $redis-get(key);
} 至此一些基本的操作我们可以在
\vendor\topthink\framework\src\think\cache\Driver.php 文件中找到redis的一些基本操作但是不够
执行高级方法需要使用句柄如下
2在方法中使用lpushrpushllenlrange等方法进行模拟压栈后查看不要在意直接操作了栈底的位置主要为了展示使用redis-list
。
public function test(){$redis Cache::store(redis)-handler(); //返回句柄对象可执行其它高级方法$redis-lpush(arr,我是第一个入栈);//比作压栈的方式从列表的左侧插入$redis-lpush(arr,我是第二个入栈);$redis-rpush(arr,我从栈底挤一挤);$redis-lpushx(arr,我应该最先被取出); //若arr是一个空列表则什么都不做echo 取出前的长.$redis-llen(arr).
;echo $redis-lpop(arr).
; //从最左边将值从栈顶取出之后删掉echo 取出后的长.$redis-llen(arr).
;echo 整个列表查看;dd($redis-lrange($redis-keys(arr)[0], 0 ,-1));//尽量使用var_dump(),此句后面的语句将不被执行我只是为了好看$redis-del(arr); //删除当前数据对象string、list、hash..}
运行结果 四、报错信息
错误1 $this-handler-connect($this-options[host], (int) $this-options[port], (int) $this-options[timeout]); 解决办法注意一定要启动redis