jetCache 基本使用


1.pom引用

 
        
<!--jetcache緩存 lettuce-->
<dependency>
    <groupId>com.alicp.jetcache</groupId>
    <artifactId>jetcache-starter-redis-lettuce</artifactId>
    <version>2.5.14</version>
</dependency>

這里引用的集成是lettuce的redis客戶端

 




2.
@SpringBootApplication
@EnableMethodCache(basePackages="com.example.demo") //開啟 Cache注解
@EnableCreateCacheAnnotation   //啟用createCache注解
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

  


3.配置文件配置 yml格式
#jetcache 集成使用
jetcache:
  statIntervalMinutes: 15   // 每隔多久統計信息的時長配置
  areaInCacheName: false   //是否配置前綴
  local:
    default:
      type: caffeine      //本地緩存類型
      keyConvertor: fastjson   //key的序列化轉化的協議
      limit: 10000              //本地緩存最大個數
      defaultExpireInMillis: 10000   //緩存的時間全局 默認值
  remote:
    default:
      type: redis.lettuce          //緩存數據庫類型
      keyConvertor: fastjson
      uri: redis://127.0.0.28:7224/  //這里支持集群配置
          #redis://127.0.0.28:7224/

          #redis://127.0.0.28:7224/

defaultExpireInMillis: 20000 //全局緩存失效時間
 #keyPrefix: ec

 



經過以上倆步已經可以使用jetCache注解 下面是基本的實戰
package com.example.demo;

import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CreateCache;
import io.lettuce.core.RedisClient;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;


public class redisTest extends BaseTest {


    @Autowired //lettuce 客戶端專用
    private RedisClient defaultClient;
/*
     @Autowired
    private Pool defaultPool;*/

    @CreateCache(name = "test", expire = 100, timeUnit = TimeUnit.SECONDS, cacheType = CacheType.BOTH)
    private Cache<String, String> cache;
     //可以根據自己的數據類型自定義value的數據類型
    @CreateCache(name = "test", expire = 100, timeUnit = TimeUnit.MINUTES, cacheType = CacheType.BOTH)
    private Cache<String, List<String>> listCache;

    @Test
    public void test() throws IOException, InterruptedException {
        cache.put("liuxw:1", "liuxw");
        System.out.println("liuxw:1  " + cache.get("liuxw:1"));

        cache.computeIfAbsent("liuxw:2", res -> {

            return "liuxw2";
        });

        System.out.println(cache.get("liuxw:1  " + "liuxw2"));

        cache.computeIfAbsent("liuxw:3", res -> {

            return "liuxw2";
        }, true, 100, TimeUnit.MINUTES);

        System.out.println("liuxw:3  " + cache.get("liuxw:1"));

        Set<String> set = new HashSet<>(Arrays.asList("liuxw:1", "liuxw2"));
        Map<String, String> map = cache.getAll(set);
        cache.removeAll(set);

        //推薦使用這個 分布式鎖
       boolean hasRun = cache.tryLockAndRun("liuxw3", 100, TimeUnit.SECONDS, () -> {
            System.out.println("我獲取到鎖了");
        });



            new Thread(() -> {
                //推薦使用這個
                boolean hasRun1 = cache.tryLockAndRun("liuxw3", 100, TimeUnit.SECONDS, () -> {
                    System.out.println("我獲取到鎖了"+Thread.currentThread().getName()+System.currentTimeMillis());
                    try {
                        Thread.sleep(0);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                });
                if (hasRun1){

                }else{
                    System.out.println("我沒獲取到鎖了"+Thread.currentThread().getName());
                }
            }, "a1"+new Random().toString()).start();
        Thread.sleep(10);

        new Thread(() -> {
            //推薦使用這個
            boolean hasRun1 = cache.tryLockAndRun("liuxw3", 100, TimeUnit.SECONDS, () -> {
                System.out.println("我獲取到鎖了"+Thread.currentThread().getName()+System.currentTimeMillis());
                try {
                    Thread.sleep(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            if (hasRun1){

            }else{
                System.out.println("我沒獲取到鎖了"+Thread.currentThread().getName());
            }
        }, "a2"+new Random().toString()).start();

        Thread.sleep(0);

        new Thread(() -> {
            //推薦使用這個 todo 分布式鎖實現邏輯學習一下
            boolean hasRun1 = cache.tryLockAndRun("liuxw3", 100, TimeUnit.SECONDS, () -> {
                System.out.println("我獲取到鎖了"+Thread.currentThread().getName()+System.currentTimeMillis());
                try {
                    Thread.sleep(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            if (hasRun1){

            }else{
                System.out.println("我沒獲取到鎖了"+Thread.currentThread().getName());
            }
        }, "a3"+new Random().toString()).start();

      System.in.read();
    }

}

 





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM