Partitioned and replicated caches can also be fronted by a "near cache" which is a smaller local cache that stores the most recently or most frequently accessed data in the on-heap memory. Just like with a partitioned cache, you can control the size of the near cache and its eviction policies.
Near caches can be created directly on client nodes by passing
NearCacheConfiguration to the
Ignite.getOrCreateNearCache(String, NearCacheConfiguration) methods. Use the
Ignite.getOrCreateCache(CacheConfiguration, NearCacheConfiguration) method if you need to both start a distributed cache dynamically and create a near cache for it.
// Create near-cache configuration for "myCache". NearCacheConfiguration<Integer, Integer> nearCfg = new NearCacheConfiguration<>(); // Use LRU eviction policy to automatically evict entries // from near-cache, whenever it reaches 100_000 in size. nearCfg.setNearEvictionPolicyFactory(new LruEvictionPolicyFactory<>(100_000)); // Create a distributed cache on server nodes and // a near cache on the local node, named "myCache". IgniteCache<Integer, Integer> cache = ignite.getOrCreateCache( new CacheConfiguration<Integer, Integer>("myCache"), nearCfg);
<bean class="org.apache.ignite.configuration.CacheConfiguration"> <property name="name" value="myCache" /> <property name="nearConfiguration"> <bean class="org.apache.ignite.configuration.NearCacheConfiguration"> <property name="nearEvictionPolicy"> <bean class="org.apache.ignite.cache.eviction.lru.LruEvictionPolicy"> <property name="maxSize" value="100000"/> </bean> </property> </bean> </property> </bean>
Usually, when using Ignite with affinity collocation, near caches should not be used. If computations are collocated with the corresponding partition cache nodes then the near cache is simply not needed because all the data is available locally in the partitioned cache. However, there are cases when it is impossible to send computations to remote nodes. For cases like these, near caches can significantly improve scalability and the overall performance of the application.
Near caches are fully transactional and get updated or invalidated automatically whenever the data changes on the servers.
Near Caches on Server Nodes
When accessing data from
PARTITIONED caches on the server side in a non-collocated fashion, you may need to configure near-caches on the server nodes via
Most configuration parameters available on
CacheConfiguration that make sense for the near cache are inherited from the server configuration. For example, if the server cache has an
ExpiryPolicy set, entries in the near cache will be expired based on the same policy.
Parameters listed in the table below are not inherited from the server configuration. They are provided separately, via the
Eviction policy for the near cache.
Start size for near cache.