服务器 > 服务器 > 缓存

Apache Ignite缓存基本操作实例详解

11人参与 2025-07-23 缓存

这段内容主要讲解了 apache ignite 中缓存(ignitecache)的基本操作,包括获取缓存、创建缓存、销毁缓存、执行原子操作以及异步操作等。下面我将用中文对这些内容进行详细解释,帮助你更好地理解。

一、获取缓存实例(getting an instance of a cache)

所有对缓存的操作都必须通过 ignitecache 实例来完成。你可以获取一个已经存在的缓存,或者动态创建一个新缓存。

示例代码:

ignite ignite = ignition.ignite();
// 获取名为 "mycache" 的缓存实例
ignitecache<integer, string> cache = ignite.cache("mycache");

⚠️ 注意:不同缓存的泛型类型可能不同,比如 ignitecache<integer, string>ignitecache<string, person> 是不同的类型。

二、动态创建缓存(creating caches dynamically)

你也可以在运行时动态创建一个缓存,使用 getorcreatecache() 方法。如果缓存已经存在,就直接返回;如果不存在,就根据配置创建。

示例代码:

cacheconfiguration<integer, string> cfg = new cacheconfiguration<>();
cfg.setname("mynewcache");
cfg.setatomicitymode(cacheatomicitymode.transactional);
ignitecache<integer, string> cache = ignite.getorcreatecache(cfg);

配置说明:

注意事项:

javax.cache.cacheexception: class org.apache.ignite.ignitecheckedexception: failed to start/stop cache, cluster state change is in progress.

三、销毁缓存(destroying caches)

使用 destroy() 方法可以从整个集群中删除一个缓存。

示例代码:

ignite ignite = ignition.ignite();
ignitecache<long, string> cache = ignite.cache("mycache");
cache.destroy();  // 删除名为 "mycache" 的缓存

⚠️ 注意:此操作是不可逆的,会删除所有节点上的缓存数据和配置。

四、基本原子操作(basic atomic operations)

获取到缓存后,可以进行常见的 putgetremove 等操作。

示例代码:

ignitecache<integer, string> cache = ignite.cache("mycache");
// 存入数据
for (int i = 0; i < 10; i++) {
    cache.put(i, integer.tostring(i));
}
// 获取数据
for (int i = 0; i < 10; i++) {
    system.out.println("got [key=" + i + ", val=" + cache.get(i) + ']');
}

批量操作注意事项:

五、条件更新操作(conditional updates)

ignite 提供了一些带有条件判断的更新方法,用于实现线程安全的更新逻辑。

示例代码:

// 如果 key 不存在,则插入,返回旧值
string oldval = cache.getandputifabsent(11, "hello");
// 如果 key 不存在,插入,返回是否成功
boolean success = cache.putifabsent(22, "world");
// 如果 key 存在,替换,返回旧值
oldval = cache.getandreplace(11, "new value");
// 如果 key 存在,替换,返回是否成功
success = cache.replace(22, "other new value");
// 如果值匹配,才替换
success = cache.replace(22, "other new value", "yet-another-new-value");
// 如果值匹配,才删除
success = cache.remove(11, "hello");

六、异步操作(asynchronous execution)

ignite 的很多缓存操作都有对应的 异步版本,方法名通常带有 async 后缀。

示例代码:

// 同步 get
v get(k key);
// 异步 get
ignitefuture<v> getasync(k key);

异步操作的处理方式:

示例监听器:

ignitecompute compute = ignite.compute();
ignitefuture<string> fut = compute.callasync(() -> "hello world");
fut.listen(f -> system.out.println("job result: " + f.get()));

七、线程池与闭包执行(closures execution and thread pools)

注意事项:

总结表格

操作类型描述
获取缓存通过 ignite.cache("name") 获取缓存实例
创建缓存使用 getorcreatecache(cfg) 动态创建缓存
销毁缓存调用 cache.destroy() 删除缓存
原子操作put, get, remove 等基本操作
条件更新putifabsent, replace, remove 等带条件操作
异步操作使用 xxxasync() 方法和 ignitefuture
线程池监听器可能由系统或公共线程池执行,避免同步调用

如果你是刚开始学习 ignite,理解这些基本缓存操作非常重要。它们是构建分布式缓存应用的基础。对于更复杂的场景,如事务、查询、索引等,请参考 ignite 的高级功能文档。

到此这篇关于apache ignite缓存基本操作的文章就介绍到这了,更多相关apache ignite缓存内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

您想发表意见!!点此发布评论

推荐阅读

Nginx反向代理与缓存配置方式

07-24

Nginx正向代理与反向代理详解

07-24

散热绝佳,+无光污染! 先马平头哥M2 Mesh机箱评测

06-19

独立缓存+TLC颗粒! 金百达KP270 1TB SSD固态硬盘评测

06-19

Nginx的核心功能--正向代理、反向代理、缓存和Rewrite

05-03

关于Streamlit性能优化:缓存与状态管理实战

04-23

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论