第 3 章 hot Rod Client API
Data Grid Hot Rod 客户端 API 提供了用于远程创建缓存、处理数据、监控集群缓存拓扑等的接口。
3.1. 基本 API 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
以下是客户端 API 如何使用 Java Hot Rod 客户端存储或检索信息的示例代码片段。它假定 Data Grid 服务器在 localhost:11222 上运行。
//API entry point, by default it connects to localhost:11222
CacheContainer cacheContainer = new RemoteCacheManager();
//obtain a handle to the remote default cache
Cache<String, String> cache = cacheContainer.getCache();
//now add something to the cache and make sure it is there
cache.put("car", "ferrari");
assert cache.get("car").equals("ferrari");
//remove the data
cache.remove("car");
assert !cache.containsKey("car") : "Value must have been removed!";
客户端 API 映射本地 API: RemoteCacheManager 对应于 DefaultCacheManager (实施 CacheContainer )。这种通用 API 有助于通过 Hot Rod 从本地调用进行简单迁移:所有需要做的是在 DefaultCacheManager 和 RemoteCacheManager 之间切换 - 这进一步简化了这两者继承的通用 CacheContainer 接口。