init: import production code mss.nahaichuan.com (exclude Runtime/Uploads/backups/logs)
这个提交包含在:
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
namespace Addons\Api;
|
||||
//[1.3.11]
|
||||
class RedisController
|
||||
{
|
||||
static $redis;
|
||||
|
||||
/**
|
||||
* [__construct]
|
||||
* @param [string] $local [地址]
|
||||
* @param [int] $port [端口]
|
||||
* @param [string] $pass [密码]
|
||||
* @param [int] $number [选择的数据库]
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
self::$redis = new \Redis();
|
||||
self::$redis->connect(C('REDIS_LOCAL'), C('REDIS_PORT')); // 连接Redis
|
||||
self::$redis->auth(C('REDIS_PASS')); // 密码验证
|
||||
self::$redis->select(C('REDIS_NUMBER')); // 选择数据库
|
||||
}
|
||||
|
||||
/**------------------------------ list --------------------------------------*/
|
||||
/**
|
||||
* [rPush 添加,从列表的尾部插入一条数据]
|
||||
* @param [string] $key [键]
|
||||
* @param [array] $value [值]
|
||||
* @param [int] $time [时间]
|
||||
* @return [bool|int]
|
||||
*/
|
||||
public function rPush($key, $value, $time = 0)
|
||||
{
|
||||
// 添加队列
|
||||
$result = self::$redis->rpush($key, $value);
|
||||
// 判断是否需要设置周期
|
||||
if ($time && intval($time) > 0){
|
||||
// 更新周期
|
||||
self::$redis->expire($key, $time);
|
||||
}
|
||||
// 更新周期
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* [lPop 获取,从列表的头部移除,获取一条数据]
|
||||
* @param [string] $key [键]
|
||||
* @return [bool|object]
|
||||
*/
|
||||
public function lPop($key)
|
||||
{
|
||||
$result = self::$redis->lpop($key);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* [total 获取列表数据]
|
||||
* @param [string] $key [键]
|
||||
* @param [string] $start_range [开始位置]
|
||||
* @param [string] $end_range [结束位置]
|
||||
* @return [bool|array]
|
||||
*/
|
||||
public function getData($key, $start_range = 0, $end_range = -1)
|
||||
{
|
||||
$result = self::$redis->lRange($key, $start_range, $end_range);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* [getLength 获取指定队列长度]
|
||||
* @param [string] $key [键]
|
||||
* @return [bool|int]
|
||||
*/
|
||||
public function getLength($key)
|
||||
{
|
||||
$result = self::$redis->lsize($key);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* [getStockNumber 下单未支付的订单 获取未支付的库存数量]
|
||||
* @param [string] $key [键]
|
||||
* @param [string] $str [元素]
|
||||
* @return [bool|int]
|
||||
*/
|
||||
public function getStockNumber($key, $str)
|
||||
{
|
||||
$keys = self::$redis->keys($key);
|
||||
$count = 0;
|
||||
foreach ($keys as &$val) {
|
||||
$data = self::$redis->lRange($val, 0, -1);
|
||||
$count += intval(json_decode($data[0], true)[$str]);
|
||||
unset($data);
|
||||
}
|
||||
return $count;
|
||||
}
|
||||
|
||||
/**
|
||||
* [forRPush 循环添加,从列表的尾部插入一条数据]
|
||||
* @param [string] $key [键]
|
||||
* @param [array] $value [值]
|
||||
* @param [int] $time [时间]
|
||||
* @return [bool|int]
|
||||
*/
|
||||
public function forRPush($key, $value, $time = 0)
|
||||
{
|
||||
// 定义长度
|
||||
for ($i = 0; $i < $value; $i++) {
|
||||
// 添加队列
|
||||
$result = self::$redis->rpush($key, 1);
|
||||
if (!$result) {
|
||||
self::$redis->removeKey($key);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// 判断是否需要设置周期
|
||||
if ($time && intval($time) > 0) {
|
||||
// 更新周期
|
||||
self::$redis->expire($key, $time);
|
||||
}
|
||||
// 更新周期
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* [forLPop 循环移除,从列表的头部移除一条数据]
|
||||
* @param [string] $key [键]
|
||||
* @param [array] $value [值]
|
||||
* @return [bool|int]
|
||||
*/
|
||||
public function forLPop($key, $value)
|
||||
{
|
||||
// 定义长度
|
||||
for ($i = 0; $i < $value; $i++) {
|
||||
// 添加队列
|
||||
$result = self::$redis->lpop($key);
|
||||
if(!$result){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**------------------------------ 公共 --------------------------------------*/
|
||||
/**
|
||||
* [existsKey 判断key是否存在]
|
||||
* @param [string] $key [键]
|
||||
* @return [bool|int] 存在返回1,不存在返回0
|
||||
*/
|
||||
public function existsKey($key)
|
||||
{
|
||||
$result = self::$redis->exists($key);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* [removeKey 移除指定key]
|
||||
* @param [string] $key [键]
|
||||
* @return [bool]
|
||||
*/
|
||||
public function removeKey($key)
|
||||
{
|
||||
$result = self::$redis->delete($key);
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户