redis 生成订单号学习

题目是生成明天的订单号,删除昨天的订单号,在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

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();
        }
    }

创作思路:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值