Continue to use EhCache and cacheehcache without proper Disk Cache in the net environment

Source: Internet
Author: User

Continue to use EhCache and cacheehcache without proper Disk Cache in the net environment

  

When talking about caching, you may be confused. All types of caching can be analyzed one by one, but finding a suitable Disk Cache under net seems a little difficult.

 

I. background

This is the case. In a recent project, You need to draw some reports on the web end. Because the basic data sources of reports are in full memory, the memory is relatively tight, everyone may

As you know, for example, some scatter charts have a large amount of report data. to speed up, I need to cache two types of data:

 

1. Calculate the intermediate results based on the basic data source. For the next acceleration, the data volume is relatively large when it is cached for dozens of minutes.

2. cache the service Response for 30 minutes. The data volume is relatively large, about 10-50 MB.

 

As I said just now, the memory is quite tight. It would be embarrassing to put the data into the memory, which is not allowed by the business. If such a large data block is placed in the distributed cache, with

Width is also a problem and will face more risks of timeout, so the best way is to use the local disk cache, so that you can get a balance between performance and memory ~~~

 

Ii. Find a solution

  

The balance point is found. in the net field, I seldom heard of the concept of disk caching. Since I have heard little about it, it is a wave in nuget, and then I found a top1 diskcache, such:

 

 

Pull down a test, lying down, just a CURD operation, even the TTL and TTI functions are not, but also donate bitcoin, O (∩ _ ∩) O

 

Since there is no good solution under net, I can only look at it under java, and soon I found ehCache. Looking at the official instructions, the intervention method is the same as the previous one, use thrift for C #

And Java. (You can refer to the previous article about how to use thrift.) For example:

 

3. Ehcache Configuration

1. maven ehcache address

         <!-- https://mvnrepository.com/artifact/org.ehcache/ehcache -->         <dependency>             <groupId>org.ehcache</groupId>             <artifactId>ehcache</artifactId>             <version>3.5.2</version>        </dependency>

 

Ehcache Official Website: http://www.ehcache.org/, you can simply understand the specific use of the official samples, DBEngines ranking is also very good.

 

2. Full-code configuration

 

Next, you can write a piece of code to test it. Insert a 10000-character cache to diskcache and insert it 1000 times to check the efficiency. The Code is as follows:

Public class App {public static void main (String [] args) throws CachePersistenceException {LocalPersistenceService persistenceService = new DefaultLocalPersistenceService (new DefaultPersistenceConfiguration (new File ("C: \ 1 \ cache "); PersistentUserManagedCache <String, String> cache = UserManagedCacheBuilder. newUserManagedCacheBuilder (String. class, String. class ). with (new UserManagedPersistenceContext <String, String> ("persistentCache", persistenceService )). withResourcePools (ResourcePoolsBuilder. newResourcePoolsBuilder (). disk (10L, MemoryUnit. GB, true )). withExpiry (Expirations. timeToLiveExpiration (Duration. of (30, TimeUnit. MINUTES ))). build (true); StringBuilder sBuilder = new StringBuilder (); for (int I = 1; I <10000; I ++) {sBuilder. append (I);} long startTime = System. currentTimeMillis (); // obtain the start time for (int I = 1; I <1000; I ++) {String key = "username" + I; String value = sBuilder. toString (); cache. put (key, value); System. out. println (String. format ("% s: current key = % s inserted into cache", I, key);} long endTime = System. currentTimeMillis (); // obtain the end time System. out. println ("program running time:" + (endTime-startTime) + "ms ");}}

 

 

The speed is more than 600 milliseconds, and the time is acceptable. It is also suitable for my project.

Of course, you can also use xml to dynamically configure ehcache, or use spring data to integrate this ehcache. Because it is mainly used for java assistance, we will not discuss it in detail,

Well, let's talk about it in this article. I hope it will help you.

 

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.