Combat. Net data Access Layer-17

Source: Internet
Author: User
Tags bool httpcontext thread
Access to the code of the data GetCache is simple: there are, the absence of the fill, "whether expired" is the only criterion of its effectiveness! Next, the author explores the issue of whether or not it is overdue to see what is going on.



Ok, or first please read the paragraph code:



Code 15: Invalid expiration of the cache article!

public class CacheManager

{

private bool Iscacheexpired (string key)

{

BOOL bexpired = false;



if (httpcontext.current!= null)

{

Web cache automatically supports Thread-safe without locking resources

if (httpcontext.current.cache[key] = = null)

Bexpired = true;

}

Else

{

Windows cache is implemented by itself and does not ensure thread-safe and must lock resources

Lock (_htwinappcache)

{

if (_htwinappcache[key] = = null)

Bexpired = true;

Else

{

Winappcache cache = (Winappcache)

_htwinappcache[key];



if (cache. IsExpired ())

{

cache = NULL;

_htwinappcache[key] = null;

Bexpired = true;

}

}

}

}



return bexpired;

}

}

























































































Do you see some clues from the code above?



Because the web appliction Cache (httpcontext.current!= NULL to determine whether the Web Applicationj) is directly supported by the. NET framework, it is convenient to judge whether or not to expire. There is no thread-safe problem, J. But this problem for Windows application is not very wonderful, both the realization of their own isexpired, but also worry about multithreading concurrent access to all kinds of problems, is really thankless drudgery ah l! The "_htwinappcache" (custom cache) and "Lock (_htwinappcache)" (Ensure Thread-safe) in the above code are two very important ways to cope with Windows application!



Perhaps a friend will ask, Windows application also want to consider the cache management problem? My answer is: look at the situation and decide!

For ordinary client Windows application, it is very rare (please note: not none) that covers this topic, but for the Server application, for example: Remoting server,windows Service (not webservices), all prompted us to face the "grim reality" L (the. NET framework does not provide System.Windows.Caching namespaces?) We had to make a good, l!



The above code is to consider the Web application and Windows application coexistence, how do we achieve cache management support!



In the current release, the author realizes "expiration" under Windows application is very simple: to see how many times it has been visited! And this number, of course, must be set in the configuration information (please refer to the end of this paragraph configuration sample)!

The cache management in the Web application is very high, but also "escape" configuration one, and read the configuration information after the processing work will fall to the parameter classes shoulder (please refer to the cache above Management "structure diagram")!



Next paragraph: http://www.csdn.net/develop/Read_Article.asp?id=27561


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.