Spring Boot StringRedisTemplateの一般的な操作

3769 ワード

Redisの穴
Redisドキュメントに記載されている保存またはクエリーは、Objectタイプを使用できますが、実際にはStringタイプが使用されます.
POM依存


    org.springframework.boot
    spring-boot-starter-data-redis


application.properties
# Redis
[email protected]@
[email protected]@
spring.redis.port=${port:6379}
#         (          )
spring.redis.pool.max-active=8
#            (          )
spring.redis.pool.max-wait=-1
#            
spring.redis.pool.max-idle=8
#            
spring.redis.pool.min-idle=1
#       (  )
spring.redis.timeout=300

注入オブジェクト
@Resource
StringRedisTemplate stringRedisTemplate;

Redis Value
@Test
public void valueAddResitTest(){
    stringRedisTemplate.opsForValue().set("key","value");
}

@Test
public void valueGetResitTest(){
    String value = stringRedisTemplate.opsForValue().get("key");
    logger.info("value:{}", value);
}

@Test
public void valueDelResitTest(){
    stringRedisTemplate.delete("key");
}

@Test
public void valueTimeoutResitTest(){
    stringRedisTemplate.opsForValue().set("timeStep", new Date().getTime()+"", 2 ,TimeUnit.MINUTES);
}

Redis List
// list              :  12306     ,                  !           ,        
@Test
public void listPushResitTest(){
    // leftPush       
    stringRedisTemplate.opsForList().rightPush("myList","1");
    stringRedisTemplate.opsForList().rightPush("myList","2");
    stringRedisTemplate.opsForList().rightPush("myList", "A");
    stringRedisTemplate.opsForList().rightPush("myList", "B");
    // leftPush       
    stringRedisTemplate.opsForList().leftPush("myList", "0");
}

@Test
public void listGetListResitTest(){
    //         
    List listAll = stringRedisTemplate.opsForList().range( "myList", 0, -1);
    logger.info("list all {}", listAll);
    //    3   
    List list = stringRedisTemplate.opsForList().range( "myList", 0, 3);
    logger.info("list limit {}", list);
}

@Test
public void listRemoveOneResitTest(){
    //       B  
    stringRedisTemplate.opsForList().remove("myList",1, "B");
}

@Test
public void listRemoveAllResitTest(){
    //     A  
    stringRedisTemplate.opsForList().remove("myList",0, "A");
}

Redis Hash
@Test
public void hashPutResitTest(){
    // map key   ,         
    stringRedisTemplate.opsForHash().put("banks:12600000", "a", "b");
}

@Test
public void hashGetEntiresResitTest(){
    //   map  
    Map map = stringRedisTemplate.opsForHash().entries("banks:12600000");
    logger.info("objects:{}", map);
}

@Test
public void hashGeDeleteResitTest(){
    //   map key      
    stringRedisTemplate.opsForHash().delete("banks:12600000", "c");
}

@Test
public void hashGetKeysResitTest(){
    //   map key  
    Set objects =  stringRedisTemplate.opsForHash().keys("banks:12600000");
    logger.info("objects:{}", objects);
}

@Test
public void hashGetValueListResitTest(){
    //   map value  
    List objects = stringRedisTemplate.opsForHash().values("banks:12600000");
    logger.info("objects:{}", objects);
}

@Test
public void hashSize() {
    //   map    
    long size =  stringRedisTemplate.opsForHash().size("banks:12600000");
    logger.info("size:{}", size);
}