Java Code Examples for redis.clients.jedis.JedisCommands#set()

The following examples show how to use redis.clients.jedis.JedisCommands#set() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Functions.java    From feiqu-opensource with Apache License 2.0 5 votes vote down vote up
public String currentBgImgUrl(int uid){
        if(uid <= 0){return "";}
        JedisCommands commands = JedisProviderFactory.getJedisCommands(null);
        try {
            String key = CacheManager.getUserBackImgKey(uid);
            String picUrl = commands.get(key);
            if(StringUtils.isEmpty(picUrl)){
                FqBackgroundImgService fqBackgroundImgService = SpringUtils.getBean("fqBackgroundImgServiceImpl");
                FqBackgroundImgExample example = new FqBackgroundImgExample();
                example.createCriteria().andUserIdEqualTo(uid).andDelFlagEqualTo(YesNoEnum.NO.getValue());
                FqBackgroundImg fqBackgroundImg = fqBackgroundImgService.selectFirstByExample(example);
                if(fqBackgroundImg != null){
                    picUrl = fqBackgroundImg.getImgUrl();
                }else {
                    picUrl = "null";
                }
                commands.set(key,picUrl);
                commands.expire(key,60*60*24);
                return picUrl;
            }else {
                return picUrl;
            }
        } finally{
            JedisProviderFactory.getJedisProvider(null).release();
        }
//        return CommonConstant.bgImgUrl;
    }
 
Example 2
Source File: CacheManager.java    From feiqu-opensource with Apache License 2.0 5 votes vote down vote up
public static void refreshUserCacheByUid(Integer userId){
    String cacheKey = "FqUser.id:"+userId;
    try {
        JedisCommands commands = JedisProviderFactory.getJedisCommands(null);
        FqUserService fqUserService = SpringUtils.getBean("fqUserServiceImpl");
        FqUser fqUser = fqUserService.selectByPrimaryKey(userId);
        FqUserCache fqUserCache = new FqUserCache(fqUser);
        commands.set(cacheKey,JSON.toJSONString(fqUserCache));
        commands.expire(cacheKey,300);
    } catch (Exception e) {
        logger.error("refreshUserCacheByUid",e);
    }finally {
        JedisProviderFactory.getJedisProvider(null).release();
    }
}
 
Example 3
Source File: CacheManager.java    From feiqu-opensource with Apache License 2.0 4 votes vote down vote up
/** 当本地缓存命没有中时,调用load方法获取结果并将结果缓存 **//*  
                .build(
                        new CacheLoader<Integer, com.cwd.boring.pojo.cache.FqUserCache>() {
                            public com.cwd.boring.pojo.cache.FqUserCache load(Integer key) throws Exception {
                                logger.info("load user " + key);
                                FqUserService fqUserService = (FqUserService) SpringContextUtil.getBean("fqUserServiceImpl");
                                FqUser fqUser = fqUserService.selectByPrimaryKey(key);
                                return new FqUserCache(fqUser);
                            }
                        }
                );*/

    public static FqUserCache getUserCacheByUid(Integer userId){
//        try {


        /*FqUserService fqUserService = SpringUtils.getBean("fqUserServiceImpl");
        FqUser fqUser = fqUserService.selectByPrimaryKey(userId);
        FqUserCache fqUserCache = new FqUserCache(fqUser);
        return fqUserCache;*/
        FqUserCache fqUserCache = null;
        try {
            JedisCommands commands = JedisProviderFactory.getJedisCommands(null);
            String cacheKey = "FqUser.id:"+userId;
            String userCacheStr = commands.get(cacheKey);
            if(StringUtils.isEmpty(userCacheStr)){
                FqUserService fqUserService = SpringUtils.getBean("fqUserServiceImpl");
                FqUser fqUser = fqUserService.selectByPrimaryKey(userId);
                fqUserCache = new FqUserCache(fqUser);
                commands.set(cacheKey,JSON.toJSONString(fqUserCache));
                commands.expire(cacheKey,300);
                return fqUserCache;
            }else {
                fqUserCache = JSON.parseObject(userCacheStr,FqUserCache.class);
                return fqUserCache;
            }
        } catch (Exception e) {
            logger.error("",e);
        }finally {
            JedisProviderFactory.getJedisProvider(null).release();
        }
        return fqUserCache;


        /*Level1CacheSupport level1CacheSupport = Level1CacheSupport.getInstance();

        FqUserCache level1FqUserCache = level1CacheSupport.get(cacheKey);
        if(level1FqUserCache == null){
            RedisObject redisUser = new RedisObject(cacheKey);
            fqUserCache = redisUser.get();
            if(fqUserCache == null){
                FqUserService fqUserService = (FqUserService) SpringContextUtil.getBean("fqUserServiceImpl");
                FqUser fqUser = fqUserService.selectByPrimaryKey(userId);
                fqUserCache = new FqUserCache(fqUser);
                redisUser.set(fqUserCache,300);
                level1CacheSupport.set(cacheKey,fqUserCache);
//                logger.info("一二缓存未查到,去数据库查到 user {}" , userId);
                return fqUserCache;
            }else {
//                logger.info("本地缓存未查到,redis查到 user {}" , userId);
                level1CacheSupport.set(cacheKey,fqUserCache);
                return fqUserCache;
            }
        }else {
//            logger.info("本地缓存查到 user {}" , userId);
            return level1FqUserCache;
        }*/


//            return userCache.get(userId);
//        } catch (ExecutionException e) {
//            logger.error("getUserCacheByUid :"+userId, e);
//        }
//        return null;
    }
 
