循环遍历获取 redis key实现在用户在线,性能不高,尤其是用户数量多的情况容易导致其他的redis线程卡死.这边用 redis hash维护一个现在用户,只要简单的实现用户同步就可以了.提交了性能
This commit is contained in:
@ -61,6 +61,12 @@ public class Constants
|
||||
* 登录用户 redis key
|
||||
*/
|
||||
public static final String LOGIN_TOKEN_KEY = "login_tokens:";
|
||||
|
||||
|
||||
/**
|
||||
* 登录当前在线 用户 redis key
|
||||
*/
|
||||
public static final String LOGIN_TOKEN_KEY_ONLINE = "login_tokens_onlie:";
|
||||
|
||||
/**
|
||||
* 防重提交 redis key
|
||||
|
@ -9,6 +9,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.HashOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
@ -23,6 +25,7 @@ public class RedisCache
|
||||
@Autowired
|
||||
public RedisTemplate redisTemplate;
|
||||
|
||||
|
||||
/**
|
||||
* 缓存基本的对象,Integer、String、实体类等
|
||||
*
|
||||
@ -47,6 +50,8 @@ public class RedisCache
|
||||
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 设置有效时间
|
||||
*
|
||||
@ -189,6 +194,20 @@ public class RedisCache
|
||||
redisTemplate.opsForHash().put(key, hKey, value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 缓存基本的对象,Integer、String、实体类等
|
||||
*
|
||||
* @param key 缓存的键值
|
||||
* @param value 缓存的值
|
||||
* @param timeout 时间
|
||||
* @param timeUnit 时间颗粒度
|
||||
*/
|
||||
public boolean delCacheMapKey(final String key,final String... token)
|
||||
{
|
||||
return redisTemplate.opsForHash().delete(key,token)>=1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取Hash中的数据
|
||||
*
|
||||
@ -224,4 +243,9 @@ public class RedisCache
|
||||
{
|
||||
return redisTemplate.keys(pattern);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user