加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Caching Best Practices--reference

发布时间:2020-12-14 06:17:35 所属栏目:Java 来源:网络整理
导读:reference:http://java.dzone.com/articles/caching-best-practices There is an irresistible attraction to writing custom caching solutions,since it seems to be the easiest path to “improving” the overall application performance. Well,cachi

reference:http://java.dzone.com/articles/caching-best-practices

There is an irresistible attraction to writing custom caching solutions,since it seems to be the easiest path to “improving” the overall application performance. Well,caching is a great technique,but there are few steps to consider before even considering it.

Best practices

  1. A key/value collection is not a Cache

    Almost all projects I worked on have been using some sort of custom caching solutions,built on top of Java Maps. A Map is not an out-of-the-box Caching solution,since a Cache is more than a key/value store. A Cache also requires:

    • eviction policies
    • max size limit
    • persistent store
    • weak references keys
    • statistics

    A Java Map doesn’t offer these features and you shouldn’t spend your customer’s money to write a custom cache solution either. You should choose a professional cache like??or?,which are both powerful and simple to use. Those tools are constantly tested by all those projects employing them,so the code quality is higher than most custom built solutions.

  2. Use a cache abstraction layer

    A very flexible solution is the?. The?annotation allows you to separate the business logic code from the caching cross-cutting concern. The caching solution is therefore configurable and it’s not going to pollute your business methods.

  3. Beware of the caching overhead

    Every API has a cost and caching is no different. If you cache a web service or an expensive database call,then the overhead is probably negligible. If you use a local cache for a recursive algorithm,you need to be aware of the overall caching solution overhead. Even the Spring cache abstraction has an?,so make sure the benefits outweigh the costs.

  4. If your database queries are slow,the cache should be your last resort

    If you use an??tool like Hibernate,that’s the first place where your optimization process should start from. Make sure the??is properly designed,and you don’t suffer from?. You could also??to validate the ORM generated queries.

    When you’re done optimizing your ORM SQL query generation,you should check your database for slow queries. Make sure all indexes are in place and that your SQL queries are effective.The indexes must always fit into RAM,otherwise you will hit the more expensive SSD or HDD. Your database has the ability to cache query results,so take advantage of it.

    If the data set is large and the growth rate is high you could horizontally scale it on multiple?.

    If all of those actions are not enough,you may consider a professional caching solution such as?.

  5. What about data consistency?

    When you start using a cache in front of your business layer,the data consistency constraint is being challenged. The benefits of??may be compromised if the cache is not properly synchronized with the database. This is like keeping a denormalized form of your actual data. If a root entity changes it may affect a large portion of your cache. If you discard the cache entries,all the caching benefits are lost. If you asynchronously update the cache entries you loose the strong data consistency,leaving you with an??data model.

Playing time

Inspired by this very interesting??on the Java 8??Map addition,I decided to present you a??alternative that has the following advantages:

  1. there is a fixed cache size of 2 entries
  2. it works with Java 1.6

<a class="item printSource" title="print" href="http://java.dzone.com/articles/caching-best-practices#printSource"&gt;print<a class="item about" title="?" href="http://java.dzone.com/articles/caching-best-practices#about"&gt;?

? fibonacciCache = CacheBuilder.newBuilder()
?() {
?
?
?
?
?
?
?
??
??
?

And the output is:

<a class="item printSource" title="print" href="http://java.dzone.com/articles/caching-best-practices#printSource"&gt;print<a class="item about" title="?" href="http://java.dzone.com/articles/caching-best-practices#about"&gt;?

Code available on?.

?

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone,Inc.)

?
<li class="taxonomy_term_4360"><a title="" href="http://java.dzone.com/category/dzone-taxonomy/java" rel="tag">Java

?
<li class="taxonomy_term_807"><a title="" href="http://java.dzone.com/category/tags/performance" rel="tag">performance

?
<li class="taxonomy_term_58297 last"><a title="" href="http://java.dzone.com/category/content-form/tips-and-tricks" rel="tag">Tips and Tricks

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读