Browse Source

用户地址缓存

igl 1 year ago
parent
commit
03aa7fdd58

+ 8 - 0
framework-location/src/main/java/com/chelvc/framework/location/support/TencentLocationHandler.java

@@ -1,9 +1,11 @@
 package com.chelvc.framework.location.support;
 
+import java.time.Duration;
 import java.util.Objects;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
+import com.chelvc.framework.base.context.SessionContextHolder;
 import com.chelvc.framework.common.model.Region;
 import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.common.util.StringUtils;
@@ -11,11 +13,13 @@ import com.chelvc.framework.location.Address;
 import com.chelvc.framework.location.Area;
 import com.chelvc.framework.location.LocationHandler;
 import com.chelvc.framework.location.config.TencentLocationProperties;
+import com.chelvc.framework.redis.RedisConstant;
 import lombok.NonNull;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 import org.springframework.web.client.RestTemplate;
 
@@ -43,6 +47,8 @@ public class TencentLocationHandler implements LocationHandler {
     private final RestTemplate restTemplate;
     private final TencentLocationProperties properties;
 
+    private final RedisTemplate<String, Object> redisTemplate;
+
     /**
      * 执行定位请求
      *
@@ -95,6 +101,8 @@ public class TencentLocationHandler implements LocationHandler {
         if (region.getType() == Region.Type.DISTRICT) {
             address.setDistrict(Area.builder().id(region.getCode()).name(position.getDistrict()).build());
         }
+        redisTemplate.opsForValue().set(RedisConstant.USER_ADDRESS + SessionContextHolder.getId(),
+                address, Duration.ofHours(48));
         return address;
     }
 

+ 7 - 0
framework-redis/src/main/java/com/chelvc/framework/redis/RedisConstant.java

@@ -0,0 +1,7 @@
+package com.chelvc.framework.redis;
+
+public interface RedisConstant {
+
+
+    String USER_ADDRESS = "user:address:";
+}