我是 Spring boot 缓存的新手,根据我的要求,我想将 JCS 与 spring boot(rest api)一起使用。我已经在谷歌上搜索过,但没有得到太多关于它的信息。
有关于 java 类的信息。有谁知道如何使用它或任何相关的教程或信息。
这可能是基于意见的问题。但这是非常需要的。
public static void main(String[] args) {
// Initialize the JCS object and get an instance of the default cache region
try {
JCS cache = JCS.getInstance("default");
String key = "key0";
String value = "value0";
cache.put(key, value);
cache.put("vasu","dev");
} catch (CacheException e) {
e.printStackTrace();
}
}
最佳答案
就 Spring Boot 的缓存而言,Spring 支持以下缓存提供程序,如以下链接中所述。
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-caching.html
引用一下,它说
If you have not defined a bean of type CacheManager or a CacheResolver named cacheResolver (see CachingConfigurer), Spring Boot tries to detect the following providers (in the indicated order):
- 通用
- JCache (JSR-107)(EhCache 3、Hazelcast、Infinispan 等)
- EhCache 2.x
- 淡褐色
- 无限跨度
- 沙发底座
- Redis
- 咖啡因
- 简单
我建议使用Ehcache,您可以在下面的链接中查看有关Ehcache与Spring框架的更多详细信息。 https://www.baeldung.com/spring-cache-tutorial
如果您只对JCS感兴趣,请引用下面的链接进行了解。
https://codyburleson.com/quick-and-simple-caching-with-apache-commons-jcs/
如果是 Spring boot,我建议创建类并将 JCS 包装在该类中,以便您可以连接任何类,以便可以抽象出 JCS 实现细节。我提供以下概要。
@Autowired CacheUpdater 缓存更新;
在方法中可以这样写,
public void someMethod(... params) { 缓存更新器.更新(键,值) }
不必有一个名为 update() 的方法,您可以创建任何方法,它应该在内部调用 JCS 将键和值放入缓存中。
关于java - 如何在 Spring boot 中使用 JCS(Rest api),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56404286/