Analysis of spring with ehcache instance

1 Introduction to ehcache

Ehcache is a pure Java in-process caching framework, which is fast and lean. It is the default cacheprovider in hibernate.

Ehcache is a widely used open source Java distributed cache. It is mainly aimed at general cache, Java EE and lightweight container. It has the characteristics of memory and disk storage, cache loader, cache extension, cache exception handler, a gzip cache servlet filter, support for rest and soap API.

Ehcache was originally developed by Greg luck in 2003. In 2009, the project was purchased by terracotta. The software is still open source, but some new main functions (such as consistency between fast restartability) can only be used in commercial products, such as enterprise ehcache and bigmemory. Ehcache technology is currently used by Wikimedia Foundation announced.

In short, ehcache is a good caching technology. Let's see how spring works with ehcache.

2 spring with ehcache

The system results are as follows:

3. Specific configuration introduction

There is a combination of these parts:

SRC: Java code, including interceptors, call interfaces, and test classes

src/cache-bean. XML: configure beans corresponding to ehcache, interceptor, test class and other information

src/ehcache. XML: ehcache cache configuration information

Webroot / lib: Library

4. Detailed introduction

4.1 src

4.1. 1 interceptor

Two interceptors are first configured in the code:

The first interceptor is:

com. test. ehcache. CacheMethodInterceptor

The contents are as follows:

Cachemethodinterceptor is used to intercept methods starting with "get". Note that this interceptor intercepts first and then executes the original calling interface.

There is also an Interceptor:

com. test. ehcache. CacheAfterReturningAdvice

Specific contents:

Cacheafterreturningadvice is used to intercept methods starting with "update". Note that this interceptor executes the original calling interface first and then is intercepted.

4.1. 2 call interface

Interface name:

com. test. service. ServiceManager

The details are as follows:

The implementation class name is:

com. test. service. ServiceManagerImpl

The details are as follows:

4.1. 3 test class

The test class name is:

com. test. service. TestMain

The specific contents are as follows:

Note here that the obtained beans are produced through the proxy factory proxyfactory. Only in this way can the interception effect be achieved.

It can be seen that four calls are set in the test class, and the execution order is:

First lookup second lookup first update third lookup

4.2 src/cache-bean. xml

cache-bean. XML is used to configure beans corresponding to ehcache, interceptor, test class and other information, as follows:

The contents of each bean are annotated. It is worth noting that do not forget the proxy factory bean.

4.3 src/ehcache. xml

ehcache. The details of ehcache cache configuration are stored in XML, as follows:

You can see that the storage location of the cache is set to "D: \ temp \ cache", and the cache name is set to "com. TT", as shown in the figure:

4.4 WebRoot/lib

For the required Java library, see the system structure picture at the beginning, which is omitted here.

5 test

Execute the test class, and the test results are as follows:

Through the implementation results, we can see that:

After the first search is intercepted, it is found that it is the first time to intercept and there is no cache. Therefore, first execute the original interface class to get the data to be queried, which may be obtained through database query, then generate the cache and put the data obtained from the query into the cache.

After the second search is intercepted, it is found that the cache already exists, so the original interface class is no longer executed, that is, the database is no longer queried, and the query data is obtained directly through the cache. Of course, it's just a simple print.

Then it is the first update. After being intercepted, all the data in the cache is stored in the database and the cache is deleted.

The last is the third query. After being intercepted, it is found that there is no cache in the system, so the original interface class is executed to query the database, create a cache, and put the data obtained from the new query into the cache. The method is the same as the first query.

So far, we have implemented the operations that spring needs to complete with ehcache.

6 attachment source code

The attachment source code can be obtained from my GitHub website.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>