Java秒杀系统-2.6-分布式session

1.UUID

package com.zengjx.miaosha.utils;

import java.util.UUID;

public class UUIDUtil {
	public static String uuid() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	public static void main(String[] args) {


		System.out.println(uuid());//abed44b03e914334a55acbdb3369fb6a
	}
}

2.生成Cookie,key就是token,通过redisService 存储到redis中,

package com.zengjx.miaosha.redis;

public abstract class BasePrefix implements KeyPrefix{
	
	private int expireSeconds;//保存在redis中持保存时间
	
	private String prefix;//主键---前缀
	
	public BasePrefix(String prefix) {//0代表永不过期
		this(0, prefix);
	}
	
	public BasePrefix(int expireSeconds, String prefix) {
		this.expireSeconds = expireSeconds;
		this.prefix = prefix;
	}
	
	public int expireSeconds() {//默认0代表永不过期
		return expireSeconds;
	}

	public String getPrefix() {
		String className = getClass().getSimpleName();
		return className+":" + prefix;
	}

}

定义MiaoshaUserKey,key保存时间为2天,前缀为“tk”

package com.zengjx.miaosha.redis;

/**
 * @ClassName HelloController
 * @Description TODO
 * @Author zengjx
 * @Company zengjx
 * @Date 2020/1/24  16:03
 * @Version V1.0
 */
public class MiaoshaUserKey  extends   BasePrefix {
    public static  final   int  TOKEN_EXPIRE=3600*24*2;
    public MiaoshaUserKey( int  expireSeconds,  String prefix) {
        super(expireSeconds,prefix);
    }
    public   static   MiaoshaUserKey  token=new MiaoshaUserKey(TOKEN_EXPIRE,"tk");
}

 3.MiaoshaService ,login 方法中,如果登录成功,生成UUID的token, 通过addCookie (HttpServletResponse ,String token,MishaUser  user)添加到Cookie.addUCookie调用redisService的set 方法保存user.

public  boolean set(KeyPrefix prefix, String key,  T value) 
创建“token ”为COOKI_NAME_TOKEN,value 为UUID 保存在Cookie.

MiaoshaUserServiceImpl 

 @Override
    public Boolean login(HttpServletResponse response,LoginVo loginVo) {
    Result  result  =new Result<>();
    //按照手机号查找用户
        if(loginVo.getMobile()==null){
            CodeMsg  codeMsg  =CodeMsg.MOBILE_NOT_EXIST;
            throw   new GlobalException(codeMsg);
        }
        if(loginVo.getMobile()==null){
            CodeMsg  codeMsg  =CodeMsg.PASSWORD_EMPTY;
            throw   new GlobalException(codeMsg);
        }

        Long aLong = Long.parseLong(loginVo.getMobile());
        logger.info("phone "+aLong);
        MiaoshaUser    miaoshaUser=    miaoshaUserDao.getById(aLong) ;
    // 密码校验

      if(miaoshaUser==null){
       CodeMsg  codeMsg  =CodeMsg.MOBILE_NOT_EXIST;
          throw   new GlobalException(codeMsg);
      }
        String password =loginVo.getPassword();
        if(!password.equals(miaoshaUser.getPassword())){
        CodeMsg  codeMsg =CodeMsg.PASSWORD_ERROR;
          throw   new GlobalException(codeMsg);
        }
      //生成cookie
      String   token= UUIDUtil.uuid();
      addCookie(response,token,miaoshaUser);
        return true;
    }

  生成UUID 并且保存在cookie 中

   redis 和cookie 设置都是

    private void addCookie(HttpServletResponse response, String token, MiaoshaUser miaoshaUser) {
    //1.user保存在redisService.set
    //2. 保存UUID  token 在Cookie,
    //3.设置cookie的路径为“/”
    //4.response添加cookie
        boolean set = redisService.set(MiaoshaUserKey.token, token, miaoshaUser);
        Cookie  cookie  =new Cookie(COOKI_NAME_TOKEN,token);
        cookie.setPath("/");
        cookie.setMaxAge(MiaoshaUserKey.TOKEN_EXPIRE);
        response.addCookie(cookie);
    }

 

你可能感兴趣的:(Java秒杀系统-2.6-分布式session)