3.4. 持久性缓存存储
与 Data Grid 7.x 相比,在 Data Grid 8 中对缓存存储配置有一些更改。
Persistence SPI
Data Grid 8.1 引入了用于缓存存储的 NonBlockingStore
接口。NonBlockingStore
SPI 公开不得阻止调用线程的方法。
将 Data Grid 连接到持久数据源的缓存存储实现了 NonBlockingStore
接口。
对于使用阻塞操作的自定义缓存存储实现,Data Grid 提供了一个 BlockingManager
工具类来处理这些操作。
引入 NonBlockingStore
接口弃用了以下接口:
-
CacheLoader
-
CacheWriter
-
AdvancedCacheLoader
-
AdvancedCacheWriter
自定义缓存存储
Data Grid 8 可让您像之前的版本一样使用 store
元素配置自定义缓存存储。
有以下更改适用:
-
删除了
singleton
属性。改为使用shared=true
。 -
添加
segmented
属性,默认为true
。
分段缓存存储
从 Data Grid 8 开始,缓存存储配置默认为 segmented="true"
,并应用到以下缓存存储元素:
-
store
-
file-store
-
string-keyed-jdbc-store
-
jpa-store
-
remote-store
-
rocksdb-store
-
soft-index-file-store
单个文件缓存存储
Data Grid 8 中删除了单文件缓存存储的 relative-to
属性。如果您的缓存存储配置包含此属性,Data Grid 会忽略它,并仅使用 path
属性来配置存储位置。
JDBC 缓存存储
JDBC 缓存存储必须包含 xlmns
命名空间声明,在某些 Data Grid 7.x 版本中不需要该声明。
<persistence> <string-keyed-jdbc-store xmlns="urn:infinispan:config:store:jdbc:13.0" shared="true"> ... </persistence>
JDBC 连接工厂
Data Grid 7.x JDBC 缓存存储可使用以下 ConnectionFactory
实现来获取数据库连接:
-
ManagedConnectionFactory
-
SimpleConnectionFactory
-
PooledConnectionFactory
Data Grid 8 现在使用基于 Agroal (与 Red Hat JBoss EAP 相同)的连接工厂来连接数据库。不再可以使用 c3p0.properties
和 hikari.properties
文件。
自 Data Grid 8.3 JDBC 连接工厂起,是 org.infinispan.persistence.jdbc.common.configuration
软件包的一部分。
分段
基于 JDBC 字符串的缓存存储配置,其现在为分段(现在是默认设置),必须包含 segmentColumnName
和 segmentColumnType
参数,如下例所示:
MySQL 示例
builder.table() .tableNamePrefix("ISPN") .idColumnName("ID_COLUMN").idColumnType(“VARCHAR(255)”) .dataColumnName("DATA_COLUMN").dataColumnType(“VARBINARY(1000)”) .timestampColumnName("TIMESTAMP_COLUMN").timestampColumnType(“BIGINT”) .segmentColumnName("SEGMENT_COLUMN").segmentColumnType("INTEGER")
PostgreSQL 示例
builder.table() .tableNamePrefix("ISPN") .idColumnName("ID_COLUMN").idColumnType(“VARCHAR(255)”) .dataColumnName("DATA_COLUMN").dataColumnType(“BYTEA”) .timestampColumnName("TIMESTAMP_COLUMN").timestampColumnType("BIGINT”) .segmentColumnName("SEGMENT_COLUMN").segmentColumnType("INTEGER");
write-behind
Write-Behind 模式的 thread-pool-size
属性已在 Data Grid 8 中删除。
删除了缓存存储和加载程序
Data Grid 7.3 弃用了在 Data Grid 8 中不再提供以下缓存存储和加载程序:
- Cassandra Cache Store
- REST Cache Store
- LevelDB Cache Store
- CLI Cache Loader
缓存存储 migrator
缓存以与 Data Grid 8 不兼容的二进制格式存储数据。
使用 StoreMigrator
实用程序将持久缓存存储中的数据迁移到 Data Grid 8。
3.4.1. 基于文件的缓存默认存储到软索引
在缓存配置中包含 file-store
persistence 现在会创建一个基于软索引文件的缓存存储 SoftIndexFileStore
,而不是单个文件缓存存储 SingleFileStore
。在 Data Grid 8.2 及更早版本中,SingleFileStore
是基于文件的缓存存储的默认设置。
如果您要从以前的版本迁移或升级到 Data Grid 8.3,且您的缓存包含使用 soft-index-file-store
元素的任何配置,您应该转换该配置以使用 file-store
元素。
3.4.1.1. 声明性配置
Data Grid 8.2 及更早版本
<persistence> <soft-index-file-store xmlns="urn:infinispan:config:soft-index:12.1"> <index path="testCache/index" /> <data path="testCache/data" /> </soft-index-file-store> </persistence>
Data Grid 8.3 及更新的版本
<persistence> <file-store> <index path="testCache/index" /> <data path="testCache/data" /> </file-store> </persistence>
3.4.1.2. 编程配置
Data Grid 8.2 及更早版本
ConfigurationBuilder b = new ConfigurationBuilder(); b.persistence() .addStore(SoftIndexFileStoreConfigurationBuilder.class) .indexLocation("testCache/index"); .dataLocation("testCache/data")
Data Grid 8.3 及更新的版本
ConfigurationBuilder b = new ConfigurationBuilder(); b.persistence() .addSoftIndexFileStore() .indexLocation("testCache/index") .dataLocation("testCache/data");
3.4.1.3. 使用带有 Data Grid 8.3 的单个文件缓存存储
您可以使用 Data Grid 8.3 或更高版本配置 SingleFileStore
缓存存储,但红帽不推荐这样做。您应该使用 SoftIndexFileStore
缓存存储,因为它们提供更好的可扩展性。
声明
<persistence passivation="false"> <single-file-store shared="false" preload="true" fetch-state="true" read-only="false"/> </persistence>
programmatic
ConfigurationBuilder b = new ConfigurationBuilder(); b.persistence() .addSingleFileStore();