方式1
1、使用redis,首先你的php.ini支持运行redis。一般需要的扩充这个功能。
显示状态是这样,就能支持运行redis
2、在extend文件夹下新建一个redis文件夹,并创一个RedisPackage.php的类,入下图所示:
完整代码:
<?php
// +----------------------------------------------------------------------
// | Description: redis 缓存封装使用
// +----------------------------------------------------------------------
// | Author: php
// +----------------------------------------------------------------------
namespace Redis;
class RedisPackage
{
protected static $handler = null;
protected $options = [
'host' => '127.0.0.1',
'port' => 6379,
'password' => '123456',
'select' => 0,
'timeout' => 0,//关闭时间 0:代表不关闭
'expire' => 0,
'persistent' => false,
'prefix' => '',
];
public function __construct($options = [])
{
if (!extension_loaded('redis')) { //判断是否有扩展(如果你的apache没reids扩展就会抛出这个异常)
throw new \BadFunctionCallException('not support: redis');
}
if (!empty($options)) {
$this->options = array_merge($this->options, $options);
}
$func = $this->options['persistent'] ? 'pconnect' : 'connect'; //判断是否长连接
self::$handler = new \Redis;
self::$handler->$func($this->options['host'], $this->options['port'], $this->options['timeout']);
if ('' != $this->options['password']) {
self::$handler->auth($this->options['password']);
}
if (0 != $this->options['select']) {
self::$handler->select($this->options['select']);
}
}
/**
* 写入缓存
* @param string $key 键名
* @param string $value 键值
* @param int $exprie 过期时间 0:永不过期
* @return bool
*/
public static function set($key, $value, $exprie = 0)
{
if ($exprie == 0) {
$set = self::$handler->set($key, $value);
} else {
$set = self::$handler->setex($key, $exprie, $value);
}
return $set;
}
/**
* 读取缓存
* @param string $key 键值
* @return mixed
*/
public static function get($key)
{
$fun = is_array($key) ? 'Mget' : 'get';
return self::$handler->{$fun}($key);
}
/**
* 获取值长度
* @param string $key
* @return int
*/
public static function lLen($key)
{
return self::$handler->lLen($key);
}
/**
* 将一个或多个值插入到列表头部
* @param $key
* @param $value
* @return int
*/
public static function LPush($key, $value, $value2 = null, $valueN = null)
{
return self::$handler->lPush($key, $value, $value2, $valueN);
}
/**
* 移出并获取列表的第一个元素
* @param string $key
* @return string
*/
public static function lPop($key)
{
return self::$handler->lPop($key);
}
}
3、测试是否可行
完整代码测试
<?php
// +----------------------------------------------------------------------
// | Description: redis 缓存使用
// +----------------------------------------------------------------------
// | Author: php
// +----------------------------------------------------------------------
namespace app\index\controller;
use think\Controller;
use redis\RedisPackage;
class Redis extends Controller
{
/**
* 测试存redis缓存 - 取redis缓存使用
* @param RedisPackage $redisPackage
*/
public function index(){
$redis= new RedisPackage();
$redis::set('foo','my name is xie');
print_r($redis::get('foo'));die;
}
}
运行结果:
方式2
ThinkPHP采用think\Cache
类提供缓存功能支持。
同时使用多个缓存类型
如果要同时使用多个缓存类型进行操作的话,可以做如下配置:
'cache' => [
// 使用复合缓存类型
'type' => 'complex',
// 默认使用的缓存
'default' => [
// 驱动方式
'type' => 'File',
// 缓存保存目录
'path' => CACHE_PATH,
],
// 文件缓存
'file' => [
// 驱动方式
'type' => 'file',
// 设置不同的缓存保存目录
'path' => RUNTIME_PATH . 'file/',
],
// redis缓存
'redis' => [
// 驱动方式
'type' => 'redis',
// 服务器地址
'host' => '127.0.0.1',
'port' =>6379,//端口
'password' => '123456',//123456你自己设置的redis的密码
],
],
配置好,怎么使用呢?这是大家比较担心的问题
完整代码
<?php
// +----------------------------------------------------------------------
// | Description: redis 缓存使用
// +----------------------------------------------------------------------
// | Author: php
// +----------------------------------------------------------------------
namespace app\index\controller;
use think\Controller;
use think\Cache;
class Redis extends Controller
{
/**
* 测试存redis缓存 - 取redis缓存使用
* @param RedisPackage $redisPackage
*/
public function index(){
Cache::store('redis')->set('cesh','value');
print_r(Cache::store('redis')->get('cesh'));die;
}
}
方式2比较方式1 简单多的,因为tp5.0以上都封装好了,写配置就能用的了。到这里是不是有种想打人的想法。这么简单,还说方式1繁琐的写法。以为方式比较源起的redis的封装方式,这样比较容易了解redis的工作原理
以上方式,觉得那个可以帮助你的就用那个的。由君选择。