You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by Thoralf Rickert <th...@cadooz.de> on 2006/04/27 11:27:42 UTC

Caching Rows

Hi!

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
"problem":

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
Normalisation.

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

- 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())
return;
     cache.put(currency.getPrimaryKey(),currency);
   }
   // 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.
If
  it can't find it there, it should read it from the database. If it
finds
  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() {
     ...
     CurrencyPeer.rebuildCache();
   }

   public void save(Connection connection) {
     ...
     CurrencyPeer.rebuildCache();
   }

- Make it possible, to disable Cache Rebuilds for some time (for
example,
  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
listener)
   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
content. 

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

bye
Thoralf



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


Re: Caching Rows

Posted by Thomas Vandahl <th...@tewisoft.de>.
Thoralf Rickert wrote:

> 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
> Normalisation.

See 
http://db.apache.org/torque/releases/torque-3.2/runtime/reference/managers-cache.html

If you have any questions regarding these features, please ask. I use 
this quite heavily.

Bye, Thomas.


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