Browse Source

redis批量删除用户token

gaodm 5 years ago
parent
commit
bc4725aa19

+ 1 - 3
src/main/java/com/diagbot/facade/SysRoleFacade.java

@@ -198,9 +198,7 @@ public class SysRoleFacade extends SysRoleServiceImpl {
         }
         //清除该角色的所有用户的token缓存
         if (ListUtil.isNotEmpty(userIds)) {
-            for (Long id : userIds) {
-                tokenFacade.deleteToken(id.toString());
-            }
+            tokenFacade.deleteBatchToken(userIds);
         }
         return true;
     }

+ 12 - 1
src/main/java/com/diagbot/service/SysTokenService.java

@@ -2,6 +2,8 @@ package com.diagbot.service;
 
 import com.diagbot.entity.JwtStore;
 
+import java.util.List;
+
 /**
  * @Description: Token验证类
  * @author: gaodm
@@ -27,9 +29,18 @@ public interface SysTokenService {
     Boolean verifyToken(String token, Integer type);
 
     /**
-     *  删除用户token
+     * 删除用户token
+     *
      * @param userId 用户ID
      * @return 删除是否成功
      */
     Boolean deleteToken(String userId);
+
+    /**
+     * 批量删除用户token
+     *
+     * @param userIds 用户列表
+     * @return 删除是否成功
+     */
+    Boolean deleteBatchToken(List<Long> userIds);
 }

+ 26 - 0
src/main/java/com/diagbot/service/impl/SysTokenServiceImpl.java

@@ -16,6 +16,7 @@ import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 
 import java.util.Date;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -143,4 +144,29 @@ public class SysTokenServiceImpl implements SysTokenService {
         });
         return l > 0;
     }
+
+    /**
+     * 批量删除用户token
+     *
+     * @param userIds 用户列表
+     * @return 删除是否成功
+     */
+    @Override
+    public Boolean deleteBatchToken(List<Long> userIds) {
+        Long l = (Long) redisForToken.execute(new RedisCallback<Long>() {
+            @Override
+            public Long doInRedis(RedisConnection connection) throws DataAccessException {
+                connection.openPipeline();
+                Long cnt = 0L;
+                for (Long userId : userIds) {
+                    byte[] redis_key = getUserTokenKey(userId.toString());
+                    connection.del(redis_key);
+                    cnt++;
+                }
+                connection.closePipeline();
+                return cnt;
+            }
+        });
+        return l > 0;
+    }
 }