Преглед изворни кода

cms delete redis app login data

huodongdong пре 6 година
родитељ
комит
445c3c5ae5

+ 130 - 0
rankin-cms-web/src/main/java/cn/rankin/cmsweb/configuration/RedisCacheConfig.java

@@ -0,0 +1,130 @@
+package cn.rankin.cmsweb.configuration;
+
+import cn.rankin.common.utils.service.RedisService;
+import cn.rankin.common.utils.service.impl.RedisServiceImpl;
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.PropertyAccessor;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.cache.annotation.CachingConfigurerSupport;
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.cache.interceptor.KeyGenerator;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.cache.RedisCacheManager;
+import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+
+import java.lang.reflect.Method;
+
+@Configuration
+@EnableCaching //加上这个注解是的支持缓存注解
+public class RedisCacheConfig extends CachingConfigurerSupport {
+
+    @Value("${spring.redis.host}")
+    private String host;
+
+    @Value("${spring.redis.port}")
+    private int port;
+
+    @Value("${spring.redis.timeout}")
+    private int timeout;
+
+    @Value("${spring.redis.password}")
+    private String password;
+
+
+    /**
+     * 连接redis的工厂类
+     *
+     * @return
+     */
+    @Bean
+    public JedisConnectionFactory jedisConnectionFactory() {
+        JedisConnectionFactory factory = new JedisConnectionFactory();
+        factory.setHostName(host);
+        factory.setPort(port);
+        factory.setTimeout(timeout);
+        factory.setPassword(password);
+        return factory;
+    }
+
+    /**
+     * 配置RedisTemplate
+     * 设置添加序列化器
+     * key 使用string序列化器
+     * value 使用Json序列化器
+     * 还有一种简答的设置方式,改变defaultSerializer对象的实现。
+     *
+     * @return
+     */
+    @Bean(name = "redisTemplate")
+    public RedisTemplate<Object, Object> redisTemplate() {
+        //StringRedisTemplate的构造方法中默认设置了stringSerializer
+        RedisTemplate<Object, Object> template = new RedisTemplate<>();
+        //设置开启事务
+        template.setEnableTransactionSupport(true);
+        //set key serializer
+        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
+        template.setKeySerializer(stringRedisSerializer);
+        template.setHashKeySerializer(stringRedisSerializer);
+        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
+        ObjectMapper objectMapper = new ObjectMapper();
+        objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
+        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
+
+        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
+
+        //set value serializer
+//        template.setDefaultSerializer(jackson2JsonRedisSerializer);
+        template.setValueSerializer(jackson2JsonRedisSerializer);
+        template.setHashValueSerializer(jackson2JsonRedisSerializer);
+
+        template.setConnectionFactory(jedisConnectionFactory());
+        template.afterPropertiesSet();
+        return template;
+    }
+
+    /**
+     * 设置RedisCacheManager
+     * 使用cache注解管理redis缓存
+     *
+     * @return
+     */
+    @Bean
+    public RedisCacheManager cacheManager() {
+        RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate());
+        return redisCacheManager;
+    }
+
+    @Bean
+    public RedisService redisService() {
+        RedisServiceImpl redisService = new RedisServiceImpl();
+        redisService.setRedisTemplate(this.redisTemplate());
+        return redisService;
+    }
+
+    /**
+     * 自定义生成redis-key
+     *
+     * @return
+     */
+    @Override
+    public KeyGenerator keyGenerator() {
+        return new KeyGenerator() {
+            @Override
+            public Object generate(Object o, Method method, Object... objects) {
+                StringBuilder sb = new StringBuilder();
+                sb.append(o.getClass().getName()).append(".");
+                sb.append(method.getName()).append(".");
+                for (Object obj : objects) {
+                    sb.append(obj.toString());
+                }
+                System.out.println("keyGenerator=" + sb.toString());
+                return sb.toString();
+            }
+        };
+    }
+}

+ 19 - 0
rankin-cms-web/src/main/java/cn/rankin/cmsweb/controller/user/TerminalDeviceController.java

@@ -4,6 +4,8 @@ import cn.rankin.cmsweb.service.user.TerminalDeviceService;
 import cn.rankin.cmsweb.service.user.TerminalUserService;
 import cn.rankin.common.utils.api.model.APIResult;
 import cn.rankin.common.utils.api.model.BaseCode;
+import cn.rankin.common.utils.constant.RedisKey;
+import cn.rankin.common.utils.service.RedisService;
 import cn.rankin.data.api.user.vo.TerminalUserVo;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.PathVariable;
@@ -15,18 +17,35 @@ import org.springframework.web.bind.annotation.RestController;
 @RequestMapping(value = "/device")
 public class TerminalDeviceController {
 
+    public final static String USER_FORMAT_KEY = RedisKey.USER_FORMAT_KEY;
+
     @Autowired
     private TerminalUserService terminalUserService;
 
     @Autowired
     private TerminalDeviceService terminalDeviceService;
 
+    @Autowired
+    private RedisService redisService;
+
+    public static String getUserFormatKey(String uid) {
+        return String.format(USER_FORMAT_KEY, uid);
+    }
+
+    public Boolean deleteCacheUser(String userId) {
+        String key = getUserFormatKey(userId);
+        redisService.delete(key);
+        return Boolean.TRUE;
+    }
+
     @RequestMapping(value = "/unbind/{userId}", method = RequestMethod.DELETE)
     public APIResult<Boolean> deviceUnbind(@PathVariable String userId) {
         APIResult<TerminalUserVo> userApiResult = terminalUserService.getUser(userId);
         if (!userApiResult.getSuccess()) {
             return APIResult.error(new BaseCode(userApiResult.getCode(), userApiResult.getMessage()));
         }
+        // 清空缓存
+        deleteCacheUser(userId);
         return terminalDeviceService.deviceUnbind(userId);
     }
 }

+ 32 - 6
rankin-user-service/src/main/java/cn/rankin/userservice/controller/WhiteUserController.java

@@ -3,16 +3,14 @@ package cn.rankin.userservice.controller;
 import cn.rankin.common.utils.api.model.APIResult;
 import cn.rankin.common.utils.api.page.Page;
 import cn.rankin.common.utils.enums.BaseOrderEnum;
+import cn.rankin.data.api.user.dto.WhiteUserDTO;
 import cn.rankin.data.api.user.dto.WhiteUserSearchDTO;
 import cn.rankin.data.api.user.entity.WhiteUser;
 import cn.rankin.userservice.code.UserServiceAPICode;
 import cn.rankin.userservice.service.WhiteUserService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.StringUtils;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import java.util.LinkedHashMap;
 
@@ -32,6 +30,34 @@ public class WhiteUserController {
         return APIResult.ok(whiteUser);
     }
 
+    @RequestMapping(method = RequestMethod.POST)
+    public APIResult<WhiteUser> create(@RequestBody WhiteUserDTO whiteUserDTO) {
+        WhiteUser whiteUser = whiteUserService.addUser(whiteUserDTO);
+        if (whiteUser != null) {
+            return APIResult.ok(whiteUser);
+        }
+        return APIResult.error(UserServiceAPICode.error("创建失败"));
+    }
+
+    @RequestMapping(method = RequestMethod.PUT)
+    public APIResult<WhiteUser> update(@RequestBody WhiteUserDTO whiteUserDTO) {
+        WhiteUser whiteUser = whiteUserService.updateUser(whiteUserDTO);
+        if (whiteUser != null) {
+            return APIResult.ok(whiteUser);
+        }
+        return APIResult.error(UserServiceAPICode.error("更新失败"));
+    }
+
+    @RequestMapping(method = RequestMethod.DELETE)
+    public APIResult<Boolean> delete(@RequestBody WhiteUserDTO whiteUserDTO) {
+        String userId = whiteUserDTO.getUserId();
+        Boolean result = whiteUserService.deleteUser(userId);
+        if (Boolean.TRUE.equals(result)) {
+            return APIResult.ok(result);
+        }
+        return APIResult.error(UserServiceAPICode.NOT_EXISTS);
+    }
+
     @RequestMapping(value = "/list", method = RequestMethod.GET)
     public APIResult<Page<WhiteUser>> search(WhiteUserSearchDTO searchDTO) {
         WhiteUser sample = new WhiteUser();
@@ -43,7 +69,7 @@ public class WhiteUserController {
         Integer pageSize = searchDTO.getPageSize();
         LinkedHashMap<String, BaseOrderEnum> sort = new LinkedHashMap<>();
         sort.put("gmtModified", BaseOrderEnum.DESC);
-//        return whiteUserService.search(sample, pageNo, pageSize, sort);
-        return null;
+        Page<WhiteUser> page = whiteUserService.search(sample, pageNo, pageSize, sort);
+        return APIResult.ok(page);
     }
 }

+ 4 - 3
rankin-user-service/src/main/java/cn/rankin/userservice/service/WhiteUserService.java

@@ -4,6 +4,7 @@ import cn.rankin.common.utils.api.model.APIResult;
 import cn.rankin.common.utils.api.page.Page;
 import cn.rankin.common.utils.enums.BaseOrderEnum;
 import cn.rankin.common.utils.enums.BaseStatusEnum;
+import cn.rankin.common.utils.util.JpaSortUtil;
 import cn.rankin.data.api.user.dto.WhiteUserDTO;
 import cn.rankin.data.api.user.entity.WhiteUser;
 import cn.rankin.userservice.repository.WhiteUserRepository;
@@ -26,7 +27,7 @@ public class WhiteUserService {
     public WhiteUser addUser(WhiteUserDTO whiteUserDTO) {
         WhiteUser whiteUser = convert(whiteUserDTO);
         WhiteUser result = whiteUserRepository.save(whiteUser);
-        return null;
+        return result;
     }
 
     @Transactional
@@ -39,13 +40,13 @@ public class WhiteUserService {
         return whiteUserRepository.findFirstByUserIdAndAndStatus(userId, BaseStatusEnum.NORMAL);
     }
 
-    public Page<WhiteUser> searchPage(WhiteUser whiteUser, Integer pageNo, Integer pageSize, LinkedHashMap<String, BaseOrderEnum> sort) {
+    public Page<WhiteUser> search(WhiteUser whiteUser, Integer pageNo, Integer pageSize, LinkedHashMap<String, BaseOrderEnum> sort) {
         Long count = whiteUserRepository.count(whiteUser);
         Page<WhiteUser> page = new Page(count, pageNo, pageSize);
         if (count == 0) {
             return page;
         }
-        List<WhiteUser> whiteUserList = whiteUserRepository.find(whiteUser, page.getStart(), pageSize);
+        List<WhiteUser> whiteUserList = whiteUserRepository.find(whiteUser, page.getStart(), pageSize, JpaSortUtil.sort(sort));
         page.setList(whiteUserList);
         return page;
     }