You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Jason McKerr <mc...@nacse.org> on 2003/08/04 20:21:32 UTC

UPDATE: OSCache with OJB -- How To

I've updated this mail to reflect the new release (Beta 2) of OSCache that Chris Miller
et al have released today.  It includes some important changes/fixes for using
OSCache as your clustered cache in OJB.

Some notes:

1) I've tested OSCache/OJB so far with the Javagroups multicast communications layer.
2) I Have NOT tested with JMS, which is now supported in OSCache.  When I've run some tests,
I'll let you know.
3) Due to method changes/refactorings, the class included below has changed.
4) If you used the older version of JavaGroups (that came with OSCache beta 1) you will need to
 upgrade the javagroups-all.jar file to the new one included in the OSCache Beta 2 distribution.
5) Once I've tested with JMS, I'll update this how-to and send it along.

Here's the general How-to:


1) First, download OSCache from the following links:

https://oscache.dev.java.net/files/documents/629/603/oscache_2_0_0b2.zip

2) put oscache.jar and javagroups-all.jar in your classpath

3) Add oscache.properties somewhere where it can be accessed (classpath,
or WEB-INF/classes or whatever).  This file allows you different
configurations (disk/memory caching, algorithms, etc). Please go to the
following for more info on that:

http://www.opensymphony.com/oscache/

4) In OJB.properties, change the ObjectCacheClass value to ObjectCacheOSCacheImpl
(you may want to change the package name if you add package level info
to the class).

5) add the Class that I've included to the proper package as described
in step 4 (again you may want to change packages).

6) For JavaGroups clustering, make sure that you enable the following clustering listener in
oscache.properties (this uses the javagroups clustering, not JMS):

cache.event.listeners=com.opensymphony.oscache.plugins.clustersupport.JavaGroupsBroadcastingListener

7) Also, you'll want to uncomment one of the Cluster Properties values in oscache.properties. I used:

cache.cluster.multicast.ip=231.12.21.132


Let me know about questions/comments.

Jason


---------------------------HERE'S THAT CLASS----------------------------
import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.cache.ObjectCache;
import org.apache.ojb.broker.cache.RuntimeCacheException;

import com.opensymphony.oscache.base.CacheEntry;
import com.opensymphony.oscache.general.GeneralCacheAdministrator;

public class ObjectCacheOSCacheImpl implements ObjectCache {

  private GeneralCacheAdministrator admin;
  private static final int NO_REFRESH = CacheEntry.INDEFINITE_EXPIRY;

  public ObjectCacheOSCacheImpl() {
  }

  public ObjectCacheOSCacheImpl(PersistenceBroker broker) {
      admin = new GeneralCacheAdministrator();
  }

  public void cache(Identity oid, Object obj) {
    try {
      admin.putInCache(oid.toString(), obj);
    }
    catch (Exception e) {
      throw new RuntimeCacheException(e.getMessage());
    }
  }

  public Object lookup(Identity oid) {
    try {
      return admin.getFromCache(oid.toString(), NO_REFRESH);
    }
    catch (Exception e) {
      return null;
    }
  }

  public void remove(Identity oid) {
    try {
      admin.flushEntry(oid.toString());
    }
    catch (Exception e) {
      throw new RuntimeCacheException(e.getMessage());
    }
  }

  public void clear() {
    if (admin != null) {
      try {
        admin.flushAll();
      }
      catch (Exception e) {
        throw new RuntimeCacheException(e);
      }
    }
  }
}



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org