Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to cache objects MANUALLY

 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello

some o-r mapping tools can cache the objects that have been queried,then next time these objects are required,it don't need to access the database again,it can also monitor the database updating.
i wonder how i can implement such "cache" function MANUALLY? because i DON'T want to use ANY o-r mapping tools. i only use the jdbc to query database,then generate the object.

who can give me some clue?? or articles? or sample codes??

thank you!!!
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Caches are complex things. You might find it easier to use an existing caching framework rather than write your own. A quick google turns up this. No idea if its any good. �3000 per CPU too.
 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks,i don't want the complex function,i only want to a simple one,who know the BASIC idea of the cache?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the most basic thing you could do is maintain some sort of Collection of already found objects. Suppose you have the objects User and Item. You could keep two Maps which hold the alread found objects. The idea could be that when you call some CRUD business method on either of these object types, it is the cache you go to first, if they are not there then its the DB you go to.

As I say this is very simple. You would have to code such issues as the cache becoming invalid - what happens for example if some other process deletes an object from the DB currently resident in your cache? You have think of what you do to stop the cache growing too big - perhaps have a defined limit of the number of entries possible in your cached Maps and push one out to put a new one in when you get to the limit. You would need to think carefully how you decide which object gets bumped out of the cache, otherwise this could make you cache more of a drain on performance rather than an aid. Your cache would not work in a cluster - so couldn't be used in a J2EE app (writing a clusterable cache is hard work). You might also have to decide at what point your cache commits objects to the DB - if it commits every object you update or insert straight away.

All this, since it is sort of "infrastructure" code, is usually best implemented by reusing an existing caching framework.
[ March 17, 2005: Message edited by: Paul Sturrock ]
 
Robert Hayes
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's something you can play with for a read-only cache:


[ March 18, 2005: Message edited by: Robert Hayes ]
 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for all of your helps,let me try.
nowaday ,which o-r mapping tools support cluster environment?i learn that hibernate don't support it.
 
Robert Hayes
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe TopLink does...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic