版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34207422/article/details/84941982
1、在pom文件中添加如下配置
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、在application.properties中添加如下的配置
#redis
spring.redis.host=
spring.redis.port=6379
spring.redis.password=
spring.redis.database=1
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=500
spring.redis.pool.min-idle=0
spring.redis.timeout=0
3、在启动上加上启动缓存的注解
@EnableCaching
4、使用redis缓存
4.1 直接使用springboot的redisTemplet对象(注意这种方法注入RedisTemplate对象的时候需要使用@Resource进行注入) 主要代码如下
import org.springframework.data.redis.core.RedisTemplate;
@Resource
private RedisTemplate<String, Object> redisTemplate;
// 存取字符串
redisTemplate.opsForValue().set("a", "1");
Object a = redisTemplate.opsForValue().get("a");
System.out.println(a);
redisTemplate.opsForValue().set("b", "劳斯莱斯是");
Object b = redisTemplate.opsForValue().get("b");
System.out.println(b);
// 存取user对象
Student stu = new Student();
stu.setId("111");
stu.setName("浙江安吉三年");
stu.setScore("244");
redisTemplate.opsForValue().set("student", stu);
Student student = (Student) redisTemplate.opsForValue().get("student");
System.out.println(student);
4.2 使用RedisUtil封装类
4.2.1 RedisUtil代码如下
package com.zw.util;
import java.io.Serializable;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;
/**
*
* @Description: spring boot 的redis工具类
*/
@SuppressWarnings("unchecked")
@Component
public class RedisUtil {
@Autowired
private RedisTemplate redisTemplate;
/**
* 批量删除对应的value
*
* @param keys
*/
public void remove(final String... keys) {
for (String key : keys) {
remove(key);
}
}
/**
* 批量删除key
*
* @param pattern
*/
public void removePattern(final String pattern) {
Set<Serializable> keys = redisTemplate.keys(pattern);
if (keys.size() > 0)
redisTemplate.delete(keys);
}
/**
* 删除对应的value
*
* @param key
*/
public void remove(final String key) {
if (exists(key)) {
redisTemplate.delete(key);
}
}
/**
* 判断缓存中是否有对应的value
*
* @param key
* @return
*/
public boolean exists(final String key) {
return redisTemplate.hasKey(key);
}
/**
* 读取缓存
*
* @param key
* @return
*/
public String get(final String key) {
Object result = null;
redisTemplate.setValueSerializer(new StringRedisSerializer());
ValueOperations<Serializable, Object> operations = redisTemplate
.opsForValue();
result = operations.get(key);
if (result == null) {
return null;
}
return result.toString();
}
/**
* 写入缓存
*
* @param key
* @param value
* @return
*/
public boolean set(final String key, String value) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate
.opsForValue();
operations.set(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 写入缓存
*
* @param key
* @param value
* @return
*/
public boolean set(final String key, Object value, Long expireTime) {
boolean result = false;
try {
ValueOperations<Serializable, Object> operations = redisTemplate
.opsForValue();
operations.set(key, value);
redisTemplate.opsForValue().set("b", "劳斯莱斯是");
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public boolean hmset(String key, Map<String, String> value) {
boolean result = false;
try {
redisTemplate.opsForHash().putAll(key, value);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public Map<String, String> hmget(String key) {
Map<String, String> result = null;
try {
result = redisTemplate.opsForHash().entries(key);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
4.2.2 调用RedisUtil对象操作redis 主要代码如下
@Autowired
private RedisUtil redisUtil;
//注意在调用junit的时候这里一定要注入,不能直接new对象出来
System.out.println("存在aa吗?"+redisUtil.exists("aa"));
System.out.println("aa的值"+ redisUtil.get("aa"));
redisUtil.set("aa", "张三");
redisUtil.set("ab", "李四", 10L);
Map< String, String> map = new HashMap<String, String>();
map.put("ac", "王五");
map.put("ad", "赵六");
redisUtil.hmset("ac", map);
redisUtil.remove("aa");