请用java编程,下面load方法只有参数key,还能再加别的参数吗 ? 代码如下: Caffeine.newBuilder() .maximumSize(maxSize) .expireAfterWrite(expireSeconds, TimeUnit.SECONDS) .build(new CacheLoader<Integer, Integer>() { @Nullable @Override public Integer load(@NonNull Integer key) throws Exception { logger.debug("load post rows from DB"); return discussPostMapper.selectDiscussPostRows(0,0,key); } });
时间: 2024-02-10 13:25:57 浏览: 31
是的,您可以在load方法中添加其他参数。只需在CacheLoader泛型参数中指定其他参数的类型,并在load方法中添加对应的参数即可。以下是示例代码:
```java
Caffeine.newBuilder()
.maximumSize(maxSize)
.expireAfterWrite(expireSeconds, TimeUnit.SECONDS)
.build(new CacheLoader<Integer, Integer, String>() {
@Nullable
@Override
public Integer load(@NonNull Integer key, String additionalParam) throws Exception {
logger.debug("load post rows from DB");
// 使用additionalParam进行操作
return discussPostMapper.selectDiscussPostRows(0, 0, key);
}
});
```
在上面的示例中,我们向CacheLoader泛型参数添加了一个String类型的additionalParam参数,并在load方法中使用它。您可以根据您的需求添加任意数量和类型的参数。
相关问题
caffeine.getifpresent方法
Caffeine.getIfPresent方法是Caffeine缓存库中的一个方法,用于检索缓存中是否存在指定的键。如果该键存在于缓存中,则返回与该键关联的值,否则返回null。该方法的语法如下:
```
V getIfPresent(Object key)
```
其中,V表示缓存中值的类型,key表示要检索的键。
注意,getIfPresent方法不会触发缓存的加载操作。如果要加载缓存中不存在的键,则需要使用Caffeine.get方法。另外,如果键不存在,则该方法不会将该键添加到缓存中。
com.github.ben-manes.caffeine.caffeine
Caffeine is a high-performance, near optimal caching library for Java 8. It provides an in-memory cache using a Google Guava-inspired API. Caffeine's key features include automatic loading of entries into the cache, automatic eviction based on usage patterns, and support for multiple eviction policies. It is widely used in production applications and has been shown to be significantly faster than other caching libraries.
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)