2018-12-21 页面优化技术-对象缓存、更新密码

1.对象缓存

getById、getBytoken

//对象缓存  由id获得user  放入redis
    public MiaoshaUser getById(long id) {
        MiaoshaUser miaoshaUser = redisService.get(MiaoshaUserKey.getById, ""+id, MiaoshaUser.class);
        if (miaoshaUser!=null) {
            return miaoshaUser;
        }
        miaoshaUser=miaoshaUserDao.selectById(id);
        if(miaoshaUser!=null) {
            redisService.set(MiaoshaUserKey.getById, ""+id, MiaoshaUser.class);
        }
        return miaoshaUser;
    }
    //对象缓存  由token获得user  放入redis
        public  MiaoshaUser getByToken(HttpServletResponse response,String token) {
            if(StringUtils.isEmpty(token)) {
                return null;
            }       
            MiaoshaUser user=redisService.get(MiaoshaUserKey.getByToken, token, MiaoshaUser.class);
            if(user!=null) {
                addCookie(response,token,user);
            }
            return user;
        }

1.更新密码

 //更新密码
    public boolean updatePassword(String token,String formPassNew,long id) {
        MiaoshaUser userById = getById(id);
        if(userById==null) {
            throw new GlobalException(CodeMsg.USER_NOT_EXIST);
        }
        //更新数据库
        MiaoshaUser toBeUpdate=new MiaoshaUser();
        toBeUpdate.setPassword(MD5Util.WebPassToDB(formPassNew, userById.getSalt()));
        miaoshaUserDao.update(toBeUpdate);
        //更新缓存   根据token和id放入redis中的user缓存都要更新
        redisService.del(MiaoshaUserKey.getById, ""+id);
        userById.setPassword(toBeUpdate.getPassword());
        redisService.set(MiaoshaUserKey.getByToken, token,userById);
        return true;
    }

1.MiaoshaUserDao

    @Update("upadte miaosha_user set password=#{password} where id =#{id}")
    public int update(MiaoshaUser toBeUpdate);

1.删除redis的key

//删除redis的key
        public Boolean del(KeyPrefix keyPrefix,String key) {
            Jedis jedis=null;
            try {
                 jedis = jedisPool.getResource();
                String realKey=keyPrefix.getPrefix()+key;
                Long del = jedis.del(realKey);
                return del>0;
            } finally {
                returnToPool(jedis);
            }
        }

1.对象缓存

```#1.对象缓存
###    

你可能感兴趣的:(2018-12-21 页面优化技术-对象缓存、更新密码)