diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/UserInfoCacheService.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/UserInfoCacheService.java index a76e7da0..7e71d8e6 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/UserInfoCacheService.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/UserInfoCacheService.java @@ -29,13 +29,6 @@ public interface UserInfoCacheService { */ boolean saveAndCheckUserLoginStatus(Long userId) throws Exception ; - /** - * 清除用户的登录状态 - * @param userId - */ - void removeLoginStatus(Long userId) ; - - /** * query all online user * @return online user diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/AccountServiceRedisImpl.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/AccountServiceRedisImpl.java index 4b10699c..dc06ded2 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/AccountServiceRedisImpl.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/AccountServiceRedisImpl.java @@ -18,19 +18,22 @@ import okhttp3.OkHttpClient; import okhttp3.Response; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.ClassPathResource; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.Cursor; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ScanOptions; +import org.springframework.data.redis.core.script.DefaultRedisScript; +import org.springframework.scripting.support.ResourceScriptSource; import org.springframework.stereotype.Service; import java.nio.charset.StandardCharsets; +import java.util.Collections; import java.util.HashMap; import java.util.Map; import static com.crossoverjie.cim.common.enums.StatusEnum.OFF_LINE; -import static com.crossoverjie.cim.route.constant.Constant.ACCOUNT_PREFIX; -import static com.crossoverjie.cim.route.constant.Constant.ROUTE_PREFIX; +import static com.crossoverjie.cim.route.constant.Constant.*; /** * Function: @@ -158,12 +161,12 @@ public void pushMsg(CIMServerResVO cimServerResVO, long sendUserId, ChatReqVO gr @Override public void offLine(Long userId) { - // TODO: 2019-01-21 改为一个原子命令,以防数据一致性 + DefaultRedisScript redisScript = new DefaultRedisScript(); + redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("lua/offLine.lua"))); - //删除路由 - redisTemplate.delete(ROUTE_PREFIX + userId); - - //删除登录状态 - userInfoCacheService.removeLoginStatus(userId); + redisTemplate.execute(redisScript, + Collections.singletonList(ROUTE_PREFIX + userId), + LOGIN_STATUS_PREFIX, + userId.toString()); } } diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java index cfbe4b97..6f709a45 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java @@ -63,11 +63,6 @@ public boolean saveAndCheckUserLoginStatus(Long userId) throws Exception { } } - @Override - public void removeLoginStatus(Long userId) { - redisTemplate.opsForSet().remove(LOGIN_STATUS_PREFIX,userId.toString()) ; - } - @Override public Set onlineUser() { Set set = null ; diff --git a/cim-forward-route/src/main/resources/lua/offLine.lua b/cim-forward-route/src/main/resources/lua/offLine.lua new file mode 100644 index 00000000..7fc5d82a --- /dev/null +++ b/cim-forward-route/src/main/resources/lua/offLine.lua @@ -0,0 +1,5 @@ + +redis.call('DEL', KEYS[1]) + +redis.call('SREM', ARGV[1], ARGV[2]) + diff --git a/cim-forward-route/src/test/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImplTest.java b/cim-forward-route/src/test/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImplTest.java index 0217f8da..51865314 100644 --- a/cim-forward-route/src/test/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImplTest.java +++ b/cim-forward-route/src/test/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImplTest.java @@ -23,11 +23,6 @@ public void checkUserLoginStatus() throws Exception { log.info("status={}", status); } - @Test - public void removeLoginStatus() throws Exception { - userInfoCacheService.removeLoginStatus(2000L); - } - @Test public void onlineUser(){ Set cimUserInfos = userInfoCacheService.onlineUser();