0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SpringDataRedisの使い方入門

Posted at

基本的JavaでRedisデータベースを操作するには、以下の3つの方法があります。
·Jedis
·Lettuce
·SpringDataRedis

ここでは、Spring Data Redis を使った迅速な設定方法と基本操作について共有します。全部で4つのステップがあります。

1.Maven依赖のインポート

以下のようにpom.xmlに依存関係を追加します:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

これで、Spring Data Redisをプロジェクトに導入できます。

2.Redisデータソースの設定

以下は、application.ymlを使用して設定する方法です。
一部のバージョンが低いSpring Bootでは、spring.data.redisの構造で記述するように指摘される場合があります。

  redis:
    host: localhost
    port: 6379
    password: YOUR_PASSWORD
    //使いたいdbを指定 デフォルトはDB0
    database: DB0

3.設定クラスを作成し、RedisTemplate オブジェクトを生成します(オプション)

シリアライザを設定する為です。
文字列またはJSON形式に設定することで、開発者が読みやすくするためです。

@Configuration
@Slf4j
public class RedisConfiguration {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        log.info("Redisテンプレートオブジェクトの作成を開始します。");
        RedisTemplate redisTemplate = new RedisTemplate();
        //Redisの接続ファクトリオブジェクトを設定します。
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        
        //Redisキーのシリアライザを設定します。
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        
        //Value のシリアライザを設定します
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        //Hash KeyとHash Valueのシリアライザを設定します
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

4.RedisTemplateオブジェクトを注入する

注入されたRedisTemplateオブジェクトを使用してRedisを操作します。

(1)5つの基本的なデータ型に対応する操作オブジェクトの作成です。
@SpringBootTest
public class SpringDateRedisTest {
    @Autowired
    private RedisTemplate redisTemplate;
    @Test
    public void testRedisTemplate(){
        System.out.println(redisTemplate);
        ValueOperations valueOperations = redisTemplate.opsForValue(); 
        HashOperations hashOperations = redisTemplate.opsForHash();
        ListOperations listOperations = redisTemplate.opsForList();
        SetOperations setOperations = redisTemplate.opsForSet();
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
    }
(2)String型
    @Test
    public void testString(){
        //set get setex setnx
        redisTemplate.opsForValue().set("name","beijing");
        String name = (String) redisTemplate.opsForValue().get("name");
        System.out.println(name);

        //`setex`を使用して有効期限を設定します。
        redisTemplate.opsForValue().set("city","fujian",3, TimeUnit.MINUTES);

        //setnx
        redisTemplate.opsForValue().setIfAbsent("name","shenyang");

    }
(3)Hash型

    @Test
    public void testHash(){
        //hset hget hdl hkeys hvals
        HashOperations hashOperations = redisTemplate.opsForHash();
        //hset
        hashOperations.put("100","name","tom");
        hashOperations.put("100","age","20");
        //hget
        hashOperations.get("100","name");

        //hkeys
        hashOperations.keys("100");

        //hvals
        hashOperations.values("100");

        //hdel
        hashOperations.delete("100","age");
    }

(4)List型
    public void testList(){
        //lpush lrange rpop llen
        ListOperations listOperations = redisTemplate.opsForList();
        //lpush
        listOperations.leftPushAll("mylist","a","b","c");
        listOperations.leftPush("mylist","d");

        //lrange
        List list = listOperations.range("mylist",0,-1);

        //rpop
        listOperations.rightPop("mylist");

        //llen
        long size = listOperations.size("mylist");
    }
(5)Set型

    public void testSet(){
        //sadd smembers scard sinter sunion srem
        SetOperations setOperations = redisTemplate.opsForSet();
    }
(6)Zset型
    public void testZset(){
        //zadd zrange zincrby zrem
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?