Summary: A client application may run a local side cache (near cache). There are two variations provided: local cache and local view. These allows the client application to cache specific or recently used data at the client memory address and have it updated automatically by the space when that data changes.
Client Side Cache - Near CacheGigaSpaces supports client cache running within the application memory address. There are two variations provided: Local ViewThe local view maintains a subset of the master space's data, allowing the client to read distributed data without performing any remote calls or serializing the data. Data is streamed into the client local view based on a certain criteria(s) specified when the local view is created. The criteria is defined via a regular template or SQLQuery, as opposed to the local cache that caches data on the client side in a lazy manner (only after the data has been read for the first time by the application). The local view is configured using the LocalViewSpaceFactoryBean class. The factory exposes the same IJSpace interface that a regular space component exposes. Nevertheless, it only supports read operations and will never access the remote space when such an operation is called. If the application performs data updates, it should have a second regular proxy and use it for such activities. The following is an example of how a local view can be configured:
Spring Namespace Configuration
<os-core:space id="space" url="jini://*/*/space" /> <os-core:local-view id="localViewSpace" space="space"> <os-core:view-query where="processed = true" class="com.example.Message"/> </os-core:local-view> <os-core:giga-space id="gigaSpace" space="localViewSpace"/> Plain Spring XML <bean id="space" class="org.openspaces.core.space.UrlSpaceFactoryBean"> <property name="url" value="jini://*/*/space" /> </bean> <bean id="viewSpace" class="org.openspaces.core.space.cache.LocalViewSpaceFactoryBean"> <property name="space" ref="space" /> <property name="localViews"> <list> <bean class="com.j_spaces.core.client.view.View"> <constructor-arg index="0" value="com.example.Message" /> <constructor-arg index="1" value="processed = true" /> </bean> </list> </property> </bean> Code IJSpace space = // get Space either by injection or code creation IJSpace localViewProxy = new LocalViewSpaceConfigurer(space). addView(new View("com.example.Message", "processed = true")).localView(); GigaSpace gigaspace = new GigaSpaceConfigurer(localViewProxy).gigaSpace(); Streaming a Subset of the Master Space Data into the Client's Process MemoryOnce the local view is constructed, data is pre-loaded into the client JVM's memory based on the view criteria (as opposed to the local cache that does not perform a pre-load stage). The local view is continuously updated by the master space in an asynchronous manner, using the notifications mechanism. Both write, take and update operations made on the master space are delivered into the client's process based on the view criteria. Local View Considerations
Local CacheThe local cache also maintains a subset of the master space's data, but unlike the local view that subset is based on the data that the client application accessed most recently, rather than a predefined subset of data. The following is an example of how a local cache can be configured. Note that the same properties can also be applied to a Local View:
Spring Namespace Configuration
<os-core:space id="space" url="jini://*/*/mySpace" /> <os-core:local-cache id="localCacheSpace" space="space" update-mode="PULL" > <os-core:properties> <props> <prop key="space-config.engine.cache_size">5000000</prop> <prop key="space-config.engine.memory_usage.enabled">true</prop> <prop key="space-config.engine.memory_usage.high_watermark_percentage">75</prop> <prop key="space-config.engine.memory_usage.write_only_block_percentage">73</prop> <prop key="space-config.engine.memory_usage.write_only_check_percentage">71</prop> <prop key="space-config.engine.memory_usage.low_watermark_percentage">45</prop> <prop key="space-config.engine.memory_usage.eviction_batch_size">1000</prop> <prop key="space-config.engine.memory_usage.retry_yield_time">100</prop> <prop key="space-config.engine.memory_usage.retry_count">20</prop> </props> </os-core:properties> </os-core:local-cache> <os-core:giga-space id="gigaSpace" space="localCacheSpace"/> Plain Spring XML <bean id="space" class="org.openspaces.core.space.UrlSpaceFactoryBean"> <property name="url" value="jini://*/*/space" /> </bean> <bean id="localCacheSpace" class="org.openspaces.core.space.cache.LocalCacheSpaceFactoryBean"> <property name="space" ref="space" /> <property name="updateMode" value="PULL" /> </bean> Code UrlSpaceConfigurer urlSpaceConfigurer = new UrlSpaceConfigurer("jini://*/*/space"); IJSpace localSpaceProxy = new LocalCacheSpaceConfigurer(urlSpaceConfigurer.space()).updateMode(UpdateMode.PULL).localCache(); GigaSpace gigaspace = new GigaSpaceConfigurer(localSpaceProxy).gigaSpace(); Local Cache PerformanceBelow simple benchmark comparing Ehcache get() operation with the local cache using the GigaSpace.readById() operation. With this benchmark the local cache/ehcache size can accommodate the entire data set the application is accessing.
Local Cache Considerations
Local Cache Memory EvictionIn order to properly configure the local cache eviction mechanism, you should consider tuning the following configuration elements: space-config.engine.cache_size space-config.engine.memory_usage.high_watermark_percentage space-config.engine.memory_usage.write_only_block_percentage space-config.engine.memory_usage.write_only_check_percentage space-config.engine.memory_usage.low_watermark_percentage #when eviction kicks in, how many objects will be evicted on every eviction cycle space-config.engine.memory_usage.eviction_batch_size space-config.engine.memory_usage.retry_count space-config.engine.memory_usage.explicit-gc space-config.engine.memory_usage.retry_yield_time See the Memory Management Facilities for additional details on these configuration properties.
Local Cache Memory HandlingThere might be cases when the local cache would not be able to evict its data fast enough. This will result in an exception thrown on the client side. The reasons for this behavior might be very large objects stored within the local cache, large amount of concurrent access to the local cache or relatively small JVM heap. In such a case a RemoteException will be thrown. GigaSpace gigaspace; while(true) { try { Object obj = gigaspace.read(template); break; } catch (Exception re) { if (re.getCause() instanceof MemoryShortageException) Thread.sleep(1000); } } Local Cache Update PoliciesThe local cache update policy defines how changes with the master Space objects are propagated to the local cache.
There are two local cache update policies you may choose from: Push and Pull. The Push PolicyWhen using the Push update policy, the local cache get a notification about the change, it receives the recent copy of the object from the master space and 'pushes' the update to the local cache immediately.
The Pull PolicyWhen using the Pull policy, the local cache get a notification about the change, and removes the stale object from the local cache (invalidate). The next time the client tries to read the object, it will be reloaded from the master space and stored in the local cache.
Local Cache Read FlowA read operation performed on local cache in the following order: A readMultiple operation performed on local cache in the following order:
Local Cache UpdateCached object updates are executed both on the master space and local cache.
Local Cache TakeTake is always executed on both the local space and the master space.
Object Reference Storage ModeStarting with XAP 7.0, the Space API local cache and local view have been modified to return a reference to the space object rather than returning a shallow copy (An object with a copy of the primitive fields and reference to the non-primitive fields) as performed with older versions. An identical object reference will be returned when conducting sub-subsequent read operation for the same object. When using a local cache/view, a new object reference will be returned when:
This improvement means the read operations using the GigaSpace.readByID , GigaSpace.read, GigaSpace.readMultiple (using a limited max objects where all the objects cached), GSIterator - having a local cache/view running at the client side, will be very fast and would not involve any new object generation. This behavior lowers the JVM garbage collection impact on the application. In addition, all read based operations have been improved to be non-lock calls. This lowers the contention on the system when having large amount of concurrent threads reading data from the local cache/view (ideal for a web applications). To control object storage type use the following property use the following:
Local View/Cache with Remote Master over the WANA popular usage of the local cache or the local view is with remote geographical sites where most of the activities are read only. In this case each site may use an application server or a web application running a local view/cache caching a subset of the data or the entire data set. With this approach there is one Master cluster storing the entire data in-memory, loading its data from the database (on demand or pre-fetch). The master site data is highly available (running primary-backup data-grid topology) where new remote sites can be started on the fly (Hub & Spoke model). Any read operations conducted at each site by the local users will be served first from the local cache. If the data had not been loaded into the local cache, it is read from the master cache (over the WAN) and loaded into the local cache. The next read operation for the same data item will not require any remote call to the master cache cluster. Write/update/delete operations will be conducted against the remote master cache in a synchronous manner. This means they will suffer from the latency between the local site and the remote master cache since they are fully acknowledged when the master cache will be fully committing these.
Local Cache / Local View DisconnectionWhen a connection between client local cache/view and remote master space is disrupted, the local cache/view identifies this disconnection and tries to reconnect with the remote space. The Local cache/view tries to reconnect several times before it fails. There are 2 types disconnections:
Properties to control the reconnection (can be set on the local cache/view bean configuration or via API call):
Here is an example how to set these parameters with a local view: <os-core:space id="space" url="jini://*/*/space" /> <os-core:local-view id="localViewSpace" space="space"> <os-core:view-query where="processed = true" class="com.example.Message"/> <os-core:properties> <props> <prop key="space-config.dist-cache.retry-connections">10</prop> <prop key="space-config.dist-cache.delay-between-retries">10000</prop> </props> </os-core:properties> </os-core:local-view> <os-core:giga-space id="gigaSpace" space="localViewSpace"/> Local View/Cache JMX MonitorThe Local View/Cache JMX Monitor monitors the number of objects stored within the client side cache and the activities performed with it. You can use JConsole to graph the number of objects within the local view/cache and other exposed statistics while the application is running. The Local View/Cache JMX Monitor exposing the following statistics:
Using the Local View/Cache JMX MonitorTo use the Local View/Cache JMX Monitor: <os-core:space id="space" url="jini://*/*/mySpace" /> <os-core:local-cache id="localCacheSpace" space="space"> <os-core:properties> <props> <prop key="space-config.filters.Statistics.enabled">true</prop> </props> </os-core:properties> </os-core:local-cache> <os-core:giga-space id="gigaSpace1" space="localCacheSpace"/> <os-core:space id="space2" url="jini://*/*/mySpace2" /> <os-core:local-cache id="localCacheSpace2" space="space2"> <os-core:properties> <props> <prop key="space-config.filters.Statistics.enabled">true</prop> </props> </os-core:properties> </os-core:local-cache> <os-core:giga-space id="gigaSpace2" space="localCacheSpace2"/> <bean id="gsClientCacheMonitor" class="com.gigaspaces.clientcachemonitor.GigaSpacesClientCacheJMXMonitor"> <property name="gigaSpaceList" > <list> <ref bean="gigaSpace1"/> <ref bean="gigaSpace2"/> </list> </property> </bean> You can specify up to 5 local View/Cache GigaSpace beans as part of the gigaSpaceList. 4. Start JConsole for your application JVM, move into the MBean Tab and select the GigaSpacesClientCacheJMXMonitor under the com.gigaspaces.clientcachemonitor.
Local Cache/View Configuration PropertiesLocal Cache Only
Local Cache/View
|
![]() |
GigaSpaces.com - Legal Notice - 3rd Party Licenses - Site Map - API Docs - Forum - Downloads - Blog - White Papers - Contact Tech Writing - Gen. by Atlassian Confluence |