(Spring Boot)(三)SprigBoot統合Redis


SpringBootはほとんどのプラグインを統合しており、基本的に手順は一致しています。依存性をインポートし、設定ファイルを変更し、プラグインを使用します。今日はSpringBoot統合Redisについて検討します。
1、Redis導入のイニシャル依存
<!--     redis    -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
2、Redisを配置する接続情報
appication.propertiesプロファイルにプロファイルRedisの接続情報を追加します。
#Redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
3、テスト方法を作成し、テストを行う
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netposa.springbootmybatis.domain.User;
import com.netposa.springbootmybatis.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootmybatisApplication.class)
public class testRedis {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Test
    public void test() throws JsonProcessingException {

        // redis          
        String userListData = redisTemplate.boundValueOps("user.findAll").get();
        //  redis       
        if(null==userListData){
            //         
            List<User> all = userMapper.getAllUser();
            //   json     
            ObjectMapper om = new ObjectMapper();
            userListData = om.writeValueAsString(all);
            //      redis ,        redis     ,        
            redisTemplate.boundValueOps("user.findAll").set(userListData);
            System.out.println("===============        ===============");
        }else{
            System.out.println("=============== redis       ===============");
        }
        System.out.println(userListData);
    }
}
このテスト方法は先にRedisに行って指定されたkeyのデータを取得して、もし調べられなかったら、データベースで調べて、結果をRedisに入れて、次の検索に便利です。
ここではRedisの操作方法について簡単に紹介します。
指定されたキーを付与し、値を取得するプロセスと、あるキーの期限切れを設定します。
		String key = "key";
        BoundValueOperations stringTemplate = redisTemplate.boundValueOps(key);
        //  key
        stringTemplate.set("test");
        //  value
        String value = (String) stringTemplate.get();
        //       1 
        stringTemplate.set("testTimeout",1, TimeUnit.DAYS);
        //      ,    
        Long expire = stringTemplate.getExpire();
        System.out.println(key+"      :"+expire);
他の方法は以下の通りです。
メソッド名
方法の説明
void set(V value)
key対応のvlaue値を設定します。
void set(V value、long offset)
value値をoffsetビットから置換します。
void set(V value、long timeout、TimeUnit unit)
valueのタイムアウト時間を設定します。timeoutは数字です。unitは単位です。例えば、日、時間などです。
Boolean setIfAbsent(V value)
keyが対応するvalueがあるかどうかを判断します。あるとfalseに戻ります。もしないなら、追加して、trueに戻ります。
V get()
keyに対応するvalueを返します。
Stering get(long start,long end)
startからendまで、valueの値を切り取ります。
V get AndSet(V value)
valueの値を置換し、valueの古い値を返します。
Long increment(long delta)
もしvalueが数字タイプの文字列であれば、デルタを追加し、新しいvalueを返します。
Double increment(double delta)
もしvalueが数字タイプの文字列であれば、デルタを追加し、新しいvalueを返します。
Integer apped(String value)
value値の後に追加し、新しいvalueの長さを返します。
Long size()
valueの長さを返します。
Boolean expire(long var 1、TimeUnit var 3)
keyのキャッシュ時間を設定します。var 1は数字で、unitは単位です。例えば、日、時間など、戻りは設定に成功していますか?
Boolean expireAt(Date var 1)
keyの具体的な締切時間を設定し、設定に成功したかどうかを返す。
Long get Expire()
keyの残りのキャッシュ時間を返します。単位:秒
K getKey()
keyの名前を返します
DataType getType()
keyのタイプを取得
Boolean persist()
keyのキャッシュ時間を削除します。
void rename(K var 1)
keyの名前を変更します