The unfortunate thing about this relationship is that now the Cache interface also has to implement all of the other methods such as keySet, values and entrySet. Originally Infinispan didn't implement these collections or returned an immutable copy (requiring all elements to be in memory). Neither choice is obviously desirable.
This all changed with ISPN-4836 which provided backing implementations of keySet, values and entrySet collections. This means that all methods were now provided and would keep up to date with changes to the underlying Cache and updates to these collections would be persisted down to the Cache. The implementation also didn't keep a copy of all contents and instead allowed for memory efficient iteration. And if the user still wanted to use a copy they could still do that, by iterating over the collection and copying themselves. This later spring boarded our implementation of Distributed Stream as well.
The problem was that the RemoteCache was left in the old state, where some things weren't implemented and others were copies just like how embedded caches used to be.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RemoteCache remoteCache = ... | |
// This was a copy of all keys in memory on your client! | |
Set<K> keys = remoteCache.keySet(); | |
// Can't do this as it is an immutable copy | |
keys.remove(someKey); | |
// Didn't support any of these | |
remoteCache.entrySet(); | |
remoteCache.values(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RemoteCache remoteCache = ... | |
// This is a backing set, no keys in memory yet! | |
Set<K> keys = remoteCache.keySet(); | |
// We can remove keys now yay! | |
keys.remove(someKey); | |
// If you do want to make a copy | |
Set<K> keysCopy = new HashSet<>(remoteCache.keySet()); | |
// Or if you like streams and want do other processing | |
Set<K> someKeyCopy = remoteCache.keySet().stream().filter(...).collect(Collectors.toSet()); | |
// You can even do this with entrySet now too | |
Set<CacheEntry<K, V>> entrySet = remoteCache.entrySet(); | |
entrySet.stream().map(...).forEach(...); | |
// Or even values! | |
Collection<V> values = remoteCache.values(); |
You can read more about this and the remote iterator which drives these collections on our user guide.
We hope you find that this improves your usage of RemoteCaches in the future by allowing you to have backed collections that also allow you to use the improvements of Java 8 with Streams.
If you have yet you can acquire Infinispan 9.1.1 or the latest stable version at http://infinispan.org/download/
No comments:
Post a Comment