Spring 3.1 Exciting new features: annotation-driven caching

Source: Internet
Author: User
Keywords Caching new features annotation driven
Tags .mall cache cached caching caching capabilities class code configuration

By reading this article, you can quickly grasp the powerful caching technology that spring brings and provide caching capabilities for existing code with little configuration.

Spring 3.1 introduces an exciting, annotated (annotation) cache technology that is inherently not a specific caching implementation (such as EHCache or oscache), but rather an abstraction of the cache usage, The effect of the return object of a cached method can be achieved by adding a small amount of annotation that it defines in the existing code.

Spring's caching technology also offers considerable flexibility not only to use Spel (Spring Expression Language) to define cached keys and various condition, but also to provide out-of-the-box cache temporary storage scenarios and to support and mainstream professional caching such as EHCache integration.

Its characteristics are summarized as follows:

Using a small amount of configuration annotation annotations enables the existing code support cache to support out-of-the-box Out-of-the-box, which supports the Spring Express Language with caching without installing and deploying additional third-party components. can use any of the object's properties or methods to define the cached key and condition support AspectJ, and support custom key and custom cache manager through its implementation of any methods of caching, with considerable flexibility and scalability

This article will focus on the above characteristics of Spring cache for a detailed introduction, mainly through a simple example and the principle of the expansion, and then we will look at a more practical cache example, and finally introduced the spring cache usage restrictions and considerations. Ok,let ' s begin!

So how did we do it?

Here we show a fully customizable cache implementation that does not use any third party component to implement the memory cache of an object.

The scene is: An account Query method to do caching, to account name key, account object for http://www.aliyun.com/zixun/aggregation/9541.html ">value, when the same account name query account, Returns the result directly from the cache, otherwise the cache is updated. The Account Query service also supports reload caching (that is, emptying the cache).

Define an entity class first: The account class, with the basic ID and name attributes, and the getter and setter methods

Listing 1. Account.java

Package Cacheofanno; public class Account {private int id; private String name, public account (String name) {this.name = name;} public int getId () {R Eturn ID; public void setId (int id) {this.id = ID;} public String GetName () {return name;} public void SetName (String name) {this.name = name; } }

Then define a cache manager, which is responsible for implementing the caching logic, supporting the addition, modification and deletion of objects, and supporting the generics of the value object. Follows:

Listing 2. Mycachemanager.java

Package Oldcache; Import Java.util.Map; Import Java.util.concurrent.ConcurrentHashMap; public class Mycachemanager<t> {private map<string,t> cache = new concurrenthashmap<string,t> (); Public T GetValue (Object key) {return cache.get (key);} public void Addorupdatecache (String key,t value) {Cache.put (key) Value); The public void Evictcache (String key) {//Deletes a record in the cache according to key (Cache.containskey (key) {Cache.remove (key)}} public void Evictcache () {//empty all records in Cache Cache.clear ();}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.