Introduction to simple-spring-memcached

Source: Internet
Author: User

Memcached is an excellent distributed cache tool that effectively improves the performance of data retrieval by primary key. The simple-spring-memcached component integrates with the Spring framework to simplify the call of memcached.

Simple-spring-memcached uses AOP in essence to implement cache calling and management. Its core component declares some advice. When a corresponding entry point is encountered, will execute these advice to manage memcached.

The entry point is declared by TAG. During project development, the DAO method is usually described as a tag to indicate that the component blocks the method.
Components provide the following entry points:
Readthroughsinglecache, readthroughmulticache, and readthroughassigncache
When the query method declares these entries, the component first reads data from the cache. when the data is obtained, the query method is skipped and the result is returned directly.
If no data is obtained, the query method is executed and the query result is cached for the next query.
Invalidatesinglecache, invalidatemulticache, invalidateassigncache
When the deletion method declares these entries, the component deletes the corresponding entities in the cache.
Updatesinglecache, updatemulticache, updateassigncache
When the update method declares that these start points are, the component updates the corresponding entities in the cache so that the data status retrieved from the cache is the latest.

Simple-spring-memcached does not provide the Implementation of the cache mechanism, but is designed to make cache calls simpler.
Third-party components (such as x-memcached and spy-memcached) are used in the implementation of cache. The official configuration for these two components is provided.

Http://code.google.com/p/simple-spring-memcached/wiki/Getting_Started

Use of simple-spring-memcached:

1. Add the following two sentences to the spring configuration file:

<import resource="simplesm-context.xml" /><aop:aspectj-autoproxy />

The simplesm-context.xml is encapsulated in the simple-spring-memcached-*. jar file, which is mainly used to load the advice of the component core for program scheduling.
Since simple-spring-memcached is mainly an AOP-based proxy, adding <AOP: aspectj-autoproxy/> to the proxy mechanism plays a role.

2. Define the memcached Client

Memcached has two common Java clients: spymemcached and xmemcached. xmemcached supports multithreading.

<bean name="defaultMemcachedClient" class="com.google.code.ssm.CacheFactory"><property name="cacheClientFactory"><bean class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" /></property><property name="addressProvider"><bean class="com.google.code.ssm.config.DefaultAddressProvider"><property name="address" value="192.168.7.131:11211" /></bean></property><property name="configuration"><bean class="com.google.code.ssm.providers.CacheConfiguration"><property name="consistentHashing" value="true" /></bean></property></bean>

Com. Google. Code. SSM. cachefactory is a factorybean that returns a cache object for advice.
The address attribute defines the IP address and port number of the cache node.

The consistenthashing attribute defines the cache node search method.

3. object definition

Memcached is equivalent to a powerful map that caches pojo objects in the form of key/value. When defining objects, you can use the @ cachekeymethod tag to specify the key value for the object, at the same time, each member variable of the object and the object must be serializable. The serializable interface can be implemented, or the serialization method can be specified for the object through the externalizable interface.

public class User implements Serializable {private static final long serialVersionUID = 7517080513591583073L;private String userId;private String username;private String password;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@CacheKeyMethodpublic String getUserId() {return userId;}public void setUserId(String userId) {this.userId = userId;}}

4. Dao Definition
Cache operations usually intercept Dao methods, and add necessary notifications to achieve the effect of adding, deleting, modifying, and querying

The entry point declaration is mainly implemented through the labels mentioned earlier.

Public class userdaoimpl implements iuserdao {Private Static final string namespace = "ns"; private Map <string, user> Users = new hashmap <string, user> (); @ overridepublic void saveuser (User user) {users. put (user. getuserid (), user);}/*** when the getbyid query method is executed, the system first obtains the entity corresponding to the userid from the cache * If the entity has not been cached, execute the query method and put the query result into the cache */@ override @ readthroughsinglecache (namespace = namespace, expiration = 3600) public user getbyid (@ parametervaluekeyprovider string userid) {system. out. println (userid); Return users. get (userid);}/*** when the updateuser method is executed, the system updates the entity * corresponding to the userid in the cache to update the object content to the entity described by @ * dataupdatecontent tag */@ updatesinglecache (namespace = namespace, expiration = 3600) @ overridepublic void updateuser (@ parametervaluekeyprovider @ parameterdataupdatecontent user) {users. put (user. getuserid (), user);}/*** when the deleteuser method is executed, the system will delete the entity corresponding to the userid in the cache */@ invalidatesinglecache (namespace = namespace) @ overridepublic void deleteuser (@ parametervaluekeyprovider string userid) {users. remove (userid );}}

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.