• 在库存服务中实现缓存与数据库双写一致性保障方案(二)


    封装请求接口,第一个实现类,data数据更新的操作,比如一个商品发生了交易,就要修改商品对应的库存了,此时就
    data update request,数据更新请求
    1.删除缓存
    2.更新数据库

    实体类:

    package com.roncoo.eshop.entity;
    
    /**
     * 库存数量model
     * @author Administrator
     *
     */
    public class ProductInventory {
    
    	/**
    	 * 商品id
    	 */
    	private Integer productId;
    	/**
    	 * 库存数量
    	 */
    	private Long inventoryCnt;
    
    	public ProductInventory() {
    
    	}
    
    	public ProductInventory(Integer productId, Long inventoryCnt) {
    		this.productId = productId;
    		this.inventoryCnt = inventoryCnt;
    	}
    
    	public Integer getProductId() {
    		return productId;
    	}
    	public void setProductId(Integer productId) {
    		this.productId = productId;
    	}
    	public Long getInventoryCnt() {
    		return inventoryCnt;
    	}
    	public void setInventoryCnt(Long inventoryCnt) {
    		this.inventoryCnt = inventoryCnt;
    	}
    
    }
    

      mapper:

    package com.roncoo.eshop.dao;
    
    import com.roncoo.eshop.entity.ProductInventory;
    import org.apache.ibatis.annotations.Param;
    
    public interface ProductInventoryMapper {
    
        /**
         * 更新库存数量
         * @param productInventory 商品库存
         */
        void updateProductInventory(ProductInventory productInventory);
    
        /**
         * 根据商品id查询商品库存信息
         * @param productId 商品id
         * @return 商品库存信息
         */
        ProductInventory findProductInventory(@Param("productId") Integer productId);
    }
    

      xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.roncoo.eshop.dao.ProductInventoryMapper">
    
      	<update id="updateProductInventory" parameterType="com.roncoo.eshop.entity.ProductInventory">
    		update product_inventory set inventory_cnt=#{inventoryCnt} where product_id=#{productId}
      	</update>
    
      	<select id="findProductInventory" resultType="com.roncoo.eshop.entity.ProductInventory">
      		select product_id as "productId",inventory_cnt as "inventoryCnt" from
      		 product_inventory where product_id=#{productId}
      	</select>
    
    </mapper>
    

      sql:

    create table product_inventory
    (
        product_id    int auto_increment
            primary key,
        inventory_cnt bigint(255) null
    );
    

      

    public interface RedisDAO {
        void set(String key, String value);
    
        String get(String key);
    
        void delete(String key);
    }
    

      

    @Repository("redisDAO")
    public class RedisDAOImpl implements RedisDAO {
        @Resource
        private JedisCluster jedisCluster;
    
        @Override
        public void set(String key, String value) {
            jedisCluster.set(key, value);
        }
    
        @Override
        public String get(String key) {
            return jedisCluster.get(key);
        }
    
        @Override
        public void delete(String key) {
            jedisCluster.del(key);
        }
    
    }
    

      

    /**
     * 商品库存Service
     */
    public interface ProductInventoryService {
        /**
         * 查询商品库存
         * @param productId 商品id
         * @return
         */
        ProductInventory findProductInventory(Integer productId);
    
        /**
         * 更新商品库存到缓存中
         * @param productInventory
         */
        void setProductInventoryCache(ProductInventory productInventory);
    
        /**
         * 删除缓存
         * @param productInventory
         */
        void removeProductInventoryCache(ProductInventory productInventory);
    
        /**
         * 更新数据库的库存
         * @param productInventory
         */
        void updateProductInventory(ProductInventory productInventory);
    }
    

      

    @Service
    public class ProductInventoryServiceImpl implements ProductInventoryService {
    
        @Resource
        private ProductInventoryMapper productInventoryMapper;
    
        @Resource
        private RedisDAO redisDAO;
    
        /***
         *
         * @param productId 商品id
         * @return
         */
        @Override
        public ProductInventory findProductInventory(Integer productId) {
            return productInventoryMapper.findProductInventory(productId);
        }
    
        @Override
        public void setProductInventoryCache(ProductInventory productInventory) {
            String key = "product:inventory:" + productInventory.getProductId();
            redisDAO.set(key, String.valueOf(productInventory.getInventoryCnt()));
        }
    
        @Override
        public void removeProductInventoryCache(ProductInventory productInventory) {
            String key = "product:inventory:" + productInventory.getProductId();
            redisDAO.delete(key);
        }
    
        @Override
        public void updateProductInventory(ProductInventory productInventory) {
            productInventoryMapper.updateProductInventory(productInventory);
        }
    }
    

      

    package com.roncoo.eshop.req;
    
    import com.roncoo.eshop.entity.ProductInventory;
    import com.roncoo.eshop.service.ProductInventoryService;
    
    /**
     * 删除缓存修改数据库
     */
    public class DataUpdateRequest implements Request{
    
    
        private ProductInventory productInventory;
        /**
         * 商品库存Service
         */
        private ProductInventoryService productInventoryService;
    
        public DataUpdateRequest(ProductInventory productInventory, ProductInventoryService productInventoryService) {
            this.productInventory = productInventory;
            this.productInventoryService = productInventoryService;
        }
    
        /**
         * 删除redis中的缓存
         * 修改数据库中的库存
         */
        @Override
        public void process(){
            productInventoryService.removeProductInventoryCache(productInventory);
            productInventoryService.updateProductInventory(productInventory);
        }
    }
    

      

    package com.roncoo.eshop.req;
    
    import com.roncoo.eshop.entity.ProductInventory;
    import com.roncoo.eshop.service.ProductInventoryService;
    
    /**
     * 重新加载商品库存的缓存
     * 读缓存读空,需要发送一个更新缓存的请求
     * @author Administrator
     *
     */
    public class ProductInventoryCacheRefreshRequest implements Request {
        /**
         * 商品id
         */
        private Integer productId;
        /**
         * 商品库存Service
         */
        private ProductInventoryService productInventoryService;
        public ProductInventoryCacheRefreshRequest(Integer productId,
                                                   ProductInventoryService productInventoryService) {
            this.productId = productId;
            this.productInventoryService = productInventoryService;
        }
    
        /**
         *  从数据库中查询最新的商品库存数量
         * 将最新的商品库存数量,刷新到redis缓存中去
         */
        @Override
        public void process() {
            ProductInventory productInventory = productInventoryService.findProductInventory(productId);
    
            productInventoryService.setProductInventoryCache(productInventory);
        }
    }
    

      

  • 相关阅读:
    HDU 5883 F
    关于codeblock 为什么不能调试
    Codeforces Round #378 (Div. 2) D. Kostya the Sculptor 分组 + 贪心
    51NOD 区间的价值 V2
    NYOJ 42 一笔画问题
    如何对二维字符数组进行排序
    hihoCoder 1383 : The Book List 北京网络赛
    利用IDA学习一个简单的安卓脱壳
    iOS APP可执行文件的组成
    Mac10.11 搭建php开发环境
  • 原文地址:https://www.cnblogs.com/q1359720840/p/15864991.html
Copyright © 2020-2023  润新知