redis 生成订单号学习

版权声明:本文为博主原创烂文章,你爱转载就转载 https://blog.csdn.net/qq_38623459/article/details/83347672

题目是生成明天的订单号,删除昨天的订单号,在redis操作,订单号暂定规则为年月日接五位数。如2018102200001

当一个订单生成的时候, 去redis去取订单号,去玩后删掉。这个取和删除操作是一个命令发过去的。不能写成两个语句。

参考博客:

redis五种结构详解:

https://www.cnblogs.com/sdgf/p/6244937.html

redis五种结构详解:

https://blog.csdn.net/kiss199203/article/details/73549091

redis设置list的过期时间

http://lionlx.iteye.com/blog/1746683

redis存储list

扫描二维码关注公众号,回复: 4461132 查看本文章

https://blog.csdn.net/Kincym/article/details/72676813

最终代码:

  /**
     * 明天的订单号
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/ordergen",produces = "application/json;charset=UTF-8")
    public String ordergen(){
        JSONObject jsonObject = new JSONObject();
        try{
        	//用于存储的订单列表
        	//List<String> ordernumlist = new ArrayList<String>();
        	//准备前部分订单号码
        	Calendar calendar = Calendar.getInstance();  
            calendar.setTime(new Date()); 
            calendar.add(Calendar.DAY_OF_MONTH, +1);//明天
             SimpleDateFormat sdf =  new SimpleDateFormat("yyyyMMdd"); 
        	String str_q = sdf.format(calendar.getTime());
        	//准备后部分订单号码
        	for(int i=1;i<=9;i++){
        		String str_h = String.valueOf(i);
        		String strmax ="00000";
        		str_h=strmax.substring(0, 5-str_h.length())+str_h;
        		String str_qh = str_q + str_h;
        		//redis存储一个list,从头开始,往后加
        		redisClient.rpush(str_q,str_qh);
        	}
        	//明天的订单号,将从今天开始存在48小时(今天只能取到今天的key,时间一到list自动没了)
        	redisClient.expire(str_q, 172800);
        	
        	jsonObject.put("code", ResponseStatusCode.SUCC.getValue());
        	return jsonObject.toJSONString();
        }catch(Exception e){
        	e.printStackTrace();
			jsonObject.put("code", ResponseStatusCode.FAIL.getValue());
			return jsonObject.toJSONString();
        }
    }
    /**
     * 获取并且删除订单号
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/ordertake",produces = "application/json;charset=UTF-8")
    public String ordertake(){
        JSONObject jsonObject = new JSONObject();
        try{
        	SimpleDateFormat sdf =  new SimpleDateFormat("yyyyMMdd"); 
        	String str_q = sdf.format(new Date());
        	String ordernow  = redisClient.lpop(str_q);
        	jsonObject.put("code", ResponseStatusCode.SUCC.getValue());
        	jsonObject.put("ordernum", ordernow);
        	return jsonObject.toJSONString();
        }catch(Exception e){
        	e.printStackTrace();
			jsonObject.put("code", ResponseStatusCode.FAIL.getValue());
			return jsonObject.toJSONString();
        }
    }

创作思路:

猜你喜欢

转载自blog.csdn.net/qq_38623459/article/details/83347672
今日推荐