is Spring Data提供的用于操作redis数据库的模板对象!!! String
Hash:即java中的Map
Set:java中的set无重复元素
List:有下标,可重复
ZSet:有分数可据此排序
实操redisTemplate。结合redis的RMD图形化管理工具来实践
创建SpringBoot项目
引入SpringDataRedis依赖
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-data-redis</ artifactId>
</ dependency>
注入RedisTemplate进行测试。 注意:实验中使用了StringRedisTemplate。原因是要使用图形化工具查看数据情况。
@SpringBootTest
class RedisTemplateItemApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void generalOperation ( ) {
redisTemplate. boundValueOps ( "name" ) . set ( "yh" ) ;
redisTemplate. expire ( "name" , 1 , TimeUnit . MINUTES) ;
BoundValueOperations address = redisTemplate. boundValueOps ( "address" ) ;
address. set ( "bj" ) ;
address. expire ( 1 , TimeUnit . MINUTES) ;
ValueOperations valueOperations = redisTemplate. opsForValue ( ) ;
valueOperations. set ( "class" , "高一七班" , 1 , TimeUnit . MINUTES) ;
stringRedisTemplate. boundValueOps ( "name" ) . set ( "yh" ) ;
stringRedisTemplate. expire ( "name" , 1 , TimeUnit . MINUTES) ;
Boolean name = stringRedisTemplate. delete ( "name" ) ;
System . out. println ( name) ;
Long delete = stringRedisTemplate. delete ( Arrays . asList ( "name" , "zSet" , "list" ) ) ;
System . out. println ( "删除的个数为" + delete) ;
stringRedisTemplate. expire ( "name" , 1 , TimeUnit . MINUTES) ;
Long time = stringRedisTemplate. getExpire ( "name" ) ;
System . out. println ( "该key的过期时间为" + time) ;
Boolean hasKey = stringRedisTemplate. hasKey