123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- /**
- * 通用cache添加
- * Created by PhpStorm.
- * User: 小威
- * Date: 2019/12/13
- * Time: 17:00
- */
- namespace JinDouYun\Cache;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Factory;
- class CacheMan
- {
- private $cache;
- private $enterpriseId;
- private $userCenterId;
- private static $cacheKey = '';
- public function __construct($enterpriseId, $userCenterId = false)
- {
- $this->enterpriseId = $enterpriseId;
- $this->userCenterId = $userCenterId;
- $this->cache = Factory::cache('mapping');
- }
- /**---------------------------------------------哈希hash------------------------------------------------**/
- /**
- * 添加
- * @param $key
- * @param $value
- * @param $cacheKey
- * @return ResultWrapper
- */
- public function addCacheHash($key,$value, $cacheKey)
- {
- $data = json_encode($value);
- $result = $this->cache->hset($cacheKey . '::' . $this->enterpriseId, $key, $data);
- if (!$result) {
- return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
- }
- return ResultWrapper::success('success');
- }
- /**
- * 查询
- * @param $key
- * @param $cacheKey
- * @param bool $type
- * @return mixed|string
- */
- public function getCacheHash($key, $cacheKey, $type = false)
- {
- $result = $this->cache->hget($cacheKey.'::'.$this->enterpriseId, $key);
- if (!$result) return '';
- $returnData = json_decode($result, true);
- if($type){
- return $returnData[$type];
- }
- return $returnData;
- }
- /**
- * 删除
- * @param $key
- * @param $cacheKey
- */
- public function delCacheHash($key, $cacheKey)
- {
- $this->cache->hdel($cacheKey.'::'.$this->enterpriseId, $key);
- }
- /***********************************************有序集合sorted set************************************************/
- /**
- * 添加
- * @param $value $缓存的字段名 value
- * @param $score $缓存的值 score
- * @param $cacheKey
- * @return mixed
- */
- public function createCacheStatistics($value,$score,$cacheKey)
- {
- $result = $this->cache->zadd($cacheKey.'::'.$this->enterpriseId, $score, $value);
- if ($result == false){
- return false;
- }
- return $result;
- }
- /**
- * 获取
- * @param $value $字段名 value
- * @param $cacheKey
- * @return mixed
- */
- public function getCacheSortedSet($value,$cacheKey)
- {
- $result = $this->cache->ZSCORE($cacheKey.'::'.$this->enterpriseId,$value);
- return $result;
- }
- /**
- * 获取key的个数
- * @param $cacheKey
- * @return mixed
- */
- public function getCacheKeySortedSetCount($cacheKey)
- {
- $result = $this->cache->ZCARD($cacheKey.'::'.$this->enterpriseId);
- return $result;
- }
- /**
- * 删除
- * @param $cacheKey
- * @return mixed
- */
- public function deleteCacheKey($cacheKey)
- {
- $result = $this->cache->del($cacheKey.'::'.$this->enterpriseId);
- return $result;
- }
- }
|