Example 4
Source File: UserController.java    From feiqu-opensource with Apache License 2.0 4 votes vote down vote up
@GetMapping("/{uid}/home")
public String home(HttpServletRequest request, HttpServletResponse response,
                   @PathVariable Integer uid, @RequestParam(defaultValue = "1") Integer p, String order) {
    try {
        FqUserCache user = webUtil.currentUser(request, response);
        if(user == null){
            return login(CommonConstant.DOMAIN_URL+request.getRequestURI(),request);
        }
        logger.info("用户:{}登进我的小窝",user.getNickname());
        ThoughtWithUser topThought = null;
        RedisString redisString = new RedisString(CommonConstant.THOUGHT_TOP_LIST);
        if(org.apache.commons.lang3.StringUtils.isNotEmpty(redisString.get())){
            topThought = JSON.parseObject(redisString.get(),ThoughtWithUser.class);
        };
        request.setAttribute("topThought",topThought);
        if (user.getId().equals(uid)) {
            PageHelper.startPage(p, 20);
            ThoughtExample example = new ThoughtExample();
            if("zan".equals(order)){
                example.setOrderByClause("like_count desc");
            }else {
                example.setOrderByClause("create_time desc");
            }
            example.createCriteria().andDelFlagEqualTo(YesNoEnum.NO.getValue());
            List<ThoughtWithUser> thoughts = thoughtService.getThoughtWithUser(example);

            List<Integer> list = fqCollectService.selectTopicIdsByTypeAndUid(TopicTypeEnum.THOUGHT_TYPE.getValue(),uid);
            JedisCommands commands = JedisProviderFactory.getJedisCommands(null);
            if(CollUtil.isNotEmpty(thoughts)){
                String key = CacheManager.getCollectKey(TopicTypeEnum.THOUGHT_TYPE.name(),uid);
                long redisCount = commands.scard(key);
                if(redisCount == 0){
                    for(Integer tid : list){
                        commands.sadd(key, tid.toString());
                    }
                    commands.expire(key,24*60*60);
                }
                for(ThoughtWithUser thoughtWithUser : thoughts){
                    if(StringUtils.isNotEmpty(thoughtWithUser.getPicList())){
                        thoughtWithUser.setPictures(Arrays.asList(thoughtWithUser.getPicList().split(",")));
                    }
                    thoughtWithUser.setCollected(commands.sismember(key,thoughtWithUser.getId().toString()));
                }
            }
            PageInfo page = new PageInfo(thoughts);
            request.setAttribute("thoughtList",thoughts);

            String followKey = CommonConstant.FQ_FOLLOW_PREFIX+user.getId();
            String followStr = commands.get(followKey);
            int fansCount;
            int followCount;
            if(StringUtils.isEmpty(followStr)){
                UserFollowExample followExample = new UserFollowExample();
                followExample.createCriteria().andFollowedUserIdEqualTo(uid).andDelFlagEqualTo(YesNoEnum.NO.getValue());
                fansCount = userFollowService.countByExample(followExample);
                followExample.clear();
                followExample.createCriteria().andFollowerUserIdEqualTo(uid).andDelFlagEqualTo(YesNoEnum.NO.getValue());
                followCount = userFollowService.countByExample(followExample);
                commands.set(followKey,fansCount+"|"+followCount);
                commands.expire(followKey, 24*60*60);
            }else {
                String[] ff = followStr.split("\\|");
                fansCount = Integer.valueOf(ff[0]);
                followCount = Integer.valueOf(ff[1]);
            }
            List<SimThoughtDTO> sim7ThoughtDTOS = Lists.newArrayList();
            String hotThoughts = commands.get(CommonConstant.SEVEN_DAYS_HOT_THOUGHT_LIST);
            if(StringUtils.isNotEmpty(hotThoughts)){
                sim7ThoughtDTOS =  JSON.parseArray(hotThoughts, SimThoughtDTO.class);
            }
            request.setAttribute("sevenDaysT",sim7ThoughtDTOS);
            request.setAttribute("fansCount",fansCount);
            request.setAttribute("followCount",followCount);
            request.setAttribute("count",page.getTotal());
            request.setAttribute("p",p);
            request.setAttribute("pageSize",20);
            request.setAttribute("activeUserList",CommonConstant.FQ_ACTIVE_USER_LIST);
        }else {
            return peopleIndex(request,response,uid);
        }
    }catch (Exception e){
        request.setAttribute(CommonConstant.SYSTEM_ERROR_CODE,"出错了");
        return GENERAL_CUSTOM_ERROR_URL;
    }finally {
        JedisProviderFactory.getJedisProvider(null).release();
    }
    return "/user/home.html";
}
 
