【Redis】Spring/SpringBootはRedis Javaクライアントを動作させます

Redis Java クライアントの操作

1.Jedis
2.Lettuce(主流) <-Spring Data Redis

SpringBoot 操作 Redis の手順

1. Redis ドライバーの依存関係を追加します。
ここに画像の説明を挿入します
2. Redis 接続情報を設定します。

spring.redis.database=0
spring.redis.port=6379
spring.redis.host=127.0.0.1
# 可省略
spring.redis.lettuce.pool.min-idle=5
spring.redis.lettuce.pool.max-idle=10
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=1ms
spring.redis.lettuce.shutdown-timeout=100ms

3. Redis APIに従ってRedisを操作する

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class TestController {
    
    

    private final String _REDIS_KEY = "myapplication_test";

    // Spring Boot 自动装配机制
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/setval")
    public void setVal(String val) {
    
    
        redisTemplate.opsForValue() // 得到操作 redis 的类型
                .set(_REDIS_KEY, val,1000, TimeUnit.SECONDS);
    }
    @RequestMapping("/getval")
    public String getValue() {
    
    
        return (String) redisTemplate.opsForValue()
                .get(_REDIS_KEY);
    }
}

Guess you like

Origin blog.csdn.net/weixin_61341342/article/details/132183822