Infinispan tutorial part 1
- Published: 15 March 2010
JBoss cache vs Infinispan
On the other hand, Infinispan has a map-like data structure that is distributed among a grid of servers. Thanks to its lock- and synchronization-free structure, Infinispan is designed from grounds up to scale your cache-based applications on a different magnitude: here we are not speaking in terms on 1, 5 or 10 nodes - you can distribute your Cache on hundreds of servers each one adding virtually its memory to the Cache.
Specifically, Infinispan nodes issue RPCs – remote procedure calls – to neighbouring nodes, which are invoked on a target node and a response is returned, in many ways similar to a local invocation.
Enough with introductions, let's get working with Infinispan: downlaod the latest stable release from JBoss site:
Unzip the archive. Let's see what it contains:
The core library, infinispan-core.jar, is contained in the root infinispan-4.0.0.FINAL folder. Additionally you need to include dependencies which are located in the "lib" folder.
In the "bin" folder you can find the importConfig script which can be used to import your existing JBoss cache configuration. There you can find as well a GUI demo which shows the most interesting features of Infinispan.
In the "etc" folder you can find some configuration samples which can be used as a good start for learning Infinispan configuration.
Finally in the "modules" directory you can find additional modules like the Query module, the Lucene search module, the Jopr module for monitoring Infinispan and others.
Setting up a simple example
One is in embedded mode, where you start an Infinispan instance within your JVM. The other is client/server mode,where you start a remote Infinispan instance and connect to it using a client connector.
DefaultCacheManager m = new DefaultCacheManager(); Cache<String, String> cache = m.getCache(); cache.put("name", "Francesco"); String name= cache.get("name"); System.out.println("Name is "+name);
The Cache interface is the JSR-107 Cache compatible interface which provides the cuncurrent, distributed cache structure and contains standard methods for adding (put) and retrieving (get) data from the Cache.
You can safely reuse this object, as well, once you have instantiated it.
Using a Custom configuration
Using a configuration file:
String configFile = "/path/to/my/infinispan_config.xml"; DefaultCacheManager m = new DefaultCacheManager(configFile); Cache<String, String> c = m.getCache();
<infinispan> <global /> <default /> <namedCache name="A"> <namedCache name="B"> </infinispan>
<global> contains System wide local settings.
<default> refers to configuration which is used by all Infinispan caches.
<namedCache> can be used to define custom specialized caches
For example, if you want to define a custom cache for evicting data from the cache you could use the following file:
<infinispan> <namedCache name="evictionCache"> <eviction wakeUpInterval="500" maxEntries="5000" strategy="FIFO" /> <expiration lifespan="60000" maxIdle="10000"/> </namedCache> </infinispan>
This specialized named cache configuration can be loaded using a the getCache(String s) method:
String configFile = "/path/to/my/infinispan_config.xml"; DefaultCacheManager m = new DefaultCacheManager(configFile); Cache<String, String> c = m.getCache("evictionCache");
Configuring a Cache programmatically
Sometimes it can be helpful to define the Cache attributes programmatically. No fear, it's absolutely easy. Just define an instance of the org.infinispan.config.Configuration class and set custom attributes. In this example we're going to override the default items expiration lifespan, setting it to 10000 ms.
Configuration c = new Configuration(); c.setExpirationLifespan(10000); manager.defineConfiguration("myconfig", c); Cache cache = manager.getCache("myconfig");