Example 5
Source File: BgImgController.java    From feiqu-opensource with Apache License 2.0 4 votes vote down vote up
@PostMapping("update")
 @ResponseBody
 public Object update(HttpServletRequest request, HttpServletResponse response, String picUrl){
    BaseResult result = new BaseResult();
    try {
         FqUserCache fqUser = webUtil.currentUser(request,response);
        if(fqUser == null){
            result.setResult(ResultEnum.USER_NOT_LOGIN);
            return result;
        }
        if(StringUtils.isEmpty(picUrl)){
            result.setResult(ResultEnum.PARAM_NULL);
            return result;
        }
        FqBackgroundImgExample example = new FqBackgroundImgExample();
        example.createCriteria().andDelFlagEqualTo(YesNoEnum.NO.getValue()).andUserIdEqualTo(fqUser.getId());
         FqBackgroundImg fqBackgroundImgDB = fqBackgroundImgService.selectFirstByExample(example);
         Date now = new Date();
        if(fqBackgroundImgDB == null){
            FqBackgroundImg backgroundImg = new FqBackgroundImg();
            backgroundImg.setImgUrl(picUrl);
            backgroundImg.setDelFlag(YesNoEnum.NO.getValue());
            backgroundImg.setUserId(fqUser.getId());
            backgroundImg.setCreateTime(now);
            backgroundImg.setUpdateTime(now);
            backgroundImg.setHistoryUrls("");
            fqBackgroundImgService.insert(backgroundImg);
        }else {
            if(picUrl.equals(fqBackgroundImgDB.getImgUrl())){
                result.setResult(ResultEnum.PIC_URL_SAME);
                return result;
            }
            fqBackgroundImgDB.setUpdateTime(new Date());
            if(StringUtils.isEmpty(fqBackgroundImgDB.getHistoryUrls())){
                fqBackgroundImgDB.setHistoryUrls(picUrl);
            }else {
                if(!fqBackgroundImgDB.getHistoryUrls().contains(picUrl)){
                    fqBackgroundImgDB.setHistoryUrls(fqBackgroundImgDB.getHistoryUrls() +","+picUrl);
                }
            }
            fqBackgroundImgDB.setImgUrl(picUrl);
            fqBackgroundImgService.updateByPrimaryKey(fqBackgroundImgDB);
        }

        JedisCommands commands = JedisProviderFactory.getJedisCommands(null);
         String key = CacheManager.getUserBackImgKey(fqUser.getId());
         commands.set(key,picUrl);
         commands.expire(key,60*60*24);
         CommonUtils.addActiveNum(fqUser.getId(),ActiveNumEnum.UPDATE_BG_IMG.getValue());
     } catch (Exception e){
         logger.error("更新背景图片失败!",e);
         result.setResult(ResultEnum.FAIL);
     }finally{
         JedisProviderFactory.getJedisProvider(null).release();
     }
     result.setData(picUrl);
     return result;
}