春季 是一个轻量级、依赖注入和面向方面的 Java 开发容器和框架。 它降低了 J2EE 开发的整体复杂性,并提供了高内聚和松耦合。 由于 Spring 提供的好处,许多开发人员使用它来创建高流量的小型到企业级应用程序。
但是这些高流量的 Spring 应用程序面临一个主要的可扩展性问题。 尽管他们可以通过向应用服务器场添加更多服务器来进行扩展,但他们的数据库服务器无法以相同的方式进行扩展以处理不断增长的事务负载。 在这种情况下,Java 分布式缓存是您处理的最佳选择,因为它通过减少昂贵的数据库访问来卸载您的数据库,并提高您的应用程序性能。
NCache 更多信息 NCache 配套文档 NCache Spring缓存文档
为什么使用 Spring 应用程序 NCache?
NCache, 内存中的分布式缓存通过引入通用 Java 缓存机制实现了 Spring Caching 提供程序,您可以使用该机制轻松缓存 Spring 应用程序的 CPU 密集型、耗时和数据库绑定方法的输出。 这种方法不仅减少了数据库负载,还减少了方法执行的次数,提高了应用程序的性能。 在这篇博客中,我将进一步讨论如何配置 Spring Caching NCache.
NCache 更多信息 NCache 作为 Spring 数据缓存 使用配置应用程序 NCache
如何配置 Spring 缓存以使用 NCache?
NCache 通过提供 Spring 缓存支持 通用 Spring 缓存提供程序,其中 NCache 充当 Spring 应用程序的缓存管理器并通过 JCache 缓存提供程序,其中 NCache 使用 JCache 支持的特性。 只需按照下面给出的两种方式和配置步骤 NCache 与您的 Spring 应用程序。
通用 Spring 缓存提供程序
用于配置您的 Spring 应用程序 NCache Generic Spring Caching Provider,首先需要添加需要的 Maven 依赖 那个春天和 NCache 提供。 然后,通过以下方式在 Spring 应用程序中定义 bean 基于 Java 的 Bean 定义 or 基于 XML 的 Bean 定义.
要使用基于 Java 的定义来定义 bean,您需要添加 @Bean
注释在 CachingConfiguration
类 setConfigFile()
方法将指定您的路径 ncache-spring.xml
文件,如下图。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@Configuration @EnableCaching public class CachingConfiguration { @Bean public CacheManager cacheManager() { SpringConfigurationManager springConfigurationManager = new SpringConfigurationManager(); URL resource = getClass().getClassLoader().getResource("ncache-spring.xml"); springConfigurationManager.setConfigFile(resource.getPath()); NCacheCacheManager cacheManager = new NCacheCacheManager(); cacheManager.setSpringConfigurationManager(springConfigurationManager); return cacheManager; } } |
对于使用基于 XML 的定义来定义 bean,您需要添加一个启用缓存的 xml 文件并指定 NCache 作为缓存管理器。 必须为两者定义 Bean 标签 cacheManager
和 NCacheConfigurationManager
. 也, springConfigurationManager
和 logFilePath
必须为 cacheManager 指定属性。 有关这些属性的更多详细信息,请参阅我们的 Spring缓存文档.
NCache 缓存管理器使用 ncache-spring.xml
文件,其中缓存必须使用自己的一组属性进行配置和定义。 示例代码如下。
1 2 3 4 5 |
<application-config default-cache-name="default"> <caches> <cache name="CustomerCollectionCache" ncache-instance="demoCache" priority="normal" expiration-type="absolute" expiration-period="10"/> </caches> </application-config> |
JCache Spring 缓存提供程序
Spring 还支持 JCache 投诉缓存提供程序,您可以在 Spring 应用程序中使用 JCache NCache. 为 Spring Framework 或 Spring Boot 添加所需的 Maven 依赖项,然后 NCache 为整合。 然后为 JCache Spring 应用程序配置缓存。
您可以使用进一步解释的两种方式配置缓存。 一种是,您可以添加 spring.cache.cache-names
标签在 application.properties
文件。 缓存名称应与中配置的相同 NCache 网络管理员.
1 |
spring.cache.cache-names=demoCache,booksCache |
其他是,您可以通过 JCacheManagerCustomizer
在运行时使用所需配置初始化缓存的类。
1 2 3 4 5 6 7 8 9 10 11 |
@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 的缓存提供程序。
您可以通过两种方式将缓存行为绑定到它们的方法。 一个是通过 缓存注释 另一个是通过 基于声明式 XML 的缓存。 要详细了解,请访问 配置缓存声明 Spring Docs.
以下示例代码使用@Cacheable 注解在包含缓存 booksCache 的 Web 服务上执行缓存。
1 2 3 4 5 6 7 8 9 |
// BookService.java @Service public class BookService { @Cacheable("booksCache") public String getBookNameByIsbn(String isbn) { return findBookInSlowSource(isbn); } // further code } |
NCache 更多信息 JCache API 文档 休眠缓存
结论!
通过使用 NCache 作为 Spring Caching 提供者,您可以线性扩展 Spring 应用程序并提高性能。 只需简单地修改您的配置文件,您就可以开始了! 所以,下载 NCache 现在自己试试吧!