Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Object Relational Mapping and the fly likes how to cache  objects MANUALLY Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "how to cache  objects MANUALLY" Watch "how to cache  objects MANUALLY" New topic
Author

how to cache objects MANUALLY

zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
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

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
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

Joined: Apr 14, 2004
Posts: 10336

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

Joined: Oct 24, 2004
Posts: 116
Here's something you can play with for a read-only cache:


[ March 18, 2005: Message edited by: Robert Hayes ]
zb cong
Ranch Hand

Joined: Jan 14, 2002
Posts: 416
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

Joined: Oct 24, 2004
Posts: 116
I believe TopLink does...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to cache objects MANUALLY
 
Similar Threads
O/R Mapping and EJBs
what's the most popular "bitter" anti-pattern?
Is ORM suitable for *big* apps ?
Spring JDBC Vs Hibernate?
To use ORM or not