Java caching mechanism (top) map and spring annotations @cacheable

Source: Internet
Author: User

Learn from https://www.cnblogs.com/ms-grf/p/7249220.html

The purpose of caching is to save access time and reduce the resource consumption caused by large concurrent accesses.

First, external memory

In addition to computer memory and CPU cache memory, such as the common C, D, E, f disk, there are U disk, floppy disk, hard disk, CD-ROM and so on. The integrity of the data can still be saved after a power outage.

Second, memory

Used to communicate with the CPU. All programs in the computer are carried out in memory, and all the operational data in the CPU and the data exchanged between and external memory are stored. Data loss is not saved.

Three, high-speed buffering

In general, the CPU processing data is very fast, and the memory speed is relatively slow, easy to cause performance problems, to solve this problem using high-speed buffering. Principle: The system stores the data that the CPU accesses frequently in the high-speed buffer area, when the CPU accesses certain data, now the buffer is searched, cannot find again to the memory to find, greatly improved the CPU computation efficiency.

When some resources or data are used frequently, and these resources or data are stored outside the system, such as databases, hard disk files and so on, each time the data is taken from the database or hard disk to obtain, the speed will be slow, will cause performance problems.

Iv. caching mechanisms commonly used in Java

Most of the time we do development in the unconscious with the cache, common multi-threaded memory visible, character stream and IOC are cached

Cache depth from the front-end to the database are involved, the page cache is the best, because the use of less resources

1. Static HashMap

Based on the memory cache, a map can take care of the cache of reference objects, the problem is that the validity of the preservation object and the life cycle can not be controlled, improper handling will lead to a sharp rise in memory, not suitable for large data volume of the cache.

Need to define a cache manager, for the implementation of caching logic, the implementation of object additions and deletions, support the generic type of the value object, is actually put the data in the map

    

Import above the service layer Cachecontext

    

2. Springcache annotation @cacheable (value = "Accountcache")

This annotation means that when this method is called, it is queried from a cache called Accountcache, and if not, the actual method (that is, querying the database) is executed, and the result of execution is cached, otherwise the object in the cache is returned. The key in the cache here is the parameter accountname,value is the account object. The "Accountcache" cache is the name defined in the Spring*.xml. We also need a spring configuration file to support comment-based caching

Add AccountService2 based on the above

    

Need to configure Cachmenager,bean inside the spring Accountcache

    

Note This spring configuration file has a key cache-enabled configuration entry:

<cache:annotation-driven/>

    

Java caching mechanism (top) map and spring annotations @cacheable

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.