编程开源技术交流,分享技术与知识

网站首页 > 开源技术 正文

thinkphp5安装predis组件(thinkphp插件)

wxchong 2024-07-26 22:46:56 开源技术 27 ℃ 0 评论

前提是你先安装好redis

1.使用composer安装

请参考这个网址

https://packagist.org/packages/predis/predis

在tp5项目根目录下命令行执行以下命令(前提:你得安装好composer,如果你不知道啥是composer,那我得打你屁股)

composer require predis/predis

2.去万能的github找

https://github.com/nrk/predis

3.修改Application想目录下的config.php文件

4.使用单例模式封装Predis操作类

<?php
namespace app\index\service;

//单例模式
class MyRedisService
{
 private static $instance;
 
 //防止被实例化
 private function __construct()
 {
 require config('predis')['include_path'];
 $config = array(
 'host' => config("predis")['host'],//IP
 'port' => config('predis')['port'], //端口
 'database' => config('predis')['database'],
 //此处的密码必须设置
 //redis设置密码 config set requirepass 123456
 'password' => config('predis')['password'] //密码
 );
 
 self::$instance = new \Predis\Client($config);
 }
 
 //防止被克隆
 private function __clone(){}
 
 static function getInstance()
 {
 if(empty(self::$instance) || !isset(self::$instance))
 {
 $obj = new self();
 }
 return self::$instance;
 }
}
  1. 调用执行

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表