Practical .NET Data Access Layer - 17

zhaozj2021-02-16  53

GetCache's code is very simple: there is a way to take it, it is impossible to fill it, "whether it has expired" is the only judgment condition for its effectiveness! Next, the author will conduct some exploration on this "expiration" problem, see what is going on.

OK, or first ask everyone to see the code:

Code 15: Expired Cache!

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, does not make sure Thread-Safe, 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;

}

}

Dear, from the above code, did you see some ends?

Since Web Appliction Cache (by httpcontext.current! = Null, whether it is Web Applicationj), it is very convenient to judge "whether expiration" is very convenient, and there is no Thread-Safe problem J. But this problem is not very wonderful for Windows Application. It is necessary to implement ISEXPIRED, but also worry that the problem of multithreaded and visiting visit is really hard to eat. "_Htwinappcache" (Custom Cache) in the above code, "Lock (_htwinappcache) (Make sure thread-way) is to cope with both very means taken in WINDOWS Application!

Maybe some friends will ask, WINDOWS Application also considers the cache management problem? My answer is: see the situation!

For ordinary client windows applications, it is true (please note: not) involving this topic, but for Server Application, for example: Remoting Server, Windows Service (WebServices is not here), it has prompted us to face "severe" Reality "L (How does .NET Framework not provide system.windows.caching namespace? Harmful we have to stove L)!

The above code is how to implement Cache Management support with the Web Application Corporates the WINDOWS Application! In the current version, the author implements "whether the expiration" under Windows Application is very simple: just look at it several times! And this number, of course, must be set in configuration information (please refer to the last configuration of this paragraph)!

Although the degree of Cache Management in Web Application is very high, but also "escape" configuration, and the processing after reading the configuration information will not let the rest of the shoulders of Parameter Classes (please refer to Cache Management above). "Schematic")!

Next paragraph: http://www.9cbs.net/develop/read_article.asp? Id = 27561

转载请注明原文地址:https://www.9cbs.com/read-24091.html

New Post(0)