win安装redis客户端
https://github.com/tporadowski/redis/releases下载zip文件解压
redis-server.exe运行redis
pom添加redis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
properties配置redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
#数据库索引默认0
spring.redis.database=0
redis使用示例
import com.alibaba.fastjson.JSON;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/**
* redis操作五种数据类型:String、List、Hash、Set、Sorted Set
*/
@SpringBootTest
public class RedisTests {
@Autowired
StringRedisTemplate template;
@Test
public void addData() {
//添加字符串数据
template.opsForValue().set("kstr1", "str1");
template.opsForValue().set("kstr2", "str2");
template.opsForValue().append("kstr2", " append");
//添加列表数据[leftPush添加到当前集合左边]
template.opsForList().leftPush("klist", "akali");
template.opsForList().leftPush("klist", "annie");
template.opsForList().rightPush("klist", "ashe");
//添加字典数据
template.opsForHash().put("person", "name", "akali");
template.opsForHash().put("person", "age", "20");
//添加集合数据
template.opsForSet().add("kset", "akali");
template.opsForSet().add("kset", "amumu");
template.opsForSet().add("kset", "amumu");
//添加有序集合数据
template.opsForZSet().add("kzset", "akali", 2);
template.opsForZSet().add("kzset", "ashe", 3);
template.opsForZSet().add("kzset", "ashe", 1);
}
@Test
public void showData() {
//打印字符串数据
System.out.println(template.opsForValue().get("kstr1"));
System.out.println(template.opsForValue().get("kstr2"));
//打印列表集合
System.out.println(template.opsForList().range("klist", 0, -1));
//打印字典数据
System.out.println(JSON.toJSONString(template.opsForHash().entries("person")));
//打印集合数据
System.out.println(JSON.toJSONString(template.opsForSet().members("kset")));
//打印有序集合数据
System.out.println(JSON.toJSONString(template.opsForZSet().range("kzset", 0, -1)));
}
/**清空redis所有数据*/
@Test
public void clear() {
template.delete(Objects.requireNonNull(template.keys("*")));
}
/**查询某键是否存在*/
@Test
public void exists() {
System.out.println("hasKey:" + template.hasKey("person"));
}
/**设置某键失效时间*/
/*立即取值:ex sleep450毫秒后取值:ex sleep500毫秒后取值:null*/
@Test
public void expire() throws InterruptedException {
template.opsForValue().set("kexpire", "ex");
template.expire("kexpire", 500, TimeUnit.MILLISECONDS);
System.out.println("立即取值:" + template.opsForValue().get("kexpire"));
Thread.sleep(450);
System.out.println("sleep450毫秒后取值:" + template.opsForValue().get("kexpire"));
Thread.sleep(500);
System.out.println("sleep500毫秒后取值:" + template.opsForValue().get("kexpire"));
}
/**常见redis缓存存取使用示例:缓存存在从redis中取值,不存在从数据库中取值,取值后缓存至redis*/
@Service
private static class DemoService {
@Autowired
StringRedisTemplate userTemplate;
public String getUsername(String id) {
String username = "";
if (userTemplate.hasKey(id)) {
username = userTemplate.opsForValue().get(id);
} else {
//mapper数据库查询select .. where ID=#{id}
//取值后缓存键值对到redis中
}
return username;
}
}
}