SpecCache.Class.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace JinDouYun\Cache;
  3. use Mall\Framework\Core\ErrorCode;
  4. use Mall\Framework\Core\ResultWrapper;
  5. use Mall\Framework\Factory;
  6. /**
  7. * 规格缓存
  8. * Class SpecCache
  9. * @package JinDouYun\Cache
  10. */
  11. class SpecCache
  12. {
  13. /**
  14. * 企业ID
  15. * @var
  16. */
  17. private $onlineEnterpriseId;
  18. /**
  19. * redis
  20. * @var
  21. */
  22. private $cache;
  23. /**
  24. * SpecName=>id
  25. * @var
  26. */
  27. private $specNameRelIdKey = 'SpecNameRelId';
  28. /**
  29. * SpecCache constructor.
  30. * @param $enterpriseId
  31. * @throws \Exception
  32. */
  33. public function __construct($enterpriseId)
  34. {
  35. $this->onlineEnterpriseId = $enterpriseId;
  36. $this->cache = Factory::cache('mapping');
  37. }
  38. /**
  39. * SpecName => id
  40. * @param $specName
  41. * @param $id
  42. * @return ResultWrapper
  43. */
  44. public function cacheSpecNameRelId($specName, $id)
  45. {
  46. return $this->cache->zadd($this->specNameRelIdKey.'::'.$this->onlineEnterpriseId, $id, $specName);
  47. }
  48. /**
  49. * del
  50. * @param $id
  51. * @return mixed
  52. */
  53. public function delSpecNameRelId($id)
  54. {
  55. return $this->cache->zremrangebyscore($this->specNameRelIdKey.'::'.$this->onlineEnterpriseId, $id,$id);
  56. }
  57. /**
  58. * get id => specName
  59. * @param $id
  60. * @return string
  61. */
  62. public function getSpecNameById($id)
  63. {
  64. $result = $this->cache->zrangebyscore($this->specNameRelIdKey.'::'.$this->onlineEnterpriseId, $id, $id);
  65. if (!$result) {
  66. return '';
  67. }
  68. return array_shift($result);
  69. }
  70. /**
  71. * get specName => id
  72. * @param $specName
  73. * @return int
  74. */
  75. public function getIdBySpecName($specName)
  76. {
  77. $result = $this->cache->zscore($this->specNameRelIdKey.'::'.$this->onlineEnterpriseId, $specName);
  78. if (!$result) return 0;
  79. return $result;
  80. }
  81. }