db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Thoralf Rickert" <thoralf.rick...@cadooz.de>
Subject Caching Rows
Date Thu, 27 Apr 2006 09:27:42 GMT

I like to have a feature in Torque that - I think - doesn't exists in it
at present. I could implement it in Torque, but I think it is better to
discuss it first here, because I'm not sure if it possible. Here is my

I've several tables where the data never changes during runtime of my
application (for example a table called 'currency' which holds every
currency that is used on this planet). These tables hold static data
like state-types etc. They are as good as static because of the lovely

Here is my feature:
- Add an attribute to the table schema in the xml file (like
  for example:
    <table name="currency" cacheRows="true">

- Build some methods in the Peer like:
   // the cache itself
   protected static HashMap<ObjectKey,Currency> cache = 
                new HashMap<ObjectKey,Currency>();

   // get from cache
   public static Currency getCached(ObjectKey key) {
     if (cache.containsKey(key)) return cache.get(key);
     return null;
   // add to cache
   public static Currency setCached(Currency currency) {
     if (currency == null || currency.isNew() || currency.isModified())
   // clears the cache and adds all table rows to it
   public static void rebuildCache() {

- The retrieveByPK() should first check the cache, if the row is there.
  it can't find it there, it should read it from the database. If it
  it, it can put the object in the cache:
   public static Currenxy retrieveByPK(...) {
     Currency retVal = getCached(key);
     if (retVal != null) return cached;
     ... Get it from the DB
     if (retVal != null) setCached(retVal);
     return retVal;

- Change the save() method in the Object Currency like this:
   public void save() {

   public void save(Connection connection) {

- Make it possible, to disable Cache Rebuilds for some time (for
  when you create many objects in the table). This could be done by a
  simple switch in the Peer.
   protected boolean rebuild = true;
   public static void enableCacheRebuild() {
     rebuild = true;

   public static void disableCacheRebuild() {
     rebuild = false;

  And change the rebuildCache() method:
   public void rebuildCache() {
     if (!rebuild) return;

- Additionally use a Listener concept like <TableBame>ChangeListener to
  inform everybody who is interested in table changes.
   public static void addCurrencyChangeListener(CurrencyChangeListener
   public static void
removeCurrencyChangeListener(CurrencyChangeListener listener)

I already did this manually in some of my classes and it works fine for
me. This feature cannot be enabled by default to all tables, because it
costs a lot of memory and makes a database obsolet (which isn't what we
want). It should only be used by some tables, that never change there

I'm not sure if this is useful for someone else? Is there any interest
in this? Or is my solution uncommon?


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

View raw message