redis主要是做緩存,比如用戶登錄,未付款的訂單等等。這一篇簡單介紹下基本用法
工程建設
1. pom文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<!--jedis連接池-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!--測試-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
</dependencies>
2. application.properties

3.RedisService
@Service public class RedisService { @Autowired RedisTemplate redisTemplate; public void add(String key, String val){ ValueOperations ops = redisTemplate.opsForValue(); ops.set(key, val); } public String select(String key){ ValueOperations ops = redisTemplate.opsForValue(); return (String) ops.get(key); } }
測試
@RunWith(SpringRunner.class) @SpringBootTest public class RedisTest { @Autowired private RedisService redisService; @Test public void add(){ redisService.add("name","紫霞"); } @Test public void get(){ String v = redisService.select("name"); System.out.println(v); } }
測試通過即可
Redis還有很多,緩沖池等等,自行深入研究
