| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- namespace PhpLife\Frame\Package\Counter;
- /**
- * 计数器类,使用hash数据结构
- * redis连不上server会抛出异常!!
- */
- class Hash extends \PhpLife\Frame\Package\Counter
- {
- const CONFIG_CLASS = 'Redis\\Counter';
- const PREFIX = '';
- const EXPIRE = 2592000; // 30 天
- protected static $hashKeyList = array();
- /**
- * 批量获取一系列的id
- * @param $idList
- * @return array
- */
- public static function hBatchGetAll($idList)
- {
- $data = array();
- foreach ($idList as $id) {
- $data[$id] = self::hGetAll($id);
- }
- return $data;
- }
- /**
- * 读取单个hash
- * @param $id
- * @return array
- */
- public static function hGetAll($id)
- {
- $key = self::getKey($id);
- try {
- $result = self::getConnection()->hMGet($key, self::getHashKeyList());
- foreach ($result as $k => $v) {
- $result[$k] = intval($v);
- }
- } catch (\Exception $ex) {
- $result = null;
- }
- return $result;
- }
- /**
- * @param $id
- * @param $hashKey
- * @param int $value
- * @return bool
- */
- public static function hSet($id, $hashKey, $value)
- {
- $key = self::getKey($id);
- try {
- $result = self::getConnection(true)->hSet($key, $hashKey, $value);
- } catch (\Exception $ex) {
- $result = false;
- }
- return $result;
- }
- /**
- * 加1
- * @param $id
- * @param $hashKey
- * @param int $value 负数时减少
- * @return int
- */
- public static function hIncrBy($id, $hashKey, $value = 1)
- {
- $key = self::getKey($id);
- try {
- $result = self::getConnection(true)->hIncrBy($key, $hashKey, $value);
- } catch (\Exception $ex) {
- $result = false;
- }
- return $result;
- }
- /**
- * 删除
- * @param $id
- * @return bool
- */
- public static function delete($id)
- {
- $key = self::getKey($id);
- try {
- $result = self::getConnection(true)->delete($key);
- } catch (\Exception $ex) {
- $result = false;
- }
- return $result;
- }
- /**
- * 设置过期时间
- * @param $id
- * @param $expire
- * @return mixed
- */
- public static function setExpire($id, $expire = null)
- {
- if (is_null($expire)) {
- $class = get_called_class();
- $expire = $class::EXPIRE;
- } else {
- $expire = intval($expire);
- }
- $key = self::getKey($id);
- try {
- $result = self::getConnection(true)->expire($key, $expire);
- } catch (\Exception $ex) {
- $result = false;
- }
- return $result;
- }
- /**
- * 获取hashKey列表
- * @return mixed
- */
- protected static function getHashKeyList()
- {
- $class = get_called_class();
- $hashKeyList = $class::$hashKeyList;
- return $hashKeyList;
- }
- /**
- * 获取连接池
- * @param bool $isMaster
- * @return \Redis
- */
- protected static function getConnection($isMaster = false)
- {
- $class = get_called_class();
- $configClass = $class::CONFIG_CLASS;
- $redis = new \PhpLife\Frame\Library\Redis($configClass);
- $connection = $redis->getConnection($isMaster);
- return $connection;
- }
- }
|