From 1315e98f12d38bd9e84ff37963b9e2203bb461c9 Mon Sep 17 00:00:00 2001 From: xhxu Date: Sat, 14 May 2022 22:26:45 +0800 Subject: [PATCH] add redis --- .../java/com/iformall/utils/Constant.java | 3 + .../com/iformall/utils/RedisCacheUtils.java | 165 ++++++++++++++++++ .../java/com/iformall/config/RedisConfig.java | 28 +++ .../controller/TtMerchantPoiController.java | 32 +++- 4 files changed, 224 insertions(+), 4 deletions(-) create mode 100644 mallinkService/src/main/java/com/iformall/utils/RedisCacheUtils.java diff --git a/mallinkService/src/main/java/com/iformall/utils/Constant.java b/mallinkService/src/main/java/com/iformall/utils/Constant.java index a729490..273a0f6 100644 --- a/mallinkService/src/main/java/com/iformall/utils/Constant.java +++ b/mallinkService/src/main/java/com/iformall/utils/Constant.java @@ -32,4 +32,7 @@ public class Constant { public static final String importMemPrev = "importmem:"; public static final String adminPage = "https://admin.malls.iformall.com"; + + /// REDIS key Prev + public static final String tt_cuser_customer_service_url_key = "cuser:serviceurl:"; } diff --git a/mallinkService/src/main/java/com/iformall/utils/RedisCacheUtils.java b/mallinkService/src/main/java/com/iformall/utils/RedisCacheUtils.java new file mode 100644 index 0000000..9bd8901 --- /dev/null +++ b/mallinkService/src/main/java/com/iformall/utils/RedisCacheUtils.java @@ -0,0 +1,165 @@ +package com.iformall.utils; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.TypeReference; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ValueOperations; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +public class RedisCacheUtils { + + private static T parseJson(String json,Class clazz) { + return JSON.parseObject(json,clazz); + } + + private static List parseListJson(String json,Class clazz) { + return JSON.parseObject(json,new TypeReference>(clazz) {}); + } + + private static Set parseSetJson(String json,Class clazz) { + return JSON.parseObject(json,new TypeReference>(clazz) {}); + } + + public static void cache(RedisTemplate template,String key, Object value,long seconds) { + ValueOperations operations = template.opsForValue(); + if (seconds > 0) { + operations.set(key, value, seconds,TimeUnit.SECONDS); + }else { + operations.set(key, value); + } + } + + public static T getCacheObject(RedisTemplate template,String key,Class clazz) { + if (template.hasKey(key)) { + ValueOperations operations = template.opsForValue(); + Object o = operations.get(key); + if (o instanceof String) { + return parseJson((String)o, clazz); + }else { + return parseJson(JSON.toJSONString(o), clazz); + } + + } + return null; + } + + public static List getCacheListObject(RedisTemplate template,String key,Class clazz) { + if (template.hasKey(key)) { + ValueOperations operations = template.opsForValue(); + Object o = operations.get(key); + if (o instanceof String) { + return parseListJson((String)o, clazz); + }else { + return parseListJson(JSON.toJSONString(o), clazz); + } + } + return null; + } + + public static Set getCacheSetObject(RedisTemplate template,String key,Class clazz) { + if (template.hasKey(key)) { + ValueOperations operations = template.opsForValue(); + Object o = operations.get(key); + if (o instanceof String) { + return parseSetJson((String)o, clazz); + }else { + return parseSetJson(JSON.toJSONString(o), clazz); + } + } + return null; + } + + public static Map getCacheMap(RedisTemplate template,String key) { + return getCacheObject(template,key,Map.class); + } + + public static String getCacheString(RedisTemplate template,String key) { + if (template.hasKey(key)) { + ValueOperations operations = template.opsForValue(); + Object o = operations.get(key); + if( o instanceof String) { + return (String) o; + } + return JSON.toJSONString(o); + } + return null; + } + + public static Long getCacheLong(RedisTemplate template,String key) { + if (template.hasKey(key)) { + ValueOperations operations = template.opsForValue(); + Object o = operations.get(key); + if (o instanceof String) { + Long.parseLong((String)o); + } + return Long.parseLong(o.toString()); + } + return null; + } + + public static Integer getCacheInteger(RedisTemplate template,String key) { + if (template.hasKey(key)) { + ValueOperations operations = template.opsForValue(); + Object o = operations.get(key); + if (o instanceof String) { + return new Integer((String)o); + } + return new Integer(o.toString()); + } + return null; + } + + public static T getFieldObject(Object o,String property,Class clazz) { + String json = JSON.toJSONString(o); + JSONObject jsonObject = JSON.parseObject(json); + Object p = jsonObject.get(property); + if (null != p ) { + if (p instanceof String) { + return parseJson((String)p, clazz); + } + return parseJson(JSON.toJSONString(p), clazz); + } + return null; + } + + public static List getFieldListObject(Object o,String property,Class clazz) { + String json = JSON.toJSONString(o); + JSONObject jsonObject = JSON.parseObject(json); + Object p = jsonObject.get(property); + if (null != p ) { + if (p instanceof String) { + return parseListJson((String)p, clazz); + } + return parseListJson(JSON.toJSONString(p), clazz); + } + return null; + } + + public static Set getFieldSetObject(Object o,String property,Class clazz) { + String json = JSON.toJSONString(o); + JSONObject jsonObject = JSON.parseObject(json); + Object p = jsonObject.get(property); + if (null != p ) { + if (p instanceof String) { + return parseSetJson((String)p, clazz); + } + return parseSetJson(JSON.toJSONString(p), clazz); + } + return null; + } + + public static Map getFieldMapObject(Object o,String property) { + return getFieldObject(o,property,Map.class); + } + + public static void removeCache(RedisTemplate template,String key) { + if (template.hasKey(key)) { + template.delete(key); + } + } +} diff --git a/mlToutiaoOpen/src/main/java/com/iformall/config/RedisConfig.java b/mlToutiaoOpen/src/main/java/com/iformall/config/RedisConfig.java index bc9721f..322e1ec 100644 --- a/mlToutiaoOpen/src/main/java/com/iformall/config/RedisConfig.java +++ b/mlToutiaoOpen/src/main/java/com/iformall/config/RedisConfig.java @@ -1,5 +1,9 @@ package com.iformall.config; +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cache.CacheManager; @@ -10,6 +14,9 @@ import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; import java.time.Duration; import java.util.HashMap; @@ -69,4 +76,25 @@ public class RedisConfig extends CachingConfigurerSupport { return cacheManager; } + @Bean("objectCommonRedisTemplate") + public RedisTemplate getObjectValueOperations(RedisConnectionFactory connectionFactory) { + RedisTemplate template = new RedisTemplate<>(); + template.setConnectionFactory(connectionFactory); + Jackson2JsonRedisSerializer j = new Jackson2JsonRedisSerializer(Object.class); + ObjectMapper om = new ObjectMapper(); + om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); + om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + j.setObjectMapper(om); + + // value值的序列化 + template.setValueSerializer(j); + template.setHashValueSerializer(j); + + // key的序列化 + template.setKeySerializer(new StringRedisSerializer()); + template.setHashKeySerializer(new StringRedisSerializer()); + template.afterPropertiesSet(); + return template; + } + } diff --git a/mlToutiaoOpen/src/main/java/com/iformall/controller/TtMerchantPoiController.java b/mlToutiaoOpen/src/main/java/com/iformall/controller/TtMerchantPoiController.java index 60132f8..2c421d6 100644 --- a/mlToutiaoOpen/src/main/java/com/iformall/controller/TtMerchantPoiController.java +++ b/mlToutiaoOpen/src/main/java/com/iformall/controller/TtMerchantPoiController.java @@ -1,6 +1,7 @@ package com.iformall.controller; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.github.pagehelper.PageInfo; import com.iformall.common.ErrorCode; import com.iformall.common.ResultData; @@ -14,6 +15,8 @@ import com.iformall.service.TtMerchantPoiService; import com.iformall.service.WxAppinfoService; import com.iformall.service.toutiao.FmTtOpenService; import com.iformall.service.toutiao.api.TtOpenMaService; +import com.iformall.utils.Constant; +import com.iformall.utils.RedisCacheUtils; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; @@ -21,6 +24,8 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; @@ -43,6 +48,10 @@ public class TtMerchantPoiController extends BaseController { @Autowired protected FmTtOpenService openService; + @Autowired + @Qualifier("objectCommonRedisTemplate") + RedisTemplate redisTemplate; + @ApiOperation("分页列表接口") @GetMapping("list") @ApiImplicitParams({ @@ -323,16 +332,31 @@ public class TtMerchantPoiController extends BaseController { if(!type.equals("1128") && !type.equals("2329")){ return new ResultData(ErrorCode.SYS_PARAMETER_ERROR); } - WxAppinfo appinfo = appinfoService.getByAppId(appid); + String key = Constant.tt_cuser_customer_service_url_key + "type:" + + appid + ":" + openid; + String cacheString = RedisCacheUtils.getCacheString(redisTemplate, key); - if(appinfo == null){ - return new ResultData(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"找不到小程序信息"); + if(StringUtils.isNotBlank(cacheString)){ + return new ResultData(cacheString); } +// WxAppinfo appinfo = appinfoService.getByAppId(appid); +// +// if(appinfo == null){ +// return new ResultData(ErrorCode.SYS_PARAMETER_ERROR.getCode(),"找不到小程序信息"); +// } try{ TtOpenMaService openMaService = openService.getTtOpenComponentService().getTtMaServiceByAppid(appid); String res = openMaService.queryCustomerServiceUrl(openid,type); - return new ResultData(res); + JSONObject jsonObject = JSONObject.parseObject(res); + Integer err_no = jsonObject.getInteger("err_no"); + if(err_no.intValue() == 0){ + RedisCacheUtils.cache(redisTemplate,key,res,24*3600); + return new ResultData(res); + }else{ + String err_tips = jsonObject.getString("err_tips"); + return new ResultData(ErrorCode.SYS_SERVER_ERROR.getCode(),err_tips); + } }catch(Exception e){ logger.error("获取客服链接 error"+ e.getMessage()); return new ResultData(ErrorCode.SYS_SERVER_ERROR.getCode(),"获取客服链接失败");