Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature:support offline atomic command by lua #149

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<CIMUserInfo> onlineUser() {
Set<CIMUserInfo> set = null ;
Expand Down
5 changes: 5 additions & 0 deletions cim-forward-route/src/main/resources/lua/offLine.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

redis.call('DEL', KEYS[1])

redis.call('SREM', ARGV[1], ARGV[2])

Original file line number Diff line number Diff line change
Expand Up @@ -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<CIMUserInfo> cimUserInfos = userInfoCacheService.onlineUser();
Expand Down
Loading