你的位置:首页 > 软件开发 > Java > 分布式缓存之Ehcache与terracotta 概念篇

分布式缓存之Ehcache与terracotta 概念篇

发布时间:2017-11-28 18:00:22
前言本章讲述Ehcache的基本概念,在实际工作中先了解使用框架的原理对后期代码编写和问题排查有很大的帮助。Ehcache是一种开源的基于标准的缓存,用于提高性能和减轻数据库负荷,是当今使用最广泛的基于java的缓存。 1、基本术语缓存:维基词典将缓存定义为"存储将 ...

分布式缓存之Ehcache与terracotta 概念篇

前言

本章讲述Ehcache的基本概念,在实际工作中先了解使用框架的原理对后期代码编写和问题排查有很大的帮助。

Ehcache是一种开源的基于标准的缓存,用于提高性能和减轻数据库负荷,是当今使用最广泛的基于java的缓存。

 

1、基本术语

  • 缓存:维基词典将缓存定义为"存储将要被使用的东西,并且可以被快速检索"。缓存是一组临时数据,要么是其它地方的数据副本,或者是计算的结果。已经在缓存中的数据可以在时间和资源方面以最小的成本重复访问。
  • 缓存条目:缓存条目由一个键和它在缓存中的映射数据值组成。
  • 缓存命中:当从缓存请求数据元素时,对于给定的键存在元素,这被称为缓存命中。
  • 缓存未命中:当从缓存请求数据元素时,对于给定的键不存在元素,这被称为缓存未命中。
  • 记录系统:数据的权威性来源,存储层中的最底层。它通常是一种传统的数据库,可能是一个专门的文件系统或者其他可靠的长期存储。
  • 驱逐:从缓存中删除条目,以便为较新的条目腾出空间(通常当缓存耗尽了数据存储容量时)。
  • 过期:经过一段时间后,从缓存中删除条目,通常是为了避免缓存中的过时数据。
  • 热数据:最近被应用程序使用的数据很可能很快就会被再次访问。这样的数据被认为是热门的。缓存可能会尝试将最热门的数据保存在速度最快的区域,同时尝试选择最不热门的数据来进行驱逐。

 

2、相关概念

2.1 数据新鲜度与过期

(1)数据新鲜度

数据新鲜度是指数据的副本(例如,在缓存中)与数据的源版本(例如,在记录系统中)相比的最新情况。一份过期的副本被认为是不同步的(或者很可能是不同步的)。

(2)缓存条目过期

Ehcache可以帮助您减少使用过期数据的可能性。在您的应用中配置缓存过期的方式和时间,一旦过期,该条目将自动从缓存中删除。目前支持的3种过期方式:

  1. no expiry:永不过期
  2. time-to-live:在条目创建后,超过了设定的时间将作过期处理
  3. time-to-idle:如果条目的最后一次访问时间离当前的时间超过了设定的时间长度,它将会作过期处理

 

2.2 分层缓存的概念

(1)存储层介绍

Storage Tiers

Ehcache 3, as in previous versions, offers a tiering model to allow storing increasing amounts of data on slower tiers (which are generally more abundant).

The idea is that resources related to faster storage are more rare, but are located where the 'hottest' data is preferred to be. Thus less-hot (less frequently used) data is moved to the more abundant but slower tiers. Hotter data is moved onto the faster tiers.

译:Ehcache 3和以前的版本一样,提供了一种分层模型,允许在较慢的层上存储更多的数据。

速度更快的存储层一般资源更少,但位于"最热门"数据的位置。因此,不太常用的数据被转移到更丰富但速度较慢的层,而更常用的数据被转移到更快的层。

 

You can configure Ehcache to use various data storage areas. When a cache is configured to use more than one storage area, those areas are arranged and managed as tiers. They are organized in a hierarchy, with the lowest tier (farther) being called the authority tier and the others being part of the caching tier (nearer, also called near cache) . The caching tier can itself be composed of more than one storage area. The hottest data is kept in the caching tier, which is typically less abundant but faster than the authority tier. All the data is kept in the authority tier, which is slower but more abundant.

