123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- namespace App\Models\Redis;
- use Illuminate\Support\Facades\Redis;
- /**
- * @see \Predis\ClientInterface
- */
- class RedisModel
- {
- public static function keys($pattern)
- {
- return Redis::keys($pattern);
- }
- public static function exists($key)
- {
- return Redis::exists($key);
- }
- // -------------------------------- 字符串 string
- public static function get($key){
- return Redis::get($key);
- }
- public static function set($key, $data){
- return Redis::set($key, $data);
- }
- public static function expire($key, $time = 60){
- return Redis::expire($key, $time);
- }
- public static function setnx($key, $data){
- return Redis::setnx($key, $data);
- }
- public static function setex($key, $time, $data)
- {
- return Redis::setex($key, $time, $data);
- }
- public static function del($keys){
- return Redis::del(...(array) $keys);
- }
- // -------------------------------- 列表 List
- public static function lPush($key, $values)
- {
- return Redis::lPush($key, ...(array) $values);
- }
- public static function rPush($key, $values)
- {
- return Redis::rPush($key, ...(array) $values);
- }
- public static function rPop($key)
- {
- return Redis::rPop($key);
- }
- public static function brPop($key, $timeout = 0)
- {
- return Redis::brPop($key, $timeout);
- }
- public static function lPop($key)
- {
- return Redis::lPop($key);
- }
- public static function lIndex($key, $index)
- {
- return Redis::lIndex($key, $index);
- }
- public static function lRange($key, $start = 0, $end = -1)
- {
- return Redis::lRange($key, $start, $end);
- }
- public static function lLen($key)
- {
- return Redis::lLen($key);
- }
- // -------------------------------- Hash
- public static function hSet($key, $field, $value)
- {
- return Redis::hSet($key, $field, $value);
- }
- public static function hMset($key, $hashKvs)
- {
- return Redis::hMset($key, $hashKvs);
- }
- public static function hMget($key, $hashKeys)
- {
- return Redis::hMget($key, (array) $hashKeys);
- }
- public static function hGetAll($key)
- {
- return Redis::hGetAll($key);
- }
- public static function hGet($key, $hashkey)
- {
- return Redis::hGet($key, $hashkey);
- }
- public static function hDel($key, $hashKeys)
- {
- return Redis::hDel($key, ...(array) $hashKeys);
- }
- public static function hLen($key)
- {
- return Redis::hLen($key);
- }
- public static function hSetnx($key, $field, $val)
- {
- return Redis::hSetnx($key, $field, $val);
- }
- public static function hIncrby($key, $field, $val)
- {
- return Redis::hIncrby($key, $field, $val);
- }
- /**
- * 从redis中取出数据并进行反序列化
- * @param $key
- * @return mixed
- */
- public static function getAfterDecode($key)
- {
- $res = static::get($key);
- return json_decode($res, true);
- }
- /**
- * 将数据序列化后存入redis
- * @param $key
- * @param $data
- * @param int $timeout
- * @return bool
- */
- public static function setAfterEncode($key, $data, $timeout = 0)
- {
- $data = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
- Redis::setnx($key, $timeout);
- return Redis::set($key, $data);
- }
- }
|