3.2. 驱除配置
与之前的版本相比,数据网格 8 简化了驱除配置。但是,驱除配置在不同的 Data Grid 版本中进行了大量更改,这意味着迁移可能并不简单。
从 Data Grid 7.2 开始,memory
元素替换了配置中的 eviction
元素。本节只使用 memory
元素进行驱除配置。有关迁移使用 eviction
元素的配置的详情,请参考 Data Grid 7.2 文档。
3.2.1. 存储类型 复制链接链接已复制到粘贴板!
借助 Data Grid,您可以使用以下选项控制如何在内存中存储条目:
- 将对象存储在 JVM 堆内存中。
- 将字节存储在原生内存中(off-heap)。
- 将字节存储在 JVM 堆内存中。
Data Grid 8 的变化
在之前的 7.x 版本和 8.0 中,您可以使用 对象
、二进制
和 off-heap
元素来配置存储类型。
从 Data Grid 8.1 开始,您可以使用 storage
属性将对象存储在 JVM 堆内存中,或者以字节形式存储在非堆内存中。
要将字节存储在 JVM 堆内存中,您可以使用 encoding
元素为您的数据指定二进制存储格式。
Data Grid 7.x | Data Grid 8 |
---|---|
|
|
|
|
|
|
Data Grid 8 中的对象存储
默认情况下,Data Grid 8.1 使用对象存储(JVM 堆):
<distributed-cache> <memory /> </distributed-cache>
<distributed-cache>
<memory />
</distributed-cache>
您还可以明确配置 storage="HEAP"
以将数据作为对象存储在 JVM 堆内存中:
<distributed-cache> <memory storage="HEAP" /> </distributed-cache>
<distributed-cache>
<memory storage="HEAP" />
</distributed-cache>
Data Grid 8 中的现成存储
将 "OFF_HEAP"
设置为 storage
属性的值,以便以字节数形式存储数据:
<distributed-cache> <memory storage="OFF_HEAP" /> </distributed-cache>
<distributed-cache>
<memory storage="OFF_HEAP" />
</distributed-cache>
off-heap 地址计数
在以前的版本中,offheap
的 address-count
属性允许您指定哈希映射中提供的指针数,以避免冲突。使用 Data Grid 8.1 时,不再使用 address-count
,off-heap 内存会被动态重新使用,以避免冲突。
Data Grid 8 中的二进制存储
使用 encoding
元素为缓存条目指定二进制存储格式:
<distributed-cache> <!--Configure MediaType for entries with binary formats.--> <encoding media-type="application/x-protostream"/> <memory ... /> </distributed-cache>
<distributed-cache>
<!--Configure MediaType for entries with binary formats.-->
<encoding media-type="application/x-protostream"/>
<memory ... />
</distributed-cache>
因此,Data Grid 不再存储原语和字符串与 byte[]
混合,但仅存储 byte[]
。
3.2.2. 驱除阈值 复制链接链接已复制到粘贴板!
驱除可让 Data Grid 在容器大于配置的阈值时删除条目来控制数据容器的大小。
在 Data Grid 7.x 和 8.0 中,您可以指定两种驱除类型,用于定义缓存中条目的最大限制:
-
COUNT
测量缓存中的条目数。 -
MEMORY
测量缓存中所有条目所占用的内存量。
根据您设置的配置,当计数或内存总量超过最大值时,Data Grid 会移除未使用的条目。
Data Grid 7.x 和 8.0 也使用 size
属性来定义数据容器的大小。根据您配置的存储类型,当条目或内存量超过 size
属性的值时,驱除发生。
使用 Data Grid 8.1 时,size
属性已弃用,以及 COUNT
和 MEMORY
。反之,您可以使用以下两种方式之一配置数据容器的最大大小:
-
带有
max-count
属性的条目总数。 -
使用
max-size
属性的最大内存量(以字节为单位)。
根据条目总数进行驱除
<distributed-cache> <memory max-count="..." /> </distributed-cache>
<distributed-cache>
<memory max-count="..." />
</distributed-cache>
根据最大内存量进行驱除
<distributed-cache> <memory max-size="..." /> </distributed-cache>
<distributed-cache>
<memory max-size="..." />
</distributed-cache>
3.2.3. 驱除策略 复制链接链接已复制到粘贴板!
驱除策略控制数据网格如何执行驱除。
Data Grid 7.x 和 8.0 允许您使用 strategy
属性设置以下驱除策略之一:
策略 | 描述 |
---|---|
| Data Grid 不会驱除条目。这是默认设置,除非您配置驱除。 |
| Data Grid 从内存中删除条目,以便缓存不会超过配置的大小。这是配置驱除时的默认设置。 |
|
Data Grid 不执行驱除。通过从 |
|
如果超过配置的大小,则网格不会将新条目写入缓存。Data Grid 会抛出一个 |
使用 Data Grid 8.1 时,您可以使用与之前版本相同的策略。但是,policy
属性被 when-full
属性替代。
<distributed-cache> <memory when-full="<eviction_strategy>" /> </distributed-cache>
<distributed-cache>
<memory when-full="<eviction_strategy>" />
</distributed-cache>
驱除算法
使用 Data Grid 7.2 时,配置驱除算法的功能已弃用,以及 Low Inter-Reference Recency Set (LIRS)。
从版本 7.2 开始,Data Grid 包括 Caffeine 缓存库,它实现了 Least Frequently Used (LFU)缓存替换算法的变化,称为 TinyLFU。对于非堆存储,Data Grid 使用 Least Recently Used (LRU)算法的自定义实现。
3.2.4. 驱除配置比较 复制链接链接已复制到粘贴板!
比较不同 Data Grid 版本之间的驱除配置。
对象存储并驱除条目数
7.2 到 8.0
<memory> <object size="1000000" eviction="COUNT" strategy="REMOVE"/> </memory>
<memory>
<object size="1000000" eviction="COUNT" strategy="REMOVE"/>
</memory>
8.1
<memory max-count="1MB" when-full="REMOVE"/>
<memory max-count="1MB" when-full="REMOVE"/>
对象存储并驱除内存量
7.2 到 8.0
<memory> <object size="1000000" eviction="MEMORY" strategy="MANUAL"/> </memory>
<memory>
<object size="1000000" eviction="MEMORY" strategy="MANUAL"/>
</memory>
8.1
<memory max-size="1MB" when-full="MANUAL"/>
<memory max-size="1MB" when-full="MANUAL"/>
二进制存储和驱除条目数
7.2 到 8.0
<memory> <binary size="500000000" eviction="MEMORY" strategy="EXCEPTION"/> </memory>
<memory>
<binary size="500000000" eviction="MEMORY" strategy="EXCEPTION"/>
</memory>
8.1
<cache> <encoding media-type="application/x-protostream"/> <memory max-size="500 MB" when-full="EXCEPTION"/> </cache>
<cache>
<encoding media-type="application/x-protostream"/>
<memory max-size="500 MB" when-full="EXCEPTION"/>
</cache>
二进制存储和驱除内存量
7.2 到 8.0
<memory> <binary size="500000000" eviction="COUNT" strategy="MANUAL"/> </memory>
<memory>
<binary size="500000000" eviction="COUNT" strategy="MANUAL"/>
</memory>
8.1
<memory max-count="500 MB" when-full="MANUAL"/>
<memory max-count="500 MB" when-full="MANUAL"/>
非堆存储并驱除条目数
7.2 到 8.0
<memory> <off-heap size="10000000" eviction="COUNT"/> </memory>
<memory>
<off-heap size="10000000" eviction="COUNT"/>
</memory>
8.1
<memory storage="OFF_HEAP" max-count="10MB"/>
<memory storage="OFF_HEAP" max-count="10MB"/>
非堆存储并驱除内存量
7.2 到 8.0
<memory> <off-heap size="1000000000" eviction="MEMORY"/> </memory>
<memory>
<off-heap size="1000000000" eviction="MEMORY"/>
</memory>
8.1
<memory storage="OFF_HEAP" max-size="1GB"/>
<memory storage="OFF_HEAP" max-size="1GB"/>