MerchantCache.Class.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * 商户cache添加
  4. * Created by PhpStorm.
  5. * User: haoren
  6. * Date: 2020/04/09
  7. * Time: 16: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 MerchantCache
  14. {
  15. private $cache;
  16. private $enterpriseId;
  17. private $userCenterId;
  18. private $cacheKey = 'Merchant';
  19. public function __construct($enterpriseId = false, $userCenterId = false)
  20. {
  21. $this->enterpriseId = $enterpriseId;
  22. $this->userCenterId = $userCenterId;
  23. $this->cache = Factory::cache('mapping');
  24. }
  25. /**---------------------------------------------商户------------------------------------------------**/
  26. /**
  27. * 添加
  28. * @param $merchantId
  29. * @param $merchantData
  30. * @return bool
  31. */
  32. public function addMerchant($merchantId, $merchantData)
  33. {
  34. $data = json_encode($merchantData);
  35. $result = $this->cache->hset($this->cacheKey, $merchantId, $data);
  36. if(!$result){
  37. return false;
  38. }
  39. return true;
  40. }
  41. /**
  42. * 查询
  43. * @param $merchantId
  44. * @param $key
  45. * @return array
  46. */
  47. public function getMerchant($merchantId, $key = false)
  48. {
  49. $result = $this->cache->hget($this->cacheKey, $merchantId);
  50. if (!$result) return [];
  51. $returnData = json_decode($result, true);
  52. if($key){
  53. return $returnData[$key];
  54. }
  55. return $returnData;
  56. }
  57. /**
  58. * 删除
  59. * @param $merchantId
  60. */
  61. public function delMerchant($merchantId = false)
  62. {
  63. if($merchantId){
  64. $this->cache->hdel($this->cacheKey, $merchantId);
  65. }else{
  66. $this->cache->del($this->cacheKey);
  67. }
  68. }
  69. }