尽管在配置中设置了过期时间,但看起来数据不会从Ignite缓存中逐出。我注意到当我使用SQL 将数据插入缓存表时,会出现这个问题。
emplCache.query(new SqlFieldsQuery(
"CREATE TABLE Employee (id LONG PRIMARY KEY, firstName VARCHAR, lastName VARCHAR,
salary DECIMAL, gender VARCHAR)")).getAll();
SqlFieldsQuery insertqry = new SqlFieldsQuery("INSERT INTO Employee (id, firstName,
lastName, salary, gender) values (?, ?, ?, ?, ?)");
emplCache.query(insertqry.setArgs(Long.toString(count), words[1], words[2],
words[3], words[4])).getAll();
这就是我配置到期的方式:
CacheConfiguration<?, ?> cfg = new CacheConfiguration<>(cacheName);
cfg.setCacheMode(CacheMode.PARTITIONED);
cfg.setName(cacheName);
cfg.setSqlSchema("PUBLIC");
cfg.setAtomicityMode(CacheAtomicityMode.ATOMIC);
cfg.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(SECONDS, 1)));
但是,如果我使用dataStreamer 插入数据,我没有看到这个问题。
IgniteDataStreamer<Integer, Employee> stmr = ignite.dataStreamer(cfg.getName())
stmr.addData(id, emp);
我是否错过了一些配置设置?
[PS]在Evgenii的回答之后尝试了以下内容:
// This is a util method that returns a cache config for the given cache name and the expiry time in seconds
CacheConfiguration<?, ?> cfg = CacheConfig.getCacheConfig("emplCache", 1);
IgniteCache<?, ?> emplCache = ignite.getOrCreateCache(cfg);
emplCache.query(new SqlFieldsQuery(
"CREATE TABLE Employee (id LONG PRIMARY KEY, firstName VARCHAR, lastName VARCHAR, salary DECIMAL, gender VARCHAR)"
+ "WITH \"template=emplCache\" ")).getAll();
现在我得到了一个例外,即缓存不存在。但我在创建缓存后尝试创建表:
Exception in thread "main" javax.cache.CacheException: class org.apache.ignite.internal.processors.query.IgniteSQLException: Cache doesn't exist: emplCache
at org.apache.ignite.internal.processors.cache.IgniteCacheProxy.query(IgniteCacheProxy.java:807)
at org.apache.ignite.internal.processors.cache.IgniteCacheProxy.query(IgniteCacheProxy.java:765)
at com.demo.ignite.svc.CsvStreamer.main(CsvStreamer.java:33)
Caused by: class org.apache.ignite.internal.processors.query.IgniteSQLException: Cache doesn't exist: emplCache
at org.apache.ignite.internal.processors.query.h2.ddl.DdlStatementsProcessor.convert(DdlStatementsProcessor.java:277)
at org.apache.ignite.internal.processors.query.h2.ddl.DdlStatementsProcessor.runDdlStatement(DdlStatementsProcessor.java:221)
at org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing.queryDistributedSqlFields(IgniteH2Indexing.java:1331)
at org.apache.ignite.internal.processors.query.GridQueryProcessor$5.applyx(GridQueryProcessor.java:1815)
at org.apache.ignite.internal.processors.query.GridQueryProcessor$5.applyx(GridQueryProcessor.java:1813)
at org.apache.ignite.internal.util.lang.IgniteOutClosureX.apply(IgniteOutClosureX.java:36)
at org.apache.ignite.internal.processors.query.GridQueryProcessor.executeQuery(GridQueryProcessor.java:2293)
at org.apache.ignite.internal.processors.query.GridQueryProcessor.querySqlFields(GridQueryProcessor.java:1820)
at org.apache.ignite.internal.processors.cache.IgniteCacheProxy.query(IgniteCacheProxy.java:795)
答案 0 :(得分:1)
表Employee将在另一个缓存中创建(将为其创建),这就是为什么不会使用配置的ExpiryPolicy。
To configure this new cache add "WITH \"template=CACHE_NAME\""
其中CACHE_NAME是配置中缓存(或模板)的名称。在你的情况下,它是cacheName;
例如,将您的创建脚本更改为:
emplCache.query(new SqlFieldsQuery(
"CREATE TABLE Employee (id LONG PRIMARY KEY, firstName VARCHAR, lastName VARCHAR, salary DECIMAL, gender VARCHAR) " +
"WITH \"template=employee\" ")).getAll();
以下是link to doc
如果从java配置缓存,则需要将名为emplCache的缓存添加到Ignite with:
ignite.addCacheConfiguration(cfg);