CartCache.Class.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * 购物车缓存
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2019/11/7
  7. * Time: 15:16
  8. */
  9. namespace JinDouYun\Cache;
  10. use Mall\Framework\Factory;
  11. class CartCache
  12. {
  13. private $cache;
  14. private $enterpriseId; //当前企业id
  15. private static $cartTable;//缓存购物车
  16. /**
  17. * CartCache constructor.
  18. * @param string $cacheDb
  19. * @param $enterpriseId
  20. * @throws \Exception
  21. */
  22. public function __construct($enterpriseId, $cacheDb = 'mapping')
  23. {
  24. $this->enterpriseId = $enterpriseId;
  25. self::$cartTable = 'cartTable' . '::' . $this->enterpriseId;
  26. $this->cache = Factory::cache($cacheDb);
  27. }
  28. /**
  29. * 购物车数据缓存
  30. * @param $params
  31. * @param $userId
  32. * @return mixed
  33. */
  34. public function cacheCartTable($params, $userId)
  35. {
  36. //$data = gzcompress(json_encode($params));
  37. $data = json_encode($params);//测试时用这个
  38. $result = $this->cache->hset(self::$cartTable, $userId, $data);
  39. return $result;
  40. }
  41. /**
  42. * 获取用户购物车数据
  43. * @param $userId
  44. * @return array|mixed
  45. */
  46. public function getCart($userId)
  47. {
  48. $result = $this->cache->hget(self::$cartTable,$userId);
  49. if (!$result) {
  50. return [];
  51. }
  52. if (!empty($result)){
  53. $result = json_decode($result, true);
  54. }
  55. return $result;
  56. }
  57. /**
  58. * 清空用户购物车
  59. * @param $userId
  60. * @return mixed
  61. */
  62. public function clearCart($userId)
  63. {
  64. $result = $this->cache->hdel(self::$cartTable,$userId);
  65. return $result;
  66. }
  67. }