java Spring Boot,使用 EhCache 缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36934115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot, caching with EhCache
提问by user3528733
I need to cache some data in my application and I am thinking about use Ehcache. And I have several questions:
我需要在我的应用程序中缓存一些数据,我正在考虑使用 Ehcache。我有几个问题:
- Do I need another server for Ehcache?
- Do I need some another client to work with Ehcache?
- How Ehcache works with multiple instances? Is it even possible to create something like shared cache using Ehcache?
- Ehcache 需要另一台服务器吗?
- 我需要其他客户端来使用 Ehcache 吗?
- Ehcache 如何处理多个实例?甚至可以使用 Ehcache 创建类似共享缓存的东西吗?
回答by Ali Dehghani
Do I need another server for Ehcache?
Ehcache 需要另一台服务器吗?
You can use Ehcache in Standalone mode. In this topology, the cache data is held in the application node. So you won't need another server in this mode. Ehcache also provides two other modes:
您可以在独立模式下使用 Ehcache。在此拓扑中,缓存数据保存在应用程序节点中。所以在这种模式下你不需要另一台服务器。Ehcache 还提供了另外两种模式:
Distributed– The data is held in a remote server (or array of servers) with a subset of recently used data held in each application node. This topology offers a rich set of consistency options. A distributed topology is the recommended approach in a clustered or scaled-out application environment. It provides the highest level of performance, availability, and scalability.
The distributed topology is available as a Terracotta open source offeringwith no client limitations but limitations on Terracotta cluster size. These are removed when using the commercial BigMemory Max.
- Replicated– The cached data set is held in each application node and data is copied or invalidated across the nodes without locking. Replication can be either asynchronous or synchronous, where the writing thread blocks while propagation occurs. The only consistency mode supported in this topology is Weak Consistency.
分布式– 数据保存在远程服务器(或服务器阵列)中,每个应用程序节点中保存着最近使用的数据的子集。此拓扑提供了一组丰富的一致性选项。分布式拓扑是集群或横向扩展应用程序环境中的推荐方法。它提供最高级别的性能、可用性和可扩展性。
分布式拓扑可作为Terracotta 开源产品提供,没有客户端限制,但对 Terracotta 集群大小有限制。这些在使用商业BigMemory Max时被删除。
- 复制的——缓存的数据集保存在每个应用程序节点中,并且数据在没有锁定的情况下跨节点复制或失效。复制可以是异步的或同步的,在传播发生时写入线程会阻塞。此拓扑中唯一支持的一致性模式是弱一致性。
Do I need some another client to work with Ehcache?
我需要其他客户端来使用 Ehcache 吗?
You should use Ehcache library in order to be able to communicate with Ehache. But Spring provides a Caching Abstraction which is more elegant to work with and also has the advantage of being independent from the underlying caching implementation. So if you use Spring Caching Abstraction you could easily switch form Ehcache to, say, Hazelcast. You can read more about Spring Caching Abstraction in here.
您应该使用 Ehcache 库以便能够与 Ehache 通信。但是 Spring 提供了一个缓存抽象,它使用起来更优雅,并且还具有独立于底层缓存实现的优势。因此,如果您使用 Spring Caching Abstraction,您可以轻松地将形式 Ehcache 切换为 Hazelcast。您可以在此处阅读有关 Spring 缓存抽象的更多信息。
Spring Bootprovides spring-boot-starter-cache
starter package which auto-configures a suitable CacheManager
according to the implementation as long as the caching support is enabled.
Spring Boot提供了spring-boot-starter-cache
启动包,CacheManager
只要启用了缓存支持,它就会根据实现自动配置合适的启动包。
How Ehcache works with multiple instances? Is it even possible to create something like shared cache using Ehcache?
Ehcache 如何处理多个实例?甚至可以使用 Ehcache 创建类似共享缓存的东西吗?
Quoting from Ehcache documentation:
引用Ehcache 文档:
Ehcache provides in-process cache, which you can replicate across multiple nodes. It is also at the core of BigMemory Go and BigMemory Max, Terracotta's commercial caching and in-memory data-storage products. The Terracotta Server Array provided with BigMemory Max enables mixed in-process/out-of-process configurations with terabyte-size caches. For information about Terracotta's BigMemory offerings, see the BigMemory Go and BigMemory Max product documentation at http://terracotta.org/documentation.
Ehcache 提供进程内缓存,您可以跨多个节点复制。它也是 BigMemory Go 和 BigMemory Max(Terracotta 的商业缓存和内存数据存储产品)的核心。BigMemory Max 随附的 Terracotta 服务器阵列支持混合进程内/进程外配置以及 TB 级缓存。有关 Terracotta 的 BigMemory 产品的信息,请参阅位于http://terracotta.org/documentation的 BigMemory Go 和 BigMemory Max 产品文档。
As stated above, there is a free clustering option available with Ehcache. For this requirement, Redis and Hazelcast are also good options.
如上所述,Ehcache 提供了一个免费的集群选项。对于这个需求,Redis 和 Hazelcast 也是不错的选择。
回答by Benjamin M
The Documentation and examples should answer all your questions:
文档和示例应该可以回答您的所有问题:
https://spring.io/blog/2015/06/15/cache-auto-configuration-in-spring-boot-1-3https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-cache
https://spring.io/blog/2015/06/15/cache-auto-configuration-in-spring-boot-1-3 https://github.com/spring-projects/spring-boot/tree/master /spring-boot-samples/spring-boot-sample-cache
You can of course simply use embedded EhCache within your Spring Boot application. If you want to share the cache, it depends on your architecture. You could expose REST endpoints to make you cache available to other applications.
您当然可以在 Spring Boot 应用程序中简单地使用嵌入式 EhCache。如果要共享缓存,则取决于您的架构。您可以公开 REST 端点以使您的缓存可供其他应用程序使用。
If you want a distributed, scaling, high performance cache, you maybe should habe a look at Hazelcast.
如果你想要一个分布式的、可伸缩的、高性能的缓存,你也许应该看看 Hazelcast。