SpringSession通過Redis統(tǒng)計在線用戶數(shù)量的實現(xiàn)代碼
最近遇到一個比較緊急的任務(wù),要求統(tǒng)計在線用戶,目的是配合性能測評,要求證明自己系統(tǒng)的在線用戶能夠達(dá)標(biāo),不過系統(tǒng)因為歷史原因,并沒有這個功能,所以只能去springSession官網(wǎng)和網(wǎng)上搜資料,想到通過統(tǒng)計redis里緩存的數(shù)據(jù)
因為系統(tǒng)原先的邏輯是使用Spring Session加上Redis做的會話共享實現(xiàn)的單點登錄,登錄之后會在session設(shè)置一個key值表示用戶已經(jīng)登錄過,同時重寫HttpServletRequestWrapper 設(shè)置remoteUser數(shù)據(jù)值
class RemoteUserRequestWrapper extends HttpServletRequestWrapper {
String userCode;
RemoteUserRequestWrapper(HttpServletRequest request) {
super(request);
this.userCode = (String) request.getSession()
.getAttribute(org.apache.commons.lang3.StringUtils.isBlank(sessionKeyName)?DEFAULT_SESSION_KEY_NAME:sessionKeyName);
}
@Override
public String getRemoteUser() {
return userCode;
}
}Spring Session緩存在redis里的數(shù)據(jù)

這個ssoLoginUser key是自己登錄時候設(shè)置的,根據(jù)業(yè)務(wù)修改,經(jīng)過測試,在登出系統(tǒng)時候,session設(shè)置過期獲取removeAttribute不能清redis里的key數(shù)據(jù),所以只能在登出系統(tǒng)邏輯加上:
Set<String> keys = RedisUtils.redisTemplate.keys("spring:session:sessions:*");
for(String key : keys){
if(key.indexOf("expires")==-1){
String s = (String)RedisUtils.redisTemplate.opsForHash().get(key, "sessionAttr:ssoLoginUser");
if(request.getRemoteUser().equals(s)) {
logger.info("loginusername:{}",s)
RedisUtils.redisTemplate.opsForHash().delete(key, "sessionAttr:ssoLoginUser");
}
}
}進(jìn)行數(shù)據(jù)統(tǒng)計:
List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
List<Map<String,Object>> data = new ArrayList<Map<String, Object>>();
Set<String> keys = redisTemplate.keys("spring:session:sessions:*");
for(String key : keys){
if(key.indexOf("expires")==-1){
String s = (String)redisTemplate.opsForHash().get(key, "sessionAttr:ssoLoginUser");
if(StringUtils.isNotBlank(s)) {
System.out.println(s);
Map<String,Object> map = new HashMap<String,Object>(16);
map.put("usercode", s);
list.add(map);
}
}
}
return list;pom.xml:
<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> <version>1.2.2.RELEASE</version> <type>pom</type> </dependency> <dependency> <groupId>biz.paluch.redis</groupId> <artifactId>lettuce</artifactId> <version>3.5.0.Final</version> </dependency>
RedisUtils.java:
package com.common.utils.redis;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class RedisUtils {
private RedisUtils() {
}
@SuppressWarnings("unchecked")
public static RedisTemplate<String, Object> redisTemplate =
ContextLoader.getCurrentWebApplicationContext().getBean(RedisTemplate.class);
/**
* 設(shè)置有效時間
*
* @param key Redis鍵
* @param timeout 超時時間
* @return true=設(shè)置成功;false=設(shè)置失敗
*/
public static boolean expire(final String key, final long timeout) {
return expire(key, timeout, TimeUnit.SECONDS);
}
/**
* 設(shè)置有效時間
*
* @param key Redis鍵
* @param timeout 超時時間
* @param unit 時間單位
* @return true=設(shè)置成功;false=設(shè)置失敗
*/
public static boolean expire(final String key, final long timeout, final TimeUnit unit) {
Boolean ret = redisTemplate.expire(key, timeout, unit);
return ret != null && ret;
}
/**
* 刪除單個key
*
* @param key 鍵
* @return true=刪除成功;false=刪除失敗
*/
public static boolean del(final String key) {
redisTemplate.delete(key);
return true;
}
/**
* 刪除多個key
*
* @param keys 鍵集合
* @return 成功刪除的個數(shù)
*/
public static long del(final Collection<String> keys) {
redisTemplate.delete(keys);
return 0;
}
/**
* 存入普通對象
*
* @param key Redis鍵
* @param value 值
*/
public static void set(final String key, final Object value) {
redisTemplate.opsForValue().set(key, value, 1, TimeUnit.MINUTES);
}
// 存儲普通對象操作
/**
* 存入普通對象
*
* @param key 鍵
* @param value 值
* @param timeout 有效期,單位秒
*/
public static void set(final String key, final Object value, final long timeout) {
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
}
/**
* 獲取普通對象
*
* @param key 鍵
* @return 對象
*/
public static Object get(final String key) {
return redisTemplate.opsForValue().get(key);
}
// 存儲Hash操作
/**
* 往Hash中存入數(shù)據(jù)
*
* @param key Redis鍵
* @param hKey Hash鍵
* @param value 值
*/
public static void hPut(final String key, final String hKey, final Object value) {
redisTemplate.opsForHash().put(key, hKey, value);
}
/**
* 往Hash中存入多個數(shù)據(jù)
*
* @param key Redis鍵
* @param values Hash鍵值對
*/
public static void hPutAll(final String key, final Map<String, Object> values) {
redisTemplate.opsForHash().putAll(key, values);
}
/**
* 獲取Hash中的數(shù)據(jù)
*
* @param key Redis鍵
* @param hKey Hash鍵
* @return Hash中的對象
*/
public static Object hGet(final String key, final String hKey) {
return redisTemplate.opsForHash().get(key, hKey);
}
/**
* 獲取多個Hash中的數(shù)據(jù)
*
* @param key Redis鍵
* @param hKeys Hash鍵集合
* @return Hash對象集合
*/
public static List<Object> hMultiGet(final String key, final Collection<Object> hKeys) {
return redisTemplate.opsForHash().multiGet(key, hKeys);
}
// 存儲Set相關(guān)操作
/**
* 往Set中存入數(shù)據(jù)
*
* @param key Redis鍵
* @param values 值
* @return 存入的個數(shù)
*/
public static long sSet(final String key, final Object... values) {
Long count = redisTemplate.opsForSet().add(key, values);
return count == null ? 0 : count;
}
/**
* 刪除Set中的數(shù)據(jù)
*
* @param key Redis鍵
* @param values 值
* @return 移除的個數(shù)
*/
public static long sDel(final String key, final Object... values) {
Long count = redisTemplate.opsForSet().remove(key, values);
return count == null ? 0 : count;
}
// 存儲List相關(guān)操作
/**
* 往List中存入數(shù)據(jù)
*
* @param key Redis鍵
* @param value 數(shù)據(jù)
* @return 存入的個數(shù)
*/
public static long lPush(final String key, final Object value) {
Long count = redisTemplate.opsForList().rightPush(key, value);
return count == null ? 0 : count;
}
/**
* 往List中存入多個數(shù)據(jù)
*
* @param key Redis鍵
* @param values 多個數(shù)據(jù)
* @return 存入的個數(shù)
*/
public static long lPushAll(final String key, final Collection<Object> values) {
Long count = redisTemplate.opsForList().rightPushAll(key, values);
return count == null ? 0 : count;
}
/**
* 往List中存入多個數(shù)據(jù)
*
* @param key Redis鍵
* @param values 多個數(shù)據(jù)
* @return 存入的個數(shù)
*/
public static long lPushAll(final String key, final Object... values) {
Long count = redisTemplate.opsForList().rightPushAll(key, values);
return count == null ? 0 : count;
}
/**
* 從List中獲取begin到end之間的元素
*
* @param key Redis鍵
* @param start 開始位置
* @param end 結(jié)束位置(start=0,end=-1表示獲取全部元素)
* @return List對象
*/
public static List<Object> lGet(final String key, final int start, final int end) {
return redisTemplate.opsForList().range(key, start, end);
}
}
ok,本博客只能學(xué)習(xí)參考,因為只是要給客戶一些在線用戶的證明而已,這個臨時的統(tǒng)計不能用于生產(chǎn),要做比較齊全的在線用戶統(tǒng)計,需要花多點時間,有問題希望能指出。ok,簡單記錄一下,方便之后自己回顧
到此這篇關(guān)于SpringSession通過Redis統(tǒng)計在線用戶數(shù)量的文章就介紹到這了,更多相關(guān)Redis在線用戶數(shù)量內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
redis.conf中使用requirepass不生效的原因及解決方法
本文主要介紹了如何啟用requirepass,以及啟用requirepass為什么不會生效,從代碼層面分析了不生效的原因,以及解決方法,需要的朋友可以參考下2023-07-07
Redis之SDS數(shù)據(jù)結(jié)構(gòu)的使用
本文主要介紹了Redis之SDS數(shù)據(jù)結(jié)構(gòu)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08

