配置 JCache Spring 缓存提供程序
Spring 6.0.12 支持符合 JCache 的缓存提供程序。 您还可以使用 JCache 支持的缓存 NCache 如果您的 Spring 版本支持 JCache。 只需按照以下步骤修改配置即可无缝使用Spring NCache JCache Spring 缓存提供程序。
配置 JCache Spring Cache Provider 的先决条件
如报名参加 NCache 客户,最低要求 爪哇岛 版本是 17.0。要配置 Spring Cache Provider,请确保满足以下先决条件。
备注
- 使用 Maven的 包为 NCache Professional,改变
<artifactId>
如下图所示:<artifactId>ncache-professional-client</artifactId>
. - 地址
spring-context-support
配置依赖 Spring框架. - 地址
spring-boot-starter-cache
配置依赖 春季靴.
添加 Maven 包
用户需要在他们的 pom.xml
配置 JCache Spring Cache 提供程序时的文件。
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<!--for NCache Enterprise-->
<artifactId>ncache-client</artifactId>
<!--for NCache Professional-->
<!--artifactId>ncache-professional-client</artifactId-->
<version>x.x.x</version>
</dependency>
定义缓存配置
备注
要在 Spring 应用程序中启用缓存,请添加 @启用缓存 注解。
JCache 通过以下方式引导 javax.cache.spi.CachingProvider
,类路径上的 JSR-107 兼容缓存库。
如果您有多个 JCache 供应商在您的 的.classpath,然后是标签 spring.cache.jcache.provider
和 spring.cache.type
需要明确添加到 应用程序属性 文件中。
spring.cache.jcache.provider=com.alachisoft.ncache.jsr107.spi.NCacheCachingProvider
spring.cache.type=jcache
配置缓存
备注
要在应用程序启动时初始化多个缓存,请提供 spring.cache.cache-names
带有逗号分隔缓存名称的标记。
要为 JCache Spring 应用程序配置缓存,如果您想在应用程序启动时初始化缓存,则需要添加 spring.cache.cache-names
标签在 应用程序属性 文件。 缓存名称应与配置中的相同 NCache 管理中心。
spring.cache.cache-names=demoCache,booksCache
此外,您还可以编辑 JCacheManagerCustomizer
类来配置 JCache spring 缓存并在运行时使用这些配置对其进行初始化。
@Configuration
public class CacheConfiguration implements JCacheManagerCustomizer
{
@Override
public void customize(CacheManager cacheManager)
{
MutableConfiguration mutableConfiguration = new MutableConfiguration();
mutableConfiguration.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.ONE_MINUTE));
cacheManager.createCache("booksCache", mutableConfiguration);
}
}
启用缓存后,将缓存行为绑定到要使用的方法 NCache 作为 Spring 的缓存提供者。 为此,请参阅部分 使用缓存声明配置应用程序.