java infinispan the easiest example

760 Views Asked by At

I would like to use infinispan (7.0.2.Final) to provide persistency for my server application written in java. Before I started to implement it, I tried the simplest scenario but it did not work...

i.e I implemented two simple unit tests and I run them one after another (i.e. I run them separately).

The first test consisted only of:

Cache<Object, Object> c = new DefaultCacheManager().getCache();
c.put("key1", "value1");

And the second one:

Cache<Object, Object> c = new DefaultCacheManager().getCache();
String result = (String) c.get("key1");

but I received result = null ...

What am I doing wrong? I read on infinispan pages that should run out of the box...

2

There are 2 best solutions below

0
On

It seems that you are expecting persistence (not clustering as @tsykora suggested). The link with programmatic configuration is right, but you'd probably want to use SingleFileStore (as the simplest persistent store).

DefaultCacheManager manager = new DefaultCacheManager();
Configuration c = new ConfigurationBuilder().persistence()
    .addSingleFileStore()
    .location("/tmp/myDataStore")
    .maxEntries(5000);

manager.defineConfiguration("myCache", c);
Cache<Object, Object> cache = manager.getCache("myCache");

(code not tested but this is the idea).

2
On

The issue here is that your configuration is not clustered. By default, there is used CacheMode=LOCAL and therefore, you can't expect data replication.

Try to set up appropriate cache mode in clustering() element:

http://infinispan.org/docs/7.0.x/user_guide/user_guide.html#_configuring_cache_programmatically