Do you need fine grained control over your Cache? Do you need advanced cache management? Do you need cache partitions? Then Tangosol Coherence is for you. The cache objects must be Serializable or Externalizable. Because of the high performance, Oracle recommends to use ExternalizableLite instead. So here you go:
#The SClass to cache
public class SClass implements ExternalizableLite {
private String a;
public SClass () {
}
public void setA (String a) {
this.a = a;
}
public String getA () {
return a;
}
public String toString () {
return a;
}
public void readExternal (DataInput dataInput) throws IOException {
a = dataInput.readUTF ();
}
public void writeExternal (DataOutput dataOutput) throws IOException {
dataOutput.writeUTF (a);
}
}
# Now how to use it?
public class CacheClient {
private NamedCache nCache;
public CacheClient () {
nCache = CacheFactory.getCache ("Name");
}
public static void main (String[] args) {
CacheClient cacheClient = new CacheClient ();
// -- Put the Object in Cache
SClass sC = new SClass ();
sC.setA ("Srivastava");
cacheClient.putValue ("a", sC);
// -- Now retrieve it
System.out.println ("Entered: " + cacheClient.getValue ("a"));
}
private void putValue (String k, Object v) {
nCache.put (k, v);
}
private String getValue (String k) {
Object obj = nCache.get (k);
// -- Assuming the Object was added in the Cache and not expired
// -- More details on Object expiration later.
return obj.toString ();
}
}
Thats it! And you get a high performing Caching infrastructure.
No comments:
Post a Comment