123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace components\service;
- use Yii;
- /**
- * redis(方便统计队列)
- * Class Redis
- * @package components\service
- */
- class Redis
- {
- const KEYS_STATUS = 'statistic:status';
- const TYPE_STATUS = 'status';
- const TYPE_RESULT = 'result';
- /**
- * [设置缓存]
- * @author: libingke
- * @param $queue
- * @param $mid
- * @param $type
- * @param $val
- * @return mixed
- */
- public static function set($queue, $mid, $type, $val)
- {
- $key = static::getKey($queue, $mid, $type);
- if ($type == static::TYPE_STATUS)
- Yii::$app->redis->hmset(static::KEYS_STATUS, $mid, $val);//统计状态
- return Yii::$app->redis->set($key, $val);
- }
- /**
- * [获取状态keys]
- * @author: libingke
- * @return mixed
- */
- public static function getStatusList()
- {
- return Yii::$app->redis->hgetall(static::KEYS_STATUS);
- }
- /**
- * [获取缓存]
- * @author: libingke
- * @param $queue
- * @param $mid
- * @param $type
- * @return mixed
- */
- public static function get($queue, $mid, $type)
- {
- $key = static::getKey($queue, $mid, $type);
- return Yii::$app->redis->get($key);
- }
- /**
- * [删除缓存]
- * @author: libingke
- * @param $queue
- * @param $mid
- * @param $type
- * @return mixed
- */
- public static function del($queue, $mid, $type)
- {
- $key = static::getKey($queue, $mid, $type);
- return Yii::$app->redis->del($key);
- }
- /**
- * [失效设置]
- * @author: libingke
- * @param $queue
- * @param $mid
- * @param $type
- * @param int $expiration
- * @return mixed
- */
- public static function expire($queue, $mid, $type, $expiration = 86400 * 7)
- {
- $key = static::getKey($queue, $mid, $type);
- return Yii::$app->redis->expire($key, $expiration);
- }
- /**
- * [获取key]
- * @author: libingke
- * @param $queue
- * @param $mid
- * @param $type
- * @return string
- * @throws \Exception
- */
- public static function getKey($queue, $mid, $type)
- {
- if ( $queue && $mid && in_array($type, [static::TYPE_STATUS, static::TYPE_RESULT]) )
- return "queue:$queue:$mid:$type";
- throw new \Exception("参数设置错误");
- }
- /**
- * purge 清空消息
- * @author: libingke
- * @param $queue
- */
- public static function purge($queue)
- {
- }
- /**
- * 批量删除 key
- * @author: libingke
- * @param $queue
- * @param array $delete
- * @param $type
- */
- public static function batchDel($queue, $delete, $type)
- {
- }
- }
|