zoukankan      html  css  js  c++  java
  • php 用redis实现购物车底层代码

    <?php
    
    class Red {
    
        static private $redis = NULL;
    
        private $_red = NULL;
    
        private $_return_data = NULL;
    
        static public function create() {
            if(self::$redis) {
                return Red::$redis;
            }
    
            self::$redis = new self;
            return self::$redis;
        }
    
        public function __call($func, $params) {
            if ($func == 'multi') {
                $this->_return_data = $this->_red->multi($params[0]);
    
            } else {
                $this->_return_data = call_user_func_array(array(&$this->_red, $func), $params);
    
            }
            return $this->_return_data;
        }
    
        private function __construct() {
            $this->_red = new Redis();
            $this->_red->connect(C("REDIS_HOST"),C("REDIS_PORT"));
            $this->_red->select(C('REDIS_DB') ?: 0);
            return Red::$redis;
        }
    
        public function ttl($key)
        {
            $new_key = C('REDIS_PREV') . $key;
            return $this->_red->ttl($new_key);
        }
    
        public function del($key)
        {
            $new_key = C('REDIS_PREV') . $key;
            return $this->_red->del($new_key);
        }
    
        public function delete($key)
        {
            $new_key = C('REDIS_PREV') . $key;
            return $this->_red->delete($new_key);
        }
    
        public function rename($key1, $key2)
        {
            $new_key1 = C('REDIS_PREV') . $key1;
            $new_key2 = C('REDIS_PREV') . $key2;
            return $this->_red->rename($new_key1, $new_key2);
        }
    
        public function exists($key)
        {
            $new_key = C('REDIS_PREV') . $key;
            return $this->_red->exists($new_key);
        }
    
        public function setex($key, $time ,$value)
        {
            $new_key = C('REDIS_PREV') . $key;
            return $this->_red->setex($new_key, $time, $value);
        }
    
        public function set($key, $value , $time = '')
        {
            $new_key = C('REDIS_PREV') . $key;
            if (is_numeric($time) && $time > 0) {
                return $this->_red->set($new_key, $value , $time);
            } else {
                return $this->_red->set($new_key, $value);
            }
        }
        public function get($key)
        {
            $new_key = C('REDIS_PREV') . $key;
            $a = $this->_red->get($new_key);
            return $a;
        }
    }
    
    <?php
    class CartModel
    {
        protected $redis;
        public function __construct()
        {
            vendor('Redis.Red');
            $this->redis = Red::create();
        }
    
        public function add($key, $id, $count = 1)
        {
            $key = C('REDIS_PREV') . $key;
            return $this->redis->hIncrBy($key, $id, $count);
        }
    
        public function addEx($key, array $data)
        {
            $key = C('REDIS_PREV') . $key;
            $r = $this->redis->multi(Redis::PIPELINE);
            foreach ($data as $k => $v) {
                $r = $r->hIncrBy($key, $k, $v);
                
            }
            return $this->redis->exec();
        }
    
        public function delete($key, $id)
        {
            $key = C('REDIS_PREV') . $key;
            $id = $this->redis->hdel($key, $id);
            return $id;
        }
    
        public function deleteEx($key, $ids)
        {
            $key = C('REDIS_PREV') . $key;
            foreach ($ids as $k => $v) {
                $this->redis->hdel($key, $v);
            }
    
            return true;
        }
    
        public function exists($key, $id)
        {
            $key = C('REDIS_PREV') . $key;
            return $this->redis->hExists($key, $id);
        }
    
        public function deleteAll($key)
        {
            $key = C('REDIS_PREV') . $key;
    
            $log = [];
            $log['token'] = $key;
            $log['time'] = date('Y-m-d H:i:s');
            $log['type'] = 'cart_model_deleteAll';
            file_put_contents('/tmp/yh.del.token.log', print_r($log, true), FILE_APPEND);
    
            return $this->redis->del($key);
        }
    
        public function count($key)
        {
            $key = C('REDIS_PREV') . $key;
            return $this->redis->hLen($key);
        }
    
        public function merge($guest, $uid)
        {
            if (!$this->hasGuestCart($guest)) {
                return true;
            }
    
            if (!$this->hasUserCart($uid)) {
                $this->redis->rename(C('GUEST.CART') . $guest, C('USER.CART') . $uid);
                return true;
            }
    
            $guestCart = $this->redis->hgetall(C('GUEST.CART') . $guest);
            $userCart = $this->redis->hgetall(C('USER.CART') . $uid);
    
            foreach ($guestCart as $k => $v) {
                if (isset($userCart[$k])) {
                    $userCart[$k] += $v;
                } else {
                    $userCart[$k] = $v;
                }
            }
    
            $this->deleteAll(C('GUEST.CART') . $guest);
            $this->deleteAll(C('USER.CART') . $uid);
    
            return $this->redis->hMset(C('USER.CART') . $uid, $userCart);
        
        }
    
        public function hasGuestCart($guest)
        {
            return $this->count(C('GUEST.CART').$guest);
        }
    
        public function hasUserCart($uid)
        {
            return $this->count(C('USER.CART').$uid);
        }
    
        public function setCount($key, $id, $count)
        {
            $key = C('REDIS_PREV') . $key;
            $status = $this->redis->hset($key, $id, $count);
            if ($status == -1) {
                return false;
            }
            return true;
        }
    
        public function getCount($key, $id)
        {
            $key = C('REDIS_PREV') . $key;
            return $this->redis->hget($key, $id);
        }
    
        public function get($key)
        {
            $key = C('REDIS_PREV') . $key;
            return $this->redis->hgetall($key);
        }
    }
    //end
    
    
    相信坚持的力量,日复一日的习惯.
  • 相关阅读:
    看完这篇文章,你还会问陈景润证明“1+2”有什么意义吗?
    stm32串口发送数据复位 第一个数据丢失
    无理数的由来
    定义一个大数组时,出现错误,程序进入HardFault_Handler中断
    STM32使用FatFs
    块级元素IE6/IE7下inline-block解决方案
    Building Your First jQuery Plugin
    ub挂载window磁盘
    PE文件结构部分解析以及输入的定位
    私有云建设之超融合技术
  • 原文地址:https://www.cnblogs.com/pansidong/p/15766227.html
Copyright © 2011-2022 走看看