CacheMan.Class.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * 通用cache添加
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2019/12/13
  7. * Time: 17:00
  8. */
  9. namespace JinDouYun\Cache;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use Mall\Framework\Factory;
  13. class CacheMan
  14. {
  15. private $cache;
  16. private $enterpriseId;
  17. private $userCenterId;
  18. private static $cacheKey = '';
  19. public function __construct($enterpriseId, $userCenterId = false)
  20. {
  21. $this->enterpriseId = $enterpriseId;
  22. $this->userCenterId = $userCenterId;
  23. $this->cache = Factory::cache('mapping');
  24. }
  25. /**---------------------------------------------哈希hash------------------------------------------------**/
  26. /**
  27. * 添加
  28. * @param $key
  29. * @param $value
  30. * @param $cacheKey
  31. * @return ResultWrapper
  32. */
  33. public function addCacheHash($key,$value, $cacheKey)
  34. {
  35. $data = json_encode($value);
  36. $result = $this->cache->hset($cacheKey . '::' . $this->enterpriseId, $key, $data);
  37. if (!$result) {
  38. return ResultWrapper::fail('error', ErrorCode::$redisWriteError);
  39. }
  40. return ResultWrapper::success('success');
  41. }
  42. /**
  43. * 查询
  44. * @param $key
  45. * @param $cacheKey
  46. * @param bool $type
  47. * @return mixed|string
  48. */
  49. public function getCacheHash($key, $cacheKey, $type = false)
  50. {
  51. $result = $this->cache->hget($cacheKey.'::'.$this->enterpriseId, $key);
  52. if (!$result) return '';
  53. $returnData = json_decode($result, true);
  54. if($type){
  55. return $returnData[$type];
  56. }
  57. return $returnData;
  58. }
  59. /**
  60. * 删除
  61. * @param $key
  62. * @param $cacheKey
  63. */
  64. public function delCacheHash($key, $cacheKey)
  65. {
  66. $this->cache->hdel($cacheKey.'::'.$this->enterpriseId, $key);
  67. }
  68. /***********************************************有序集合sorted set************************************************/
  69. /**
  70. * 添加
  71. * @param $value $缓存的字段名 value
  72. * @param $score $缓存的值 score
  73. * @param $cacheKey
  74. * @return mixed
  75. */
  76. public function createCacheStatistics($value,$score,$cacheKey)
  77. {
  78. $result = $this->cache->zadd($cacheKey.'::'.$this->enterpriseId, $score, $value);
  79. if ($result == false){
  80. return false;
  81. }
  82. return $result;
  83. }
  84. /**
  85. * 获取
  86. * @param $value $字段名 value
  87. * @param $cacheKey
  88. * @return mixed
  89. */
  90. public function getCacheSortedSet($value,$cacheKey)
  91. {
  92. $result = $this->cache->ZSCORE($cacheKey.'::'.$this->enterpriseId,$value);
  93. return $result;
  94. }
  95. /**
  96. * 获取key的个数
  97. * @param $cacheKey
  98. * @return mixed
  99. */
  100. public function getCacheKeySortedSetCount($cacheKey)
  101. {
  102. $result = $this->cache->ZCARD($cacheKey.'::'.$this->enterpriseId);
  103. return $result;
  104. }
  105. /**
  106. * 删除
  107. * @param $cacheKey
  108. * @return mixed
  109. */
  110. public function deleteCacheKey($cacheKey)
  111. {
  112. $result = $this->cache->del($cacheKey.'::'.$this->enterpriseId);
  113. return $result;
  114. }
  115. }