2 Replies Latest reply: Feb 16, 2012 7:56 AM by metallist RSS

Strange remote cache behaviour

metallist Newbie

Hello, everyone!
I'm experiencing some strange remote cache behaviour using Jboss 6.1.0 and remote Infinispan 4.2.1 server over Hot Rod Protocol. Here's the code of a small test:

 

import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

public class Main {

    private static RemoteCacheManager remoteCacheManager = new RemoteCacheManager("127.0.0.1", 11222);
    private static RemoteCache<String, Object> remoteCache = remoteCacheManager.getCache("testCache");

    public static void main(String[] args) throws InterruptedException {
        CacheObject cacheObject1 = new CacheObject(1, "object1");
        remoteCache.put("key1", cacheObject1, 60, TimeUnit.SECONDS, 60, TimeUnit.SECONDS);

        CacheObject cacheObjectRes1 = (CacheObject) remoteCache.get("key1");
        System.out.println("CacheObjectRes1: " + cacheObjectRes1);

        CacheObject cacheObject2 = new CacheObject(2, "object2");
        remoteCache.put("key1", cacheObject2, 60, TimeUnit.SECONDS, 60, TimeUnit.SECONDS);

        CacheObject cacheObjectRes2 = (CacheObject) remoteCache.get("key1");
        System.out.println("CacheObjectRes2: " + cacheObjectRes2);
    }
}

class CacheObject implements Serializable {
    
    private int id;
    private String name;

    CacheObject(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "CacheObject{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

 

 

The point is that an entry under the key "key1" does not get overwritten on second call of remoteCache.put(...). So after I run the code  I get the following on my STDOUT

 

 

CacheObjectRes1: CacheObject{id=1, name='object1'}
CacheObjectRes2: CacheObject{id=1, name='object1'}

 

 

The strange thing is that if I substitute

 

remoteCache.put("key1", cacheObject1, 60, TimeUnit.SECONDS, 60, TimeUnit.SECONDS);

 

 

with

 

remoteCache.put("key1", cacheObject1, 60, TimeUnit.SECONDS);

 

 

the code starts working as expected and the entry does get overwritten.

 

I've several questions I'd like to ask:

  1. Am I doing something wrong?
  2. How do I get this thing to work, 'cause I really need to specify both lifespan and max idle time parameters for cache entries?

 

Any help or suggestions are much appreciated. Maybe I missed something on the JIRA & this has been fixed in later Infinispan releases.