Simple-Spring-Memcached in-depth research 1

Source: Internet
Author: User
Tags date now

Simple-Spring-Memcached (ssm) is an open-source project that uses Annatation and AOP to complete cache data operations. After carefully reading the code, I basically solved all the problems I encountered earlier, and the implementation of MultiCache was beyond my expectation. The main advantages of this project are as follows: Comprehensive integration with Spring supports two Memcached Java clients (spymemcached, Xmemcached) to implement cache operations based on Annotation, which is less invasive to code than annotation, the following describes how to use annotation to meet most of the requirements. The Annotation in the ssm project is mainly divided into the following categories of SingleCache class operations on the Cache data of a single POJO. ParameterValueKeyProvider and CacheKeyMethod are used to identify the Cache data of the List type for assembling keyMultiCache class operations, parameterValueKeyProvider and CacheKeyMethod are used to identify and assemble the keyAssignCache class and specify the key to operate the Cache data, the key simple-spring-memcached specified by assignedKey in annotation uses AOP to call and manage the cache. Its core component declares some Advice, when the corresponding entry point is met, these Advice will be executed to manage memcached. The entry point is declared by TAG. During project development, the corresponding tag description is usually provided on the DAO method, to indicate that the component's interception of this method provides the following entry points: ReadThroughSingleCache, ReadThroughMultiCache, and ReadThroughAssignCache. When the query method declares these entry points, the component first reads data from the cache. If 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, and InvalidateAssignCache when the deletion method declares these entries, the component deletes the corresponding entities in the cache, the status of the data retrieved from the Cache is the latest UpdateSingleCache, UpdateMultiCache, and UpdateAssignCache. Detailed descriptions of the Annotation functions of ReadThroughSingleCache: Read the data in the Cache. If it does not exist, the read data is stored in the Cachekey generation rule: ParameterValueKeyProvider specified parameter. If the parameter object contains the CacheKeyMethod annotation method, the method is called, otherwise, call the toString method @ ReadThroughSingleCache (namespace = "Alpha", expiration = 30) public String getDateString (@ ParameterValueKeyProvider final String key) {final Date now = new Date (); try {Thread. sleep (1500);} catch (InterruptedException ex) {} return now. toString () + ":" + now. getTime () ;} InvalidateSingleCache function: data key generation rule in the invalid Cache: When ParameterValueKeyProvider annotation is used, it is consistent with ReadThroughSingleCache when ReturnValueKeyProvider annotation is used, key is generated by @ InvalidateSingleCache (namespace = "Charlie") public void updateRandomString (@ ParameterValueKeyProvider final Long key) {// Nothing really to do here .} @ InvalidateSingleCache (namespace = "Charlie") @ ReturnValueKeyProvider public Long commit (final Long key) {return key;} UpdateSingleCache function: update the data key generation rule in the Cache: ParameterValueKeyProvider specified ParameterDataUpdateContent: the data in the method parameter is used as the ReturnDataUpdateContent to update the cached data after the method is called. Note: The preceding two annotations are used to update the cached data, @ UpdateSingleCache (namespace = "Alpha", expiration = 30) public void overrideDateString (final int trash, @ ParameterValueKeyProvider final String key, @ ParameterDataUpdateContent final String overrideData) {}@ UpdateSingleCache (namespace = "Bravo", expiration = 300) @ ReturnDataUpdateContent public String evaluate (@ ParameterValueKeyProvider final Long key) {try {Thread. sleep (100);} catch (InterruptedException ex) {} final Long now = new Date (). getTime (); final String result = now. toString () + "-U-" + key. toString (); return result;} ReadThroughAssignCache function: reads data in the Cache. If no data exists, the read data is saved to Cachekey to generate the rule: the assignedKey field in the authorization annotation specifies @ ReadThroughAssignCache (assignedKey = "SomePhatKey", namespace = "Echo", expiration = 3000) public List <String> getAssignStrings () {try {Thread. sleep (500);} catch (InterruptedException ex) {} final List <String> results = new ArrayList <String> (); final long extra = System. currentTimeMillis () % 20; final String base = System. currentTimeMillis () + ""; for (int ix = 0; ix <20 + extra; ix ++) {results. add (ix + "-" + base);} return results;} InvalidateAssignCache function: the data key generation rule of the specified key in the cache fails: assignedKey field specified @ InvalidateAssignCache (assignedKey = "SomePhatKey", namespace = "Echo") public void invalidateAssignStrings () {} UpdateAssignCache function: update the rules for generating the specified cache key: assignedKey field specified @ UpdateAssignCache (assignedKey = "SomePhatKey", namespace = "Echo", expiration = 3000) public void updateAssignStrings (int bubpkus, @ brief final List <String> newData ){}

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.