Reputation: 272
I have several Oracle Coherence clusters, and on each cluster I have the same set of caches with the same cache names. How can I access a single cache (say "Cache1") from each cluster within my application? For example, I may want to check the count of "Cache1" across all environments to display to the user.
The clusters are set up using Coherence Extend, and I have setup the client-side cache-config with separate cache-mappings and remote-cache-schemes for each cluster. However, if I set the cache-name element to "Cache1" for each cluster, it only retrieves data from the first cluster listed in the xml. If I set it to something else (e.g. "Cache1-Dev1"), I get a Tangosol.IO.Pof.PortableException with the message 'No scheme for cache: "Cache1-Dev1"'.
<cache-config xmlns="http://schemas.tangosol.com/cache">
<caching-scheme-mapping>
<cache-mapping>
<cache-name>Cache1-Dev1</cache-name>
<scheme-name>extend-direct-dev1</scheme-name>
</cache-mapping>
<cache-mapping>
<cache-name>Cache1-Dev2</cache-name>
<scheme-name>extend-direct-dev2</scheme-name>
</cache-mapping>
</cache-scheme-mapping>
<caching-schemes>
<remote-cache-scheme>
<scheme-name>extend-direct-dev1</scheme-name>
<service-name>ExtendTcpCacheService-dev1</service-name>
<initiator-config>
<tcp-initiator>
<remote-addresses>
<socket-address>
<address>dev1-address</address>
<port>9500</port>
</socket-address>
</remote-addresses>
</tcp-initiator>
<outgoing-message-handler>
<request-timeout>60s</request-timeout>
</outgoing-message-handler>
</initiator-config>
</remote-cache-scheme>
<remote-cache-scheme>
<scheme-name>extend-direct-dev2</scheme-name>
<service-name>ExtendTcpCacheService-dev2</service-name>
<initiator-config>
<tcp-initiator>
<remote-addresses>
<socket-address>
<address>dev2-address</address>
<port>9500</port>
</socket-address>
</remote-addresses>
</tcp-initiator>
<outgoing-message-handler>
<request-timeout>60s</request-timeout>
</outgoing-message-handler>
</initiator-config>
</remote-cache-scheme>
</cache-config>
Upvotes: 1
Views: 2694
Reputation: 272
Found the answer elsewhere.
First, get the proxy service instance, and cast it to a CacheService. You should then be able to get the cache from that service instance.
Java implementation:
Service service = CacheFactory.getService("ExtendTcpCacheService-dev1");
CacheService cacheService = (CacheService) service;
NamedCache cache = cacheService.ensureCache("Cache1");
The code is almost identical in C#:
var service = CacheFactory.GetService("ExtendTcpCacheService-dev1");
var cacheService = (ICacheService)service;
var cache = cacheService.EnsureCache("Cache1");
This also means you no longer need to list the caches in the cache-mapping section of your cache-config xml file, though you need at least one cache-mapping containing cache-name and scheme-name for coherence to run, even if it isn't used.
Upvotes: 3