Data stores supported by Ehcache include:

  • On-Heap Store - Utilizes Java’s on-heap RAM memory to store cache entries. This tier utilizes the same heap memory as your Java application, all of which must be scanned by the JVM garbage collector. The more heap space your JVM utilizes, the more your application performance will be impacted by garbage collection pauses. This store is extremely fast, but is typically your most limited storage resource.

  • Off-Heap Store - Limited in size only by available RAM. Not subject to Java garbage collection (GC). Is quite fast, yet slower than the On-Heap Store because data must be moved to and from the JVM heap as it is stored and re-accessed.

  • Disk Store - Utilizes a disk (file system) to store cache entries. This type of storage resource is typically very abundant but much slower than the RAM-based stores. As for all application using disk storage, it is recommended to use a fast and dedicated disk to optimize the throughput.

  • Clustered Store - This data store is a cache on a remote server. The remote server may optionally have a failover server providing improved high availability. Since clustered storage comes with performance penalties due to such factors as network latency as well as for establishing client/server consistency, this tier, by nature, is slower than local off-heap storage.

分布式缓存之Ehcache与terracotta 概念篇

译:存储层中的最底层被命名为权威层(authority tier ,资源量最丰富的层),一般是磁盘存储层(Disk Tier)或集群存储层(Clustered Tier),其它更近访问更快的层有堆内存储层(Heap tier)和堆外存储层(Off Heap Tier)

Ehcache支持的数据存储包括:

  • 堆内存储 - 利用Java的堆内存来存储缓存条目。使用与Java应用程序相同的由JVM垃圾收集器管理的堆内存。JVM使用的堆空间越多,应用程序性能受到的垃圾收集暂停影响越大。此存储最快但空间最小。
  • 堆外存储 - 只有可用RAM限制大小。不受Java垃圾收集(GC)的影响。它的速度非常快,但比堆内存储要慢,因为数据的存储和重新访问的都要经过堆内存储层。 
  • 磁盘存储 - 利用磁盘(文件系统)来存储缓存条目。这种类型的存储资源通常非常丰富,但是比基于ram的存储要慢得多。对于使用磁盘存储的所有应用程序,建议使用一个快速且专用的磁盘来优化吞吐量。
  • 集群存储 - 这个数据存储是远程服务器上的一个缓存。远程服务器可以有选择地提供一个故障转移服务器使可用性更高。集群存储由于网络延迟和建立客户机/服务器一致性等因素而受到性能的惩罚,因此这一层的性能比本地的堆外存储要慢。

来自官方 id="multi-tier-sequence-flow">Sequence Flow for Cache Operations with Multiple TiersIn order to understand what happens for different cache operations when using multiple tiers, here are examples of Put and Get operations. The sequence diagrams are oversimplified but still show the main points.分布式缓存之Ehcache与terracotta 概念篇

 Figure 2. Multiple tiers using Put

分布式缓存之Ehcache与terracotta 概念篇

 Figure 3. Multiple tiers using Get

 

You should then notice the following:

  • When putting a value into the cache, it goes straight to the authoritative tier, which is the lowest tier.

  • A following get will push the value upwards in the caching tiers.

  • Of course, as soon as a value is put in the authoritative tier, all higher-level caching tiers are invalidated.

  • A full cache miss (the value isn’t on any tier) will always go all the way down to the authoritative tier.

译:为了理解在使用多个层时不同的缓存操作会发生什么情况,图为Put和Get操作的示例。序列图过于简化,但仍然显示了要点。

你应该注意到以下几点:

  • 当将一个值放入缓存时,它会直接转到权威层,这是最底层。
  • get将在缓存层中向上推送值。
  • 一旦将值放入到权威层中,所有更近的缓存层都将失效。
  • 缓存丢失(值不在任何层上)总是会一直向下获取直到权威层。

来自官方

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:分布式缓存之Ehcache与terracotta 概念篇

关键词:缓存

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。