文章目录
- 1.相关概念
- 2.环境搭建及测试
1.相关概念
Springboot学习笔记:https://blog.csdn.net/meini32/article/details/132544107
Redis快速入门:https://blog.csdn.net/meini32/article/details/132666283
2.环境搭建及测试
redis相关配置
SpringBoot中的配置
https://blog.csdn.net/weixin_41003771/article/details/102663355
1.引入坐标
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.50</version>
</dependency>
2.配置文件
#redis
redis.host=192.168.33.10
redis.port=6379
redis.timeout=3
redis.password=123456
redis.poolMaxTotal=10
redis.poolMaxIdle=10
redis.poolMaxWait=3
3.redis配置类
@Data
@ConfigurationProperties(prefix = "redis")
@Component
//对应配置文件中的名
public class RedisConfig {
private String host;
private int port;
private int timeout;
private String password;
private int poolMaxTotal;
private int poolMaxIdle;
private int poolMaxWait;
}
4.redis连接池
//创建一个JedisPool,该对象是用于管理Redis连接的池。
@Service
public class RedisPoolFactory {
@Autowired
RedisConfig redisConfig;
@Bean
public JedisPool JedisPoolFactory(){
JedisPoolConfig poolConfig = new JedisPoolConfig();
poolConfig.setMaxIdle(redisConfig.getPoolMaxIdle());
poolConfig.setMaxTotal(redisConfig.getPoolMaxTotal());
poolConfig.setMaxWaitMillis(redisConfig.getPoolMaxWait() * 1000);
JedisPool jp = new JedisPool(poolConfig, redisConfig.getHost(), redisConfig.getPort(),
redisConfig.getTimeout()*1000, redisConfig.getPassword(), 0);
return jp;
}
}
5.redis服务(操作)
//Spring Boot服务,专门用于与Redis数据库进行交互
@Service
public class RedisService {
@Autowired
JedisPool jedisPool;
/**
* 获取当个对象
* */
public <T> T get(String key,Class<T> clazz) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//生成真正的key
String realKey = key;
String str = jedis.get(realKey);
if(!StringUtils.isEmpty(str)) {
T t = stringToBean(str, clazz);
return t;
}
return null;
}finally {
returnToPool(jedis);
}
}
/**
* 设置对象
* */
public <T> boolean set(String key, T value) {
return setex( key, value, 0);
}
/**
* 设置对象 带缓存时间
* */
public <T> boolean setex(String key, T value , int seconds) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String str = beanToString(value);
if(str == null || str.length() <= 0) {
return false;
}
//生成真正的key
String realKey = key;
if(seconds <= 0) {
jedis.set(realKey, str);
}else {
jedis.setex(realKey, seconds, str);
}
return true;
}finally {
returnToPool(jedis);
}
}
/**
* 判断key是否存在
* */
public <T> boolean exists(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//生成真正的key
String realKey = key;
return jedis.exists(realKey);
}finally {
returnToPool(jedis);
}
}
/**
* 增加值
* */
public <T> Long incr(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//生成真正的key
String realKey = key;
return jedis.incr(realKey);
}finally {
returnToPool(jedis);
}
}
/**
* 减少值
* */
public <T> Long decr(String key) {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
//生成真正的key
String realKey = key;
return jedis.decr(realKey);
}finally {
returnToPool(jedis);
}
}
private <T> String beanToString(T value) {
if(value == null) {
return null;
}
Class<?> clazz = value.getClass();
if(clazz == int.class || clazz == Integer.class) {
return ""+value;
}else if(clazz == String.class) {
return (String)value;
}else if(clazz == long.class || clazz == Long.class) {
return ""+value;
}else {
return JSON.toJSONString(value);
}
}
@SuppressWarnings("unchecked")
private <T> T stringToBean(String str, Class<T> clazz) {
if(str == null || str.length() <= 0 || clazz == null) {
return null;
}
if(clazz == int.class || clazz == Integer.class) {
return (T)Integer.valueOf(str);
}else if(clazz == String.class) {
return (T)str;
}else if(clazz == long.class || clazz == Long.class) {
return (T)Long.valueOf(str);
}else {
return JSON.toJavaObject(JSON.parseObject(str), clazz);
}
}
private void returnToPool(Jedis jedis) {
if(jedis != null) {
jedis.close();
}
}
}
6.测试
测试一下添加和查看
@SpringBootTest
public class RedisServiceTest {
@Autowired
private RedisService redisService;
@Test
void testRedisInteraction() {
// 设置一个键值对
redisService.set("testKey", "testValue");
// 获取并打印这个值
String value = (String) redisService.get("testKey",String.class);
System.out.println(value);
assertEquals("testValue", value);
}
}
测试结果
控制台输出
到redis中查看是否有这个键值对