SpringSession通过Redis统计在线用户数量的实现代码(springsession实现session共享条件)真没想到

随心笔谈9个月前发布 admin
196 00
🌐 经济型:买域名、轻量云服务器、用途:游戏 网站等 《腾讯云》特点:特价机便宜 适合初学者用 点我优惠购买
🚀 拓展型:买域名、轻量云服务器、用途:游戏 网站等 《阿里云》特点:中档服务器便宜 域名备案事多 点我优惠购买
🛡️ 稳定型:买域名、轻量云服务器、用途:游戏 网站等 《西部数码》 特点:比上两家略贵但是稳定性超好事也少 点我优惠购买

文章摘要

这篇文章介绍了名为`com.common.utils.redis`的Java包,该包提供了与Redis数据库交互的各种静态方法。文章重点围绕Redis操作展开,包括字符串操作(如设置、删除、获取)、哈希操作(如哈希表的存储与获取)、集合操作(如集合的添加与删除)以及列表操作(如列表的追加与获取)。这些方法通过`RedisTemplate`类执行Redis命令,返回相应的操作结果。文章还展示了如何使用这些方法进行高效的数据库操作,提升了应用的性能和灵活性。

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);

public static boolean expire(final String key, final long timeout) {

return expire(key, timeout, TimeUnit.SECONDS);
}

public static boolean expire(final String key, final long timeout, final TimeUnit unit) {

Boolean ret=redisTemplate.expire(key, timeout, unit);
return ret !=null && ret;
}

public static boolean del(final String key) {

redisTemplate.delete(key);
return true;
}

public static long del(final Collection<String> keys) {

redisTemplate.delete(keys);
return 0;
}

public static void set(final String key, final Object value) {

redisTemplate.opsForValue().set(key, value, 1, TimeUnit.MINUTES);
}

// 存储普通对象操作

public static void set(final String key, final Object value, final long timeout) {

redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
}

public static Object get(final String key) {

return redisTemplate.opsForValue().get(key);
}

// 存储Hash操作

public static void hPut(final String key, final String hKey, final Object value) {

redisTemplate.opsForHash().put(key, hKey, value);
}

public static void hPutAll(final String key, final Map<String, Object> values) {

redisTemplate.opsForHash().putAll(key, values);
}

public static Object hGet(final String key, final String hKey) {

return redisTemplate.opsForHash().get(key, hKey);
}

public static List<Object> hMultiGet(final String key, final Collection<Object> hKeys) {

return redisTemplate.opsForHash().multiGet(key, hKeys);
}

// 存储Set相关操作

public static long sSet(final String key, final Object… values) {
Long count=redisTemplate.opsForSet().add(key, values);
return count==null ? 0 : count;
}

public static long sDel(final String key, final Object… values) {
Long count=redisTemplate.opsForSet().remove(key, values);
return count==null ? 0 : count;
}

// 存储List相关操作

public static long lPush(final String key, final Object value) {
Long count=redisTemplate.opsForList().rightPush(key, value);
return count==null ? 0 : count;
}

public static long lPushAll(final String key, final Collection<Object> values) {
Long count=redisTemplate.opsForList().rightPushAll(key, values);
return count==null ? 0 : count;
}

public static long lPushAll(final String key, final Object… values) {
Long count=redisTemplate.opsForList().rightPushAll(key, values);
return count==null ? 0 : count;
}

public static List<Object> lGet(final String key, final int start, final int end) {
return redisTemplate.opsForList().range(key, start, end);
}
}

© 版权声明

相关文章