You can use a CacheLoader to fetch data from an external source to cache. Also, you can
use a CacheWriter to enable the cache to write the cache data to an external source. Section 7 of
the JCache specification defines CacheLoader and CacheWriter operations.
About this task
You can enable the JCache CacheLoader and writer by running a program or by using the
objectGrid.xml file. You can use the eXtreme Scale ObjectGrid Loader
(com.ibm.websphere.objectgrid.plugins.Loader) in a JCache. The
com.ibm.websphere.objectgrid.plugins.Loader get()
method is similar to CacheLoader,
and the batchUpdate()
method is similar to the CacheWriter.
Restriction: JCache CacheWriter supports partial success. However, if a JCache is
configured with an ObjectGrid Loader, the support for partial success is limited. The two major
differences between a JCache CacheWriter and an ObjectGrid Loader include:
- When JCache is configured with a CacheWriter, the cache is updated before the CacheWriter is
called. If JCache is configured to use ObjectGrid Loader, the ObjectGrid Loader is called first,
then the cache is updated.
- JCache CacheWriter can pass the entries that failed to write to a source external to the
caller. However, ObjectGrid Loader cannot pass entries that failed to write to an external source.
If an entry fails to write to an external source, all entries are rolled back on the same partition
as the failed key.
For example, if entries A and B are on partition 1, entries C and D are on
partition 2. If the cache is configured with an ObjectGrid Loader and entry C failed to write to the
external source, both C and D are rolled back because they are on the same partition. However, if
the cache is configured to use JCache CacheWriter, only C is rolled back.
Follow the same steps to configure CacheLoader and CacheWriter for a local cache as for a
distributed, or remote, cache.
Procedure
-
Add the CacheLoader and CacheWriter to the cache by running a program.
CacheManager cacheMgr = null;
...
// Get the cache manager
...
MutableConfiguration config = new MutableConfiguration();
CacheConfiguration wxsConfig = CacheConfigFactory.createCacheConfiguration(config);
wxsConfig.setWriteThrough(true);
wxsConfig.setReadThrough(true);
// Sets the JSR cache loader or writer
wxsConfig.setCacheLoaderFactory(FactoryBuilder.factoryOf(MyCacheLoader.class));
wxsConfig.setCacheWriterFactory(FactoryBuilder.factoryOf(MyCacheWriter.class));
// Or, use the extreme Scale loader
// wxsConfig.addPlugin(CacheConfigFactory.createCachePlugin(CachePluginType.LOADER, MyObjectGridLoader.class.getName(), CachePluginScope.CLIENT_AND_SERVER));
Cache cache = cacheMgr.createCache("MyCache", wxsConfig);
-
Add the CacheLoader and CacheWriter to the cache by using the
objectGrid.xml file.
The following example is in objectGrid.xml:
<objectGrids>
<objectGrid name="Grid" >
<!-- Example 1: Use a loader -->
<backingMap name="map1" pluginCollectionRef="cacheLoader" readThroughEnabled="true"/>
<!-- Example 2: Use a loader factory -->
<backingMap name="map2" pluginCollectionRef="cacheLoaderFactory" readThroughEnabled="true"/>
<!-- Example 3: Use a writer -->
<backingMap name="map3" pluginCollectionRef="cacheWriter" writeThroughEnabled="true"/>
<!-- Example 4: Use a writer factory -->
<backingMap name="map4" pluginCollectionRef="cacheWriterFactory" writeThroughEnabled="true"/>
<!-- Example 5: Use both loader and writer -->
<backingMap name="map5" pluginCollectionRef="cacheLoaderWriter" readThroughEnabled="true" writeThroughEnabled="true"/>
<!-- Example 6: Use factory for both loader and writer -->
<backingMap name="map6" pluginCollectionRef="cacheLoaderWriterFactory" readThroughEnabled="true" writeThroughEnabled="true"/>
<!-- Example 7: Use the extreme Scale loader -->
<backingMap name="map7" pluginCollectionRef="loaderRW" readThroughEnabled="true" writeThroughEnabled="true"/>
</objectGrid>
</objectGrids>
<backingMapPluginCollections>
<backingMapPluginCollection id="cacheLoader">
<bean id="CacheLoader" className="com.mycompany.mypackage.MyJSRCacheLoader" />
</backingMapPluginCollection>
<backingMapPluginCollection id="cacheLoaderFactory">
<bean id="CacheLoader" className="com.mycompany.mypackage.MyJSRCacheLoaderFactory" />
</backingMapPluginCollection>
<backingMapPluginCollection id="cacheWriter">
<bean id="CacheWriter" className="com.mycompany.mypackage.MyJSRCacheWriter" />
</backingMapPluginCollection>
<backingMapPluginCollection id="cacheWriterFactory">
<bean id="CacheWriter" className="com.mycompany.mypackage.MyJSRCacheWriterFactory" />
</backingMapPluginCollection>
<backingMapPluginCollection id="cacheLoaderWriter">
<bean id="CacheLoader" className="com.mycompany.mypackage.MyJSRCacheLoader" />
<bean id="CacheWriter" className="com.mycompany.mypackage.MyJSRCacheWriter" />
</backingMapPluginCollection>
<backingMapPluginCollection id="cacheLoaderWriterFactory">
<bean id="CacheLoader" className="com.mycompany.mypackage.MyJSRCacheLoaderFactory" />
<bean id="CacheWriter" className="com.mycompany.mypackage.MyJSRCacheWriterFactory" />
</backingMapPluginCollection>
<backingMapPluginCollection id="OGLoader">
<bean id="Loader" className="com.mycompany.mypackage.MyXSLoaderWithRW" />
</backingMapPluginCollection>