您的位置:首页 > 科技 > 能源 > 搜狗推广管家下载_北京seo加盟_内容营销是什么意思_seosem顾问

搜狗推广管家下载_北京seo加盟_内容营销是什么意思_seosem顾问

2024/11/15 7:15:30 来源:https://blog.csdn.net/enthan809882/article/details/142259051  浏览:    关键词:搜狗推广管家下载_北京seo加盟_内容营销是什么意思_seosem顾问
搜狗推广管家下载_北京seo加盟_内容营销是什么意思_seosem顾问

文章目录

      • redisController
      • opsForHash的一次优化经历

redis-cli连接redis太不直观,一直寻思弄个工具类,网上一看到处都是,拿来直接用吧。

这代码也没多少啊,编辑时居然卡卡的。。。

redisController

代码:

/** redis功能*/
@RestController
@RequestMapping(value = "/interface/redis")
public class RedisController {private static Logger logger = LoggerFactory.getLogger(RedisController.class);@Autowiredprivate RedisTemplate redisTemplate;@RequestMapping("/setString")public Object set(@RequestBody RedisRequest request) {redisTemplate.opsForValue().set(request.getKey(), request.getValue());return "OK";}// 注:get方法在不确定类型的情况下不能直接用@RequestMapping("/getString")public String get(@RequestBody RedisRequest request) {Object o = redisTemplate.opsForValue().get(request.getKey());return JSON.toJSONString(o);}// type也是通用功能,不区分类型  keys加强下  实际用不到这个接口了@RequestMapping("/type")public JsonResult type(@RequestBody RedisRequest request) {JsonResult result = new JsonResult<>();try {logger.info("redis功能_delete请求开始,request={}", JSON.toJSONString(request));DataType type = redisTemplate.type(request.getKey());logger.info("redis功能_delete请求完成,result={}",JSON.toJSONString(result));result=JsonResult.success(type);return result;}catch(Exception e){logger.error("redis功能_delete请求异常,error:",e);return JsonResult.error("-1","redis功能_delete请求异常,error="+e.getMessage());}}// 删除的时候区分类型吗? 不区分@RequestMapping("/delete")public JsonResult delete(@RequestBody RedisRequest request) {JsonResult result = new JsonResult<>();try {logger.info("redis功能_delete请求开始,request={}", JSON.toJSONString(request));redisTemplate.delete(request.getKey());logger.info("redis功能_delete请求完成,result={}",JSON.toJSONString(result));return result;}catch(Exception e){logger.error("redis功能_delete请求异常,error:",e);return JsonResult.error("-1","redis功能_delete请求异常,error="+e.getMessage());}}@RequestMapping("/keys")public JsonResult keys(@RequestBody RedisMapRequest request) {JsonResult<Object> result = new JsonResult<>();HashMap keysMap  = new HashMap<>();Set<String> keys = new HashSet<String>();try {logger.info("redis功能_keys请求开始,request={}",JSON.toJSONString(request));if(!StringUtils.isEmpty(request.getKey())){keys=redisTemplate.keys(request.getKey());}else{keys = redisTemplate.keys("*");}for (String item:keys) {DataType type = redisTemplate.type(item);keysMap.put(item,type);}result=JsonResult.success(keysMap);logger.info("redis功能_keys请求完成,result={}",JSON.toJSONString(result));return result;}catch (Exception e){logger.error("redis功能_keys请求异常,error:",e);return JsonResult.error("-1","redis功能_keys请求异常,error="+e.getMessage());}}@RequestMapping("/info")public JsonResult info(@RequestBody RedisRequest request) {JsonResult result = new JsonResult<>();try {logger.info("redis功能_info请求开始,request={}", JSON.toJSONString(request));Properties info = redisTemplate.getConnectionFactory().getConnection().info();result=JsonResult.success(info);logger.info("redis功能_info请求完成,result={}",JSON.toJSONString(result));return result;}catch(Exception e){logger.error("redis功能_info请求异常,error:",e);return JsonResult.error("-1","redis功能_info请求异常,error="+e.getMessage());}}// 添加单条记录@RequestMapping("/putToMap")public JsonResult putToMap(@RequestBody RedisMapRequest request) {JsonResult result = new JsonResult<>();try {logger.info("redis功能_putToMap请求开始,request={}", JSON.toJSONString(request));redisTemplate.opsForHash().put(request.getKey(), request.getSonKey(),request.getSonValue());// 重查Map map = redisTemplate.opsForHash().entries(request.getKey());result=JsonResult.success(map);logger.info("redis功能_putToMap请求完成,result={}",JSON.toJSONString(result));return result;}catch(Exception e){logger.error("redis功能_putToMap请求异常,error:",e);return JsonResult.error("-1","redis功能_putToMap请求异常,error="+e.getMessage());}}/** 添加所有到map* 注:这是个增量接口* 如果key相同,会覆盖* 如果map为空 不会影响原有map*/@RequestMapping("/putAllToMap")public JsonResult putAllToMap(@RequestBody RedisMapRequest request) {JsonResult<Object> result = new JsonResult<>();try {logger.info("redis功能_putAllToMap请求开始,request={}", JSON.toJSONString(request));redisTemplate.opsForHash().putAll(request.getKey(),request.getMap());logger.info("redis功能_putAllToMap请求开始,request={}", JSON.toJSONString(request));return result;}catch(Exception e) {logger.error("redis功能_putAllToMap请求异常,error:", e);return JsonResult.error("-1", "redis功能_putAllToMap请求异常,error=" + e.getMessage());}}@RequestMapping("/getFromMap")public JsonResult getFromMap(@RequestBody RedisMapRequest request) {JsonResult<Object> result = new JsonResult<>();try {logger.info("redis功能_getFromMap请求开始,request={}",JSON.toJSONString(request));Object o = redisTemplate.opsForHash().get(request.getKey(), request.getSonKey());result=JsonResult.success(o);return result;}catch(Exception e){logger.error("redis功能_putAllToMap请求异常,error:", e);return JsonResult.error("-1", "redis功能_putAllToMap请求异常,error=" + e.getMessage());}}// 强制set  先删除 再set全部@RequestMapping("/setMap")public JsonResult setMap(@RequestBody RedisMapRequest request,HashMap map) {JsonResult<Object> result = new JsonResult<>();try {logger.info("redis功能_setMap请求开始,request={}",JSON.toJSONString(request));if(null==map){map = new HashMap<>();}redisTemplate.opsForHash().delete(request.getKey());redisTemplate.opsForHash().putAll(request.getKey(), map);logger.info("redis功能_setMap请求完成,result={}",JSON.toJSONString(result));return result;}catch(Exception e){logger.error("redis功能_setMap请求异常,error:", e);return JsonResult.error("-1", "redis功能_setMap请求异常,error=" + e.getMessage());}}@RequestMapping("/getMapValues")public JsonResult getMapValues(@RequestBody RedisMapRequest request) {JsonResult result = new JsonResult<>();try {logger.info("redis功能_getMapValues请求开始,request={}",JSON.toJSONString(request));List list = redisTemplate.opsForHash().values(request.getKey());result=JsonResult.success(list);logger.info("redis功能_getMapValues请求完成,request={}",JSON.toJSONString(request));return result;}catch(Exception e){logger.error("redis功能_getMapValues请求异常,error:", e);return JsonResult.error("-1", "redis功能_getMapValues请求异常,error=" + e.getMessage());}}@RequestMapping("/getMapEntries")public JsonResult getMapEntries(@RequestBody RedisMapRequest request) {JsonResult<Object> result = new JsonResult<>();try {logger.info("redis功能_getMapEntries请求开始,request={}",JSON.toJSONString(request));Map map = redisTemplate.opsForHash().entries(request.getKey());result=JsonResult.success(map);logger.info("redis功能_getMapEntries请求完成,result={}",JSON.toJSONString(result));return result;}catch(Exception e){logger.error("redis功能_getMapEntries请求异常,error:", e);return JsonResult.error("-1", "redis功能_getMapEntries请求异常,error=" + e.getMessage());}}@RequestMapping("/delMap")public JsonResult delMap(@RequestBody RedisMapRequest request) {JsonResult<Object> result = new JsonResult<>();try {logger.info("redis功能_delMap请求开始,request={}",JSON.toJSONString(request));Long delflag = redisTemplate.opsForHash().delete(request.getKey());result=JsonResult.success(delflag);logger.info("redis功能_delMap请求完成,result={}",JSON.toJSONString(result));return result;}catch(Exception e){logger.error("redis功能_delMap请求异常,error:", e);return JsonResult.error("-1", "redis功能_delMap请求异常,error=" + e.getMessage());}}// 从map中删除一个键值对@RequestMapping("/deleteKeyFromMap")public JsonResult deleteKeyFromMap(@RequestBody RedisMapRequest request) {JsonResult<Object> result = new JsonResult<>();try {logger.info("redis功能_deleteKeyFromMap请求开始,request={}",JSON.toJSONString(request));Long delflag = redisTemplate.opsForHash().delete(request.getKey(), request.getSonKey());result=JsonResult.success(delflag);logger.info("redis功能_deleteKeyFromMap请求完成,result={}",JSON.toJSONString(result));return result;}catch(Exception e){logger.error("redis功能_deleteKeyFromMap请求异常,error:", e);return JsonResult.error("-1", "redis功能_deleteKeyFromMap请求异常,error=" + e.getMessage());}}@RequestMapping("/listAddValues")public JsonResult listAddValues(@RequestBody RedisListRequest request) {try {logger.info("redis功能_listAddValues请求开始,request={}", JSON.toJSONString(request));JsonResult result = new JsonResult<>();Long i = redisTemplate.opsForList().rightPushAll(request.getKey(), request.getValues());logger.info("redis功能_listAddValues请求完成,i={}", i);result.setMessage("redis功能_listAddValues请求完成,i="+i);return result;}catch(Exception e){logger.error("redis功能_listAddValues请求异常,error:",e);return JsonResult.error("-1","redis功能_listAddValues请求异常");}}@RequestMapping("/listLeftPush")public JsonResult listLeftPush(@RequestBody RedisListRequest request) {try {logger.info("redis功能_listLeftPush请求开始,request={}", JSON.toJSONString(request));JsonResult result = new JsonResult<>();Long i = redisTemplate.opsForList().leftPush(request.getKey(), request.getValue());logger.info("redis功能_listLeftPush请求完成,i={}",i);result.setMessage("redis功能_listLeftPush请求完成,i="+i);return result;}catch(Exception e){logger.error("redis功能_listLeftPush请求异常,error:",e);return JsonResult.error("-1","redis功能_listLeftPush请求异常");}}@RequestMapping("/listLeftPop")public JsonResult listLeftPop(@RequestBody RedisListRequest request) {try {logger.info("redis功能_listLeftPop请求开始,request={}", JSON.toJSONString(request));JsonResult result = new JsonResult<>();// 注: pop是不需要值得Object object = redisTemplate.opsForList().leftPop(request.getKey());logger.info("redis功能_listLeftPop请求完成,object={}",JSON.toJSONString(object));result.setMessage("redis功能_listLeftPush请求完成,object="+JSON.toJSONString(object));return result;}catch(Exception e){logger.error("redis功能_listLeftPush请求异常,error:",e);return JsonResult.error("-1","redis功能_listLeftPush请求异常");}}@RequestMapping("/listShowAll")public JsonResult listShowAll(@RequestBody RedisListRequest request) {try {logger.info("redis功能_listShowAll请求开始,request={}", JSON.toJSONString(request));JsonResult result = new JsonResult<>();// 小技巧,遍历全部Object object = redisTemplate.opsForList().range(request.getKey(),0,-1);logger.info("redis功能_listShowAll请求完成,object={}",JSON.toJSONString(object));result.setMessage("redis功能_listShowAll请求完成,object="+JSON.toJSONString(object));return result;}catch(Exception e){logger.error("redis功能_listShowAll请求异常,error:",e);return JsonResult.error("-1","redis功能_listShowAll请求异常,error="+e.getMessage());}}@RequestMapping("/setZset")public JsonResult setZset(@RequestBody RedisZsetRequest request) {try {logger.info("redis功能_setZset请求开始,request={}", JSON.toJSONString(request));JsonResult result = new JsonResult<>();Boolean flag = redisTemplate.opsForZSet().add(request.getKey(), request.getValue(), request.getScore());logger.info("redis功能_setZset请求完成,flag={}",flag);result.setMessage("redis功能_setZset请求完成,条数="+flag);return result;}catch(Exception e){logger.error("redis功能_setZset请求异常,error:",e);return JsonResult.error("-1","redis功能_setZset请求异常,error="+e.getMessage());}}@RequestMapping("/zsetRangeMembersAndScores")public JsonResult zsetRangeMembersAndScores(@RequestBody RedisZsetRequest request) {JsonResult result = new JsonResult<>();try {logger.info("redis功能_zsetRangeMembersAndScores请求开始,request={}", JSON.toJSONString(request));Set set = redisTemplate.opsForZSet().range(request.getKey(), request.getStart(), request.getEnd());ZSetOperations<String, String> zSetOps = redisTemplate.opsForZSet();ArrayList<Object> zsetList = new ArrayList<>();RedisZsetEntity zsetEntity=null;for (Object item: set) {zsetEntity = new RedisZsetEntity();Double score = zSetOps.score(request.getKey(), item);zsetEntity.setKey(request.getKey());zsetEntity.setValue(item.toString());zsetEntity.setScore(score);zsetList.add(zsetEntity);}result=JsonResult.success(zsetList);logger.info("redis功能_zsetRangeMembersAndScores请求完成,result={}",JSON.toJSONString(result));return result;}catch(Exception e){logger.error("redis功能_zsetRangeMembersAndScores请求异常,error:",e);return JsonResult.error("-1","redis功能_zsetRangeMembersAndScores请求异常,error="+e.getMessage());}}@RequestMapping("/zsetIncrementScore")public JsonResult zsetIncrementScore(@RequestBody RedisZsetRequest request) {JsonResult result = new JsonResult<>();try {logger.info("redis功能_zsetIncrementScore请求开始,request={}", JSON.toJSONString(request));ZSetOperations<String, String> zSetOps = redisTemplate.opsForZSet();Double yScore = zSetOps.score(request.getKey(), request.getValue());Double newScore = zSetOps.incrementScore(request.getKey(), request.getValue(), request.getDelta());result.setMessage("redis功能_zsetIncrementScore请求完成,yScore={}"+yScore+",newScore="+ newScore);logger.info("redis功能_zsetIncrementScore请求完成,result={}",JSON.toJSONString(result));return result;}catch(Exception e){logger.error("redis功能_zsetIncrementScore请求异常,error:",e);return JsonResult.error("-1","redis功能_zsetIncrementScore请求异常,error="+e.getMessage());}}@RequestMapping("/zsetIntersectAndStore")public JsonResult zsetIntersectAndStore(@RequestBody RedisZsetRequest request) {JsonResult result = new JsonResult<>();try {logger.info("redis功能_zsetIntersectAndStore请求开始,request={}", JSON.toJSONString(request));ZSetOperations<String, String> zSetOps = redisTemplate.opsForZSet();zSetOps.intersectAndStore(request.getKey(), request.getOtherKey(), request.getDestKey());// 新集合logger.info("redis功能_zsetIntersectAndStore请求完成,result={}",JSON.toJSONString(result));return result;}catch(Exception e){logger.error("redis功能_zsetIntersectAndStore请求异常,error:",e);return JsonResult.error("-1","redis功能_zsetIntersectAndStore请求异常,error="+e.getMessage());}}public JsonResult removeFromList(@RequestBody RedisListRequest request) {try {logger.info("redis功能_removeFromList请求开始,request={}", JSON.toJSONString(request));JsonResult result = new JsonResult<>();// 小技巧,遍历全部Long i = redisTemplate.opsForList().remove(request.getKey(), request.getCount(), request.getValue());logger.info("redis功能_removeFromList请求完成,条数={}",i);result.setMessage("redis功能_removeFromList请求完成,条数="+i);return result;}catch(Exception e){logger.error("redis功能_removeFromList请求异常,error:",e);return JsonResult.error("-1","redis功能_removeFromList请求异常,error="+e.getMessage());}}
}

opsForHash的一次优化经历

opsForHash有put方法也有putAll方法,如果条数比较多,非常推荐先放到一个map,再用putAll,这样减少和redis交互的次数,性能提高很多。

有一次执行时发现明显的卡两秒,排查了下,是在for循环里面单条put了,所以非常慢,优化为putAll之后快了许多。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com