zoukankan      html  css  js  c++  java
  • PHP中封装Redis购物车功能

    <?php
    // 服务层
    
    namespace CommonService;
    use VendorFuncRed;
    
    class CartService extends CommonService {
        protected $redis;
        protected $pre_key;
        public function __construct()
        {
            parent::__construct();
            $this->redis = Red::create();
            $this->pre_key = C('USER.CART').C('APPID').':';
        }
    
        /**
         * 加入购物车,移除购物车,但是不会删除
         * @param $openid
         * @param $sku_id
         * @param int $count
         * @return mixed
         */
        public function add($openid, $sku_id, $count = 1)
        {
            $key = $this->pre_key.$openid;
            // 可增可减
            return $this->redis->hIncrBy($key, $sku_id, $count);
        }
    
        /**
         * 批量添加
         * @param $openid
         * @param array $data
         * @return mixed
         */
        public function addBatch($openid, array $data)
        {
            $key = $this->pre_key.$openid;
            // 批量执行
            $r = $this->redis->multi(Redis::PIPELINE);
            foreach ($data as $k => $v) {
                $r = $r->hIncrBy($key, $k, $v);
            }
            return $this->redis->exec();
        }
    
        /**
         * 删除购物车单个商品
         * @param $openid
         * @param $sku_id
         * @return mixed
         */
        public function delete($openid, $sku_id)
        {
            $key = $this->pre_key.$openid;
            return $this->redis->hdel($key, $sku_id);
        }
    
        /**
         * 删除购物车多个商品
         * @param $openid
         * @param $sku_ids
         * @return bool
         */
        public function deleteBatch($openid, $sku_ids)
        {
            $key = $this->pre_key.$openid;
            foreach ($sku_ids as $k => $v) {
                $this->redis->hdel($key, $v);
            }
    
            return true;
        }
    
        /**
         * 检测商品是否已在购物车中
         * @param $openid
         * @param $sku_id
         * @return mixed
         */
        public function exists($openid, $sku_id)
        {
            $key = $this->pre_key.$openid;
            return $this->redis->hExists($key, $sku_id);
        }
    
        /**
         * 清空购物车
         * @param $openid
         * @return mixed
         */
        public function deleteAll($openid)
        {
            $key = $this->pre_key.$openid;
            return $this->redis->del($key);
        }
    
        /**
         * 判断购物车中是否有数据,有多少
         * @param $openid
         * @return mixed
         */
        public function hasUserCart($openid)
        {
            $key = $this->pre_key.$openid;
            return $this->redis->hLen($key);
        }
    
        /**
         * 设置为固定数量
         * @param $openid
         * @param $sku_id
         * @param $count
         * @return bool
         */
        public function setCount($openid, $sku_id, $count)
        {
            $key = $this->pre_key.$openid;
            $status = $this->redis->hset($key, $sku_id, $count);
            if ((int)$status === -1) {
                return false;
            }
            return true;
        }
    
        /**
         * 获取购物车中单个商品的数量
         * @param $openid
         * @param $sku_id
         * @return mixed
         */
        public function getCount($openid, $sku_id)
        {
            $key = $this->pre_key.$openid;
            return $this->redis->hget($key, $sku_id);
        }
    
        /**
         * 获取全部数据
         * @param $openid
         * @return mixed
         */
        public function getAll($openid)
        {
            $key = $this->pre_key.$openid;
            return $this->redis->hgetall($key);
        }
    
        /**
         * 获取全部商品id
         * @param $openid
         * @return mixed
         */
        public function getAllKeys($openid)
        {
            $key = $this->pre_key.$openid;
            return $this->redis->hkeys($key);
        }
    
        /**
         * 获取全部商品数量
         * @param $openid
         * @return mixed
         */
        public function getAllVal($openid)
        {
            $key = $this->pre_key.$openid;
            return $this->redis->hvals($key);
        }
    }
    

    加入购物车,移除购物车,清空购物车,查看购物车数量,查看全部商品等等。

  • 相关阅读:
    centos7安装docker
    spring成神之路第四十三篇:spring 中编程式事务怎么用的?
    spring成神之路第四十二篇:玩转 JdbcTemplate
    spring成神之路第四十一篇:@EnableCaching 集成 redis 缓存
    spring成神之路第四十篇:缓存使用(@EnableCaching、@Cacheable、@CachePut、@CacheEvict、@Caching、@CacheConfig)
    spring成神之路第三十八篇:@Scheduled & @EnableScheduling 定时器详解
    spring成神之路第三十七篇:@EnableAsync & @Async 实现方法异步调用
    spring成神之路第三十六篇:@EnableAspectJAutoProxy、@Aspect 中通知顺序详解
    spring成神之路第三十五篇:@Aspect 中 5 中通知详解
    spring成神之路第三十四篇:@Aspect 中@Pointcut 12 种用法
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/10580223.html
Copyright © 2011-2022 走看看