[ArticleAuthor: Sun Li link: http://www.cnblogs.com/sunli/ updated by: 2010-07-13]
General Cache Usage
When we use cache in applications, it is likely to use the followingCodeIndicates the logic. Obtain the cached data first.
Query the database or obtain data in other ways, and store the data in the cache to return data.
Code
// The Code cannot be executed directly, but it is used to express the logic.
Data = Cache. Get (key );
If (Data = Null | ! Isvalid (data )){
SQL = " Select ...... " ;
Data = DB. Query (SQL ); // Data may be null
If (Data ){
Cache. Set (Key, Data, expire );
}
}
Return Data;
The above code runs very well in many cases, which is also used by many people.
Problem
If the key content does not exist in the database, the data in the code above is always null and there is no data in the cache, if the request for this key suddenly becomes very large (in many cases, for example, a wrong link request does not have data), a large number of requests will bypass the cache, direct access to the back-end database will cause a lot of pressure, and it is difficult to analyze the cause of the problem, because you think you have added the cache.
If the data volume is large and the access volume is large, the system may crash.
Solution
The solution to this problem is to cache null when the database queries null.
For example, if the database returns a list, we can store an empty list to process the cache. set (key, new list (), expire); of course, depending on the situation, you can also use other methods.