123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- namespace App;
- use Illuminate\Support\Facades\Redis;
- class RedisModel
- {
- 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 del( $keys ){
- return Redis::del( ...(array) $keys);
- }
- public static function incr( $key ){
- return Redis::incr($key);
- }
- public static function setex( $key, $sec, $data){
- return Redis::setex($key, $sec, $data);
- }
- public static function exists( $key ){
- return Redis::exists($key);
- }
- /**
- * 从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);
- }
- public static function keys($pattern)
- {
- return Redis::keys($pattern);
- }
- // -------------------------------- 列表 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);
- }
- /**
- * 返回列表指定范围内的元素
- * @param $key
- * @param int $start
- * @param int $end
- * @return array
- */
- public static function lRange($key, $start = 0, $end = -1)
- {
- return Redis::lRange($key, $start, $end);
- }
- public static function lLen($key)
- {
- return Redis::llen($key);
- }
- public static function rPopLpush($srcKey, $dstKey)
- {
- return Redis::rpoplpush($srcKey, $dstKey);
- }
- // -------------------------------- 集合 Set
- /**
- * # SADD key member1 [member2]
- *
- * 向集合添加一个或多个成员
- *
- * @param $key
- * @param $members
- * @return int 成功添加数量
- */
- public static function sAdd($key, $members)
- {
- // php >= 5.6
- return (int) Redis::sadd($key, ...(array) $members);
- // 通用写法
- // $params = array_merge([$key], (array) $members);
- // return (int) call_user_func_array([$redis, 'sadd'], $params);
- }
- // -------------------------------- 有序集合 Sorted Set
- public static function zAdd($key, $fieldScoreKv)
- {
- $params = [];
- foreach ($fieldScoreKv as $field => $score) {
- $params[] = $score;
- $params[] = $field;
- }
- return Redis::zAdd($key, ... $params);
- }
- public static function zRem($key, $members)
- {
- return Redis::zRem($key, ...(array) $members);
- }
- /**
- * 返回指定元素的分数
- * @param $key
- * @param $member
- * @return float
- */
- public static function zScore($key, $member)
- {
- return Redis::zScore($key, $member);
- }
- // -------------------------------- Hash
- 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 hDel($key, $hashKeys)
- {
- return Redis::hDel($key, ...(array) $hashKeys);
- }
- public static function hLen($key)
- {
- return Redis::hLen($key);
- }
- public static function hSet($key, $hashkey, $value)
- {
- return Redis::hSet($key, $hashkey, $value);
- }
- public static function hGet($key, $hashkey)
- {
- return Redis::hGet($key, $hashkey);
- }
- public static function hExists($key, $hashkey)
- {
- return Redis::hExists($key, $hashkey);
- }
- public static function hIncrby ($key, $hashkey, $num)
- {
- return Redis::hIncrby($key, $hashkey, $num);
- }
- }
|