123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- <?php
- namespace App\Servers;
- use Illuminate\Support\Facades\Redis;
- /**
- * Redis数据缓存类
- */
- class RedisDataServer
- {
- /**
- * 单列对象
- * @var
- */
- private static $server;
- private function __construct()
- {
- }
- /**
- * 创建对象
- * @return RedisDataServer
- */
- static function creatServer()
- {
- if (empty(self::$server)) {
- self::$server = new RedisDataServer();
- }
- return self::$server;
- }
- /**
- * 获取缓存数据
- * @param $key
- * @param string $type
- * @return mixed
- */
- function getData($key, $type = 'str')
- {
-
- $value = Redis::get($key);
- if ($type == 'json') {
- $value = json_decode($value, true);
- }
- return $value;
- }
- /**
- * 设置缓存数据
- * @param $key
- * @param $value
- * @param string $type
- * @param int $time
- * @param $random
- */
- function setData($key, $value, $type = 'str', $time = 10, $random = true)
- {
- if (!empty($value)) {
- if ($type == 'json') {
- $value = json_encode($value);
- }
- if ($time > 1 && $random) $time += $this->getRandom();
- Redis::setex($key, $time, $value);
- }
- }
- /**
- * 长时间存放数据
- * @param $key
- * @param $value
- * @param string $type
- */
- function setLongData($key, $value, $type = 'str')
- {
- if (!empty($value)) {
- if ($type == 'json') {
- $value = json_encode($value);
- }
- Redis::set($key, $value);
- }
- }
- /**
- * 更新数据
- * @param $key
- * @param $value
- * @param string $type
- * @param int $start_num
- */
- function setSetrange($key, $value, $type = 'str', $start_num = 0)
- {
- if (!empty($value)) {
- if ($type == 'json') {
- $value = json_encode($value);
- }
- Redis::setrange($key, $start_num, $value);
- }
- }
- /**
- * 删除Redis数据
- * @param $key
- */
- function delData($key)
- {
- Redis::del($key);
- }
- /**
- * 获取随机数
- * @return int
- */
- private function getRandom()
- {
- return mt_rand(1, 10);
- }
- /**
- * 创建队列序号
- * @param $key
- * @param $num
- * @return mixed
- */
- function setQueue($key, $num)
- {
- $num_arr = range(1, $num);
- $key_num = Redis::lpush($key, $num_arr);
- return $key_num;
- }
- /**
- * 添加序号
- * @param $key
- * @param $num
- * @return mixed
- */
- function addQueue($key, $num)
- {
- $key_num = Redis::lpush($key, $num);
- return $key_num;
- }
- /**
- * 弹出队列数据
- * @param $key
- * @return false
- */
- function getQueue($key)
- {
- $key_num = Redis::rpop($key);
- if (empty($key_num)) $key_num = false;
- return $key_num;
- }
- /**
- * 队列阻塞弹出
- * @param $key
- * @param int $wait_time
- * @return false
- */
- function getWaitQueue($key, $wait_time = 5)
- {
- $key_num = Redis::brpop($key, $wait_time);
- if (empty($key_num)) $key_num = false;
- return $key_num;
- }
- /**
- * 获取队列数据长度
- * @param $key
- * @return mixed
- */
- function getQueueList($key)
- {
- $key_num = Redis::llen($key);
- return $key_num;
- }
- /**
- * 获取过期时间
- * @param $key
- * @return mixed
- */
- function getTime($key)
- {
- $key_num = Redis::ttl($key);
- return $key_num;
- }
- /**
- * 缓存清除
- * @return mixed
- */
- function flushdb()
- {
- return Redis::flushdb();
- }
- /**
- * key加锁
- * @param $key
- * @param $value
- * @param int $time
- * @return mixed
- */
- function lockKey($key,$value,$time = 10){
- return Redis::set($key, $value, 'ex', $time, 'nx');
- }
- /**
- * 解锁
- * @param $key
- * @param $value
- * @return mixed
- */
- function unLockKey($key, $value)
- {
- $res = $this->getData($key);
- if ($res == $value) {
- return Redis::del($key);
- }
- }
- }
|