My question is: where should I put
that cache? I can put it on the
Article class (in the DTO), or I can
put it on the DAO class.
As the others mentioned, it sounds indeed a bit like re-inventing the wheel. But let's consider both cases anyway, so that you will get better understanding of how ORM works.
If you store the category in the article, the same category will be loaded again and again if accessed by different articles.
getCategory() {
if( category == null ) { category = <load from DAO> }
return category;
}
If you store it in the DAO, then all articles with the same category will benefit the cache, but you need to take care to update the cache as well when the category is changed.
saveCategory( Category c ) {
cache.put( c.id, c );
<save in database>
}
Problem with this approach is that (1) the cache may grow large over time, and (2) that external update in the database will never be reflected if you don't have a timeout mechanism or a way to clear the cache.
Actually, ORM such as Hibernate have three levels of caching:
- The entity itself. When the category is lazy loaded, it's stored in the article entity for subsequent direct access.
- The first-level cache. This is the cache of the current session/transaction. The same entity won't be loaded twice but fetched from the cache.
- The 2nd-level cache. This is a cache across all sessions/transactions. It corresponds to the option of caching the value in the DAO. 2nd level cache is sometimes trickier because of the reasons I mentioned above.
Hope you see better the shortcomings/benefits of each type of cache. For more information, look in popular ORM documentation. Theses issues are common and well documented.