首先,您为什么(可能?)使用 GemFire 的 API 创建带有 ClientCache.createClientRegionFactory(:ClientRegionShortcut) 的客户端区域,尤其是在 Spring 上下文中?要在 Spring 上下文中正确管理 GemFire 对象的生命周期,您应该使用 Spring Data GemFire 和 SDG 的配置,XML 或(最好)@987654323 @。
第二,这个……
但是,当与 GemfireTemplate 结合使用相同的设置时,要将 Gemfire 用作存储库而不是缓存,似乎根本不会在本地缓存数据,并且始终获取从服务器返回。
是相当弱的论点。是什么让您认为它“似乎没有”在本地缓存数据?
您是否观察到 GemFire 日志中的输出来支持您的主张?你写过一个测试来证实你的假设吗?您在 Spring 上下文中配置 GemFire 并通过 GemfireTemplate 获取对区域的访问权的 Spring (?) 配置是什么?您使用的是什么版本的 Spring Data GemFire?等等等等。
您知道GemfireTemplate 是wrapper for a GemFire Region(它使用模板软件设计模式,与Spring 的 JdbcTemplate 的用途相同)吗?任何GemfireTemplate.put(key, value) 操作都有效calls Region.put(key, value)。
注意:使用各种 Spring 模板可以方便地使用附加功能修饰应用程序的数据访问操作,例如翻译特定于数据存储的异常(例如 GemFire,以及经常错误地“检查”异常)在 Spring 的中,所有重要的数据访问 Exception Hierarchy 以及使您的应用程序数据访问操作能够在事务范围内正确参与事务,而无需编写像 tx.begin() 和 @987654347 这样的样板代码@ 或 tx.rollback()。总之……
所以,即使你的代码看起来有点像这样......
RegionFactory exampleRegionFactory = clientCache.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY_HEAP_LRU);
// 1. call various setters to configure the Region created by the factory
exampleRegionFacatory.set...
...
// 2. create the Region
Region exampleRegion = exampleRegionFactory.create("Example");
// 3. interface to the Region via the GemfireTemplate
GemfireTemplate exampleTemplate = new GemfireTemplate(exampleRegion);
// 4. put key/value into /Example Region
examleTemplate.put("myKey", "myValue");
这有什么不同,而不是 4,只是这个......
exampleRegion.put("myKey", "myValue");
最后,考虑一下我写的test class...
这个测试类是based on Spring Data GemFire 2.0.1.RELEASE。
这个测试类includes a Gfsh shell script 启动一个小型 GemFire 集群(定位器和服务器)并在每次测试运行后检查服务器,如果您愿意的话。但是,为了在测试运行后检查服务器的状态,您需要注释掉 this line。但是,您必须记住在下次测试运行之前使用remove --region=/Example --key=testKey 擦除服务器上的数据,否则this assertion 显然会失败!
如您所见,我有客户Region data policy set with the ClientRegionShort.CACHING_PROXY_HEAP_LRU(如上所述)。您可以尝试不同的ClientRegionShortcuts 来查看它们的效果。除了ClientRegionShortcut.PROXY(当然,它不会在客户端区域“本地”存储任何数据,因此具有 NO 大小),此测试类将通过所有ClientRegionShortcuts。使用基于LOCAL 的ClientRegionShortcuts,您无需运行服务器。
最后,我创建了 2 个 Spring 配置文件,Spring Data GemFire Annotation-based configuration approach(配置文件“Spring Data GemFire”)以及 GemFire API-based approach(配置文件“GemFireApi”)。永远不要使用后者,尤其是在 Spring 上下文中。我写这个配置文件纯粹是为了证明如果您使用上面的客户端区域配置的 sn-p,它仍然可以工作!
您可以使用 Spring TestContext @ActiveProfiles 注释 like so 切换配置文件。
最后要注意的是,Region.size() 方法只考虑本地(客户端)区域的大小为explained in the Javadoc,其中Region.sizeOnServer() 将检查相应的(按名称)服务器区域的大小 (Javadoc) .
此测试按预期通过!
希望这会有所帮助!
-约翰