spring boot 看看这个 Guava cache 与 caffeine 看来 Guava cache 不能再用了

今天天看这个发现 ecache 对多线程的处理还要自己加锁来处理,Guava cache 是有问题的,现在最新的caffeine是不要再加锁来处理。

https://softwaremill.com/race-condition-cache-guava-caffeine/

我空我写个测试来测一下上边的问题,现用caffeine做个对比,现在没空先记录一下。

http://www.manongjc.com/article/48915.html

这个是使用caffeine的好文章。

 

来个测试用例吧:

1. 

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
            <version>2.6.2</version>
        </dependency>
    </dependencies>>

2.

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.CaffeineSpec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
@EnableCaching
public class InitConfig {
    private static final Logger logger = LoggerFactory.getLogger(InitConfig.class);

    /**
     * caffeine CacheManager
     * @return
     */
    @Bean("caffeine")
    public CacheManager cacheManager() {
        String caffeineSpec = "initialCapacity=50,maximumSize=500,refreshAfterWrite=10s";
        CaffeineSpec spec = CaffeineSpec.parse(caffeineSpec);
        Caffeine caffeine = Caffeine.from(spec);
        CaffeineCacheManager cacheManager = new CaffeineCacheManager();
        cacheManager.setCaffeine(caffeine);
        cacheManager.setAllowNullValues(false);
        CacheLoader<Object, Object> cacheLoader = key -> null;
        cacheManager.setCacheLoader(cacheLoader);
        return cacheManager;
    }
}

3. 单独调用( 10s 后过期 ):

import org.springframework.cache.CacheManager;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo2ApplicationTests {

    @Resource
    @Qualifier("caffeine")
    private CacheManager cacheManager;

    @Test
    public void contextLoads() {

        while(true)
        {
            //System.out.println(cacheManager.getCacheNames());
            Cache cache = cacheManager.getCache("");

            if ( cache != null ) {
                String s = cache.get("123", String.class);
                System.out.println(s);
                if ( s == null ) {
                    cache.put("123", "456");
                    String s1 = cache.get("123", String.class);
                    System.out.println( "null....."+s1 );
                }

                System.out.println(s);
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值