先导入需要的maven包还有redis配置
pom:
<dependency
<groupId org.springframework.boot</groupId
<artifactId spring-boot-starter-data-redis</artifactId
</dependency
#properties
#Redis数据库索引(默认为0)
spring.redis.database=0
#Redis服务器地址
spring.redis.host=localhost
#Redis服务器连接端口
spring.redis.port=6379
#Redis服务器连接密码(默认为空)
spring.redis.password=
#连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=20
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=10
#连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
#连接超时时间(毫秒)
spring.redis.timeout=1000
## 抽取的序列化工具类
/**
序列化工具类
/
public class SerializeUtil {
/*
* 单个序列化
* @param object
* @return
*/
public static byte[] serialize(Object object) {
if (object == null) {
return null;
}
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
byte[] bytes = null;
try {
// 序列化
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
bytes = baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
close(oos);
close(baos);
}
return bytes;
}
/**
* 单个反序列化
*
* @param bytes
* @return
*/
public static Object unserialize(byte[] bytes) {
if (bytes == null) {
return null;
}
ByteArrayInputStream bais = null;
ObjectInputStream ois = null;
try {
// 反序列化
bais = new ByteArrayInputStream(bytes);
ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
e.printStackTrace();
} finally {
close(bais);
close(ois);
}
return null;
}
/**
* 序列化 list 集合
*
* @param list
* @return
*/
public static byte[] serializeList(List<?> list) {
if (list == null || list.size() == 0) {
return null;
}
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
byte[] bytes = null;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
for (Object obj : list) {
oos.writeObject(obj);
}
bytes = baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
close(oos);
close(baos);
}
return bytes;
}
/**
* 反序列化 list 集合
*
* @param lb
* @return
*/
public static List<?> unserializeList(byte[] bytes) {
if (bytes == null) {
return null;
}
List<Object> list = new ArrayList<Object>();
ByteArrayInputStream bais = null;
ObjectInputStream ois = null;
try {
// 反序列化
bais = new ByteArrayInputStream(bytes);
ois = new ObjectInputStream(bais);
while (bais.available() > 0) {
Object obj = (Object) ois.readObject();
if (obj == null) {
break;
}
list.add(obj);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
close(bais);
close(ois);
}
return list;
}
/**
* 关闭io流对象
*
* @param closeable
*/
public static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
## 一个简单的测试redis
//注入redisTemplate
@Autowired
private StringRedisTemplate redisTemplate;
@Test
public void setRedis() {
1
2
//缓存中最常用的方法
redisTemplate.opsForValue().set(“first”,“siwei”);
//设置缓存过期时间为30 单位:秒
//关于TimeUnit下面有部分源码截图
redisTemplate.opsForValue().set(“second”,“siweiWu”,30, TimeUnit.SECONDS);
System.out.println(“存入缓存成功”);
}
@Test
public void getRedis(){
String first = redisTemplate.opsForValue().get(“first”);
String second = redisTemplate.opsForValue().get(“second”);
System.out.println("取出缓存中first的数据是:"+first);
System.out.println("取出缓存中second的数据是:"+second);
}
@Test
public void delRedis() {
//根据key删除缓存
Boolean first1 = redisTemplate.delete("first");
System.out.println("是否删除成功:"+first1);
}
版权声明:本文为Chen_1999原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。