Java DAO Caching

I am developing a Java application environment and I am having a little problem due to my lack of experience.

I have a custom DAO that gets "Article" objects from a database. I have an Article class, and the DAO has a method called getArticle(int id) , this method returns Article . Article has a Category object, and I'm using lazy loading.

So, when I request an article category ( Article a = new Article(); a.getCategory(); ), the Article class gets the Category from the DAO and then returns it.

Now I'm going to cache it, so when I query the article category several times, the database is only requested once.

My question is: where should I put this cache? I can put it in the Article class (in the DTO), or I can put it in the DAO class.

What do you say?

+4
source share
5 answers

My question is: where do I put this cache? I can put it on the Article Class (in the DTO), or I can put it in the DAO class.

As others have noted, this sounds really like reinventing the wheel. But let's look at both cases anyway, so that you better understand how ORM works.

If you save a category in an article , the same category will be loaded again and again when accessing 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 be useful for the cache, but you also need to take care to update the cache when the category has changed.

 saveCategory( Category c ) { cache.put( c.id, c ); <save in database> } 

The problem with this approach is that (1) the cache can grow over time and (2) that an external update in the database will never be reflected unless you have a timeout mechanism or a way to clear the cache.

Actually, ORM, such as Hibernate, has three levels of caching:

  • The essence itself . When a category is loaded with laziness, it is stored in the article object for subsequent direct access.
  • First level cache . This is the cache of the current session / transaction. The same object will not be loaded twice, but retrieved from the cache.
  • 2nd level cache . This is the cache in all sessions / transactions. This corresponds to the option to cache the value in the DAO. The second-level cache is sometimes more complicated due to the reasons I mentioned above.

I hope you see the best disadvantages / advantages of each type of cache. For more information, check out the popular ORM documentation. Abstracts are general and well-documented.

+8
source

Do you find using hibernate ?

+8
source

Consider using Hibernate or an even simpler form (JPA)

+3
source

The goal of the DAO is to distract the persistence mechanism. Since "where" this category (DB, file on disk, network socket, cache) occurs, is part of this persistence mechanism, the DAO must "hide" it from the object that consumes it.

+2
source

It depends on whether your Article single class for direct access to the DAO or not.

Personally, I would do the caching in Article for the simple fact that not everyone who uses the DAO will want to cache. Also, in terms of the fact that one class does one and only one, since you already have lazy loading in Article , it would be pointless to move part of the loading path to the DAO

0
source

Source: https://habr.com/ru/post/1309503/


All Articles