Cached memory Cache __java in Java

Source: Internet
Author: User

Cached memory Cache in Java

1. Why should the cache exist

Application server resources are limited, and the number of requests received by the database per second is limited. If you use limited resources to provide as much throughput as possible, one way: Reduce the amount of computing, shorten the request process (reduce network IO or hard disk IO), then the cache will be able to do

2. What kind of data can be stored in the cache

Unchanging data over a period of time

3. Why should there be a local cache

In the system, some data, small amount of data, but access is very frequent (such as the national standard administrative area data), for this scenario, need to get the data into the application of the local cache in order to enhance the system's access efficiency, reduce unnecessary database access (database access Occupy database connection, while the network consumption is relatively large) One thing to note, however, is the footprint of the cache and the expiration policy of the cache.

4. The principle of memory cache or local caching

The way to implement caching is to use a map, and the implementation of the cache is divided into three major steps: First, to the cache to find out whether there are the objects we need, if there is, then directly return. Second: If the object is not in the cache, add its data to the cache. For the next use.     Third: If you find the appropriate data, or create the appropriate data, then use this data directly. However, the key of the map object in Java is set to string type and is also convenient to find.

It is actually a map collection class implementation that saves the cached content to memory, as long as the server does not close the cache and is not purged unless the person clears

Import Java.util.Map;
Import Java.util.concurrent.ConcurrentHashMap;

public class Cacheclass {
	
	private static map<string,string> cache = new Concurrenthashmap<string, String > ();
	
	public static void Setcache (string key, String obj, long seconds) {
		cache.put (key,obj);
	}
	
	public static string GetCache (String key) {return
		 cache.get (key);
	}
	
	public static  void RemoveCache (String key) {
		cache.remove (key);
	}
	
	public static void Main (String args[]) {
		RemoveCache ("the");
		String name = GetCache ("a");
		System.out.println ("name=" +name);
		Setcache ("China", "Chinese", 60*60*24*30);
		Name = GetCache ("a");
		System.out.println ("Second time value name=" +name);
		RemoveCache ("the");
		Name = GetCache ("a");
		System.out.println ("Third value name=" +name);
	}



Run Result:

Name=null
Second time value name= China
Third time value Name=null


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.