您的位置:首页 > 数据库 > Redis

redis不同数据接口类型的应用

2019-08-21 10:47 806 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/weixin_42395975/article/details/98746594

redis不同数据接口类型的应用

散列hash类型的使用:刷新职位类型到redis
/**
* 同步职位类型到redis
* @param exDemandBean
* @throws Exception
*/
public void selectCodeToRedis() {
if (redisUtil.hasKey(RedisKey_CodeConstants.BASE_CODEPOST_INFO)) {
codePostMap = redisUtil.hmget(RedisKey_CodeConstants.BASE_CODEPOST_INFO);
} else {
Map<String, Object> codePostNameMap = new HashMap<String, Object>();
List<IntentionPosition> postList = intentionPositionDao.getPositionNameList();
for (IntentionPosition it : postList) {
codePostNameMap.put(it.getPositionId() + "", it.getPositionName());
}
redisUtil.hmset(RedisKey_CodeConstants.BASE_CODEPOST_INFO, codePostNameMap, time);
}
}

结果:

String类型:刷新省市区code到redis
@Override
public List<CodeArea> getAllChinaProvinceAndChildren() {
List<CodeArea> areaList = null;
try {
JSONArray array = (JSONArray) redisUtil.get(RedisKey_CodeConstants.BASE_PROVINCEAndCITY_INFO);
if(array !=null){
areaList = JSONObject.parseArray(array.toJSONString(), CodeArea.class);
}

if(areaList == null){
Map<String, Object> param = new HashMap<String, Object>();
param.put("parentId", "1");
areaList = codeAreaDao.findProvinceAllCascadeCity(param);
redisUtil.set(RedisKey_CodeConstants.BASE_PROVINCEAndCITY_INFO,areaList, CacheConstants.CACHE_CODE_TIME);
}
} catch (Exception e) {
logger.error("getAllChinaProvinceAndChildren", e);
}
return areaList;
}

结果:

[{"abbr":"河北",
"children":[
{"abbr":"石家庄","code":"130100","id":65,"name":"石家庄市","parentId":2,"statusNo":1,"typeCode":3,"updateTime":1463452570000},
{"abbr":"邢台","code":"130500","id":66,"name":"邢台市","parentId":2,"statusNo":1,"typeCode":3,"updateTime":1463452570000},
{"abbr":"唐山","code":"130200","id":67,"name":"唐山市","parentId":2,"statusNo":1,"typeCode":3,"updateTime":1463452570000},
{"abbr":"秦皇岛","code":"130300","id":68,"name":"秦皇岛市","parentId":2,"statusNo":1,"typeCode":3,"updateTime":1463452570000},
{"abbr":"保定","code":"130600","id":69,"name":"保定市","parentId":2,"statusNo":1,"typeCode":3,"updateTime":1463452570000},
{"abbr":"衡水","code":"131100","id":70,"name":"衡水市","parentId":2,"statusNo":1,"typeCode":3,"updateTime":1463452570000},
{"abbr":"张家口","code":"130700","id":71,"name":"张家口市","parentId":2,"statusNo":1,"typeCode":3,"updateTime":1463452570000},
{"abbr":"廊坊","code":"131000","id":72,"name":"廊坊市","parentId":2,"statusNo":1,"typeCode":3,"updateTime":1463452570000},
{"abbr":"沧州","code":"130900","id":73,"name":"沧州市","parentId":2,"statusNo":1,"typeCode":3,"updateTime":1463452570000},
{"abbr":"邯郸","code":"130400","id":74,"name":"邯郸市","parentId":2,"statusNo":1,"typeCode":3,"updateTime":1463452570000}
]
}
]
Set,sortSet集合类型:

使用场景:用户购买简历后,用sortSet类型存储score排序。简历是否被关注使用hash散列类型储存,统计简历被关注的数量。

@ResponseBody
@RequestMapping(value = "/followResume", method = RequestMethod.POST)
@CheckParam("userId,resumeId")
public ResultMessage followResume(@RequestParam(value = "userId") Integer userId,
@RequestParam(value = "resumeId") Integer resumeId,
@RequestParam(value = "source") String source) {
try {
Map<String, Object> result = new HashMap<>();
/**验证用户信息*/
CorpUser user = corpUserService.getById(userId.longValue());
/**验证简历是否存在*/
Boolean exists = redisUtil.hasKey(RedisKey_ResumeConstants.BASE_RESUME_INFO + resumeId);
if (!exists) {
logger.error(RedisKey_ResumeConstants.BASE_RESUME_INFO + resumeId + "\t\t\t简历不存在");
return new ResultMessage(ResponseCode.DATA_NOT_FOUND.getCode(), "简历不存在", "");
}
//获取该简历联系方式
Map<Object, Object> resumeInfo = redisUtil.hmget(RedisKey_ResumeConstants.BASE_RESUME_INFO + resumeId);
result.put("mobile", resumeInfo.get("mobile") != null ? resumeInfo.get("mobile") : "");
result.put("name", resumeInfo.get("name") != null ? resumeInfo.get("name") : "");
/**验证简历是否已被关注*/
if (redisUtil.sHasKey(RedisKey_ResumeConstants.BASE_RESUME_USER + userId + ":seenMobile", resumeId)) {
//已下载过的简历,直接返回信息
return new ResultMessage(ResponseCode.SUCCESS.getCode(), "操作成功", result);
}
/**判断简历来源
* 1.如果为共享简历库(搜索),验证积分是否充足
*/
if (source.trim().equals("search")) {
/**判断积分是否充足(调用服务)*/
Boolean reducePointtByResume = userFeign.isReducePointtByResume(userId.longValue());
if(!reducePointtByResume) {
return new ResultMessage(ResponseCode.BUSINESS_DATA_ZERO.getCode(), "积分不足", "");
}
}
/**添加到关注记录里(关注即为下载)*/
redisUtil.sSet(RedisKey_ResumeConstants.BASE_RESUME_USER + userId + ":seenMobile", resumeId);
/**下载后,添加到统计数据,供BI使用*/
String countKey = RedisKey_ResumeConstants.BASE_RESUME_DOWNLOADCOUNT + DateConvertUtils.format(DateConvertUtils.getNow(), DateConvertUtils.allDate);
String countField = resumeId.toString();
boolean hasKey = redisUtil.hHasKey(countKey, countField);
if (!hasKey) {
redisUtil.hset(countKey, countField, 1);
redisUtil.expire(countKey, 30 * 24 * 60 * 60); // 一个月有效期
} else {
redisUtil.hincr(countKey, countField, 1);
}
/**关注后添加到用户简历库(关注即为下载)*/
long time = new Date().getTime();
redisUtil.add(RedisKey_ResumeConstants.BASE_RESUME_USER + userId + ":repository", resumeId, time);
/**增加简历关注(下载)量*/
redisUtil.hincr(RedisKey_StatisticsConstants.STATISTICS_RESUME_DOWNLOADS, resumeId.toString(), 1);
/**判断简历来源
* 2.如果为共享简历库(搜索),扣除积分
*/
if (source.trim().equals("search")) {
/**消耗积分(调用服务)*/
ResultEntity resultEntity = userFeign.reducePointbyRe
3ff7
sume(userId.longValue());
if(!resultEntity.isSuccess()) {
return new ResultMessage(Integer.parseInt(resultEntity.getCode()), resultEntity.getMessage(), "");
}
logger.info("===成功扣除积分===");
}
/**给求职者(简历)发送短信*/
logger.info(String.format("给简历id:%s 发送完善信息短信",resumeId));
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
YlwTianyanchaCorpBase corpInfoByEhrId = tycCorpService.getByEhrId(user.getCorpId());
if (resumeInfo.get("mobile") != null && RegexUtil.isMobile_11num(resumeInfo.get("mobile").toString())) {
ResultMessage resultMessage = codeFeign.sendSmsByCodeOrCustom("509",resumeInfo.get("mobile").toString(), null,Long.valueOf(userId), Long.valueOf(resumeId), null, null,null, null, corpInfoByEhrId != null ? corpInfoByEhrId.getName() : "", null,false);
logger.info("短信接口返回结果:" + JSONObject.fromObject(resultMessage));
}
}
});
thread.start();
return new ResultMessage(com.qh.util.base.ResponseCode.SUCCESS.getCode(), "操作成功", result);
} catch (NumberFormatException e) {
e.printStackTrace();
return new ResultMessage(com.qh.util.base.ResponseCode.SYSTEM_INNER_ERROR.getCode(), "操作异常", "");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: