About how WnForm/Web uses cache Cach

Source: Internet
Author: User

Reference http://kb.cnblogs.com/page/69483/

Http://www.cnblogs.com/zgx/archive/2009/03/16/1413643.html

Http://www.cnblogs.com/zhangxp1129/archive/2012/09/05/2671522.html

 

 

MSDN: http://msdn.microsoft.com/zh-cn/magazine/system.web.caching.cache (VS.85). aspx

An instance of this class is created for each application domain, and the Instance remains valid as long as the corresponding application domain remains active.

Note:

CacheClass cannot be used outside ASP. NET applications. It is designed and tested to provide caching for Web applications in ASP. NET. ASP. NET cache may not work properly in other types of applications (such as console applications or Windows Forms applications.

 

System. Web. Caching is the namespace used to manage the cache, and its parent space is System. Web. Therefore, cache is usually used for Web website development, including development in B/S projects.

The cache design mainly takes into account that the network bandwidth may delay data submission and re-sending. If the data is stored on the client, the user can directly read the data from the client, reduces data interaction between the client and the server, and improves program performance.

 

 

Can System. Web. Caching be used in WinForm programs?

If you use winform, you don't need to think about it because your program runs in the memory. Winfrom directly uses the memory data dictionary.

For web applications, cache stores common data in the server's memory. When different customers request the same data, they directly read the data from the memory to improve performance.

 

Simple: WebForm is a "thin client" that occupies server resources. WinForm is a "Fat customer ticket" and occupies local client memory.

We recommend two writing methods:

 

First, how to use it in a web project.

View Code /************************************* *
* Role: cache-related operations
* Author: * Date: 2008-02-11
* URL:
**************************************/
Using System;
Using System. Web;
Using System. Collections. Generic;
Using System. Text;

Namespace RedGlove. Lib
{
Public class DataCache
{
/// <Summary>
/// Obtain the Cache value of the specified CacheKey of the current application
/// </Summary>
/// <Param name = "CacheKey"> </param>
/// <Returns> </returns>
Public static object GetCache (string CacheKey)
{
System. Web. Caching. Cache objCache = HttpRuntime. Cache;
Return objCache [CacheKey];

}

/// <Summary>
/// Set the Cache value of the specified CacheKey for the current application
/// </Summary>
/// <Param name = "CacheKey"> </param>
/// <Param name = "objObject"> </param>
Public static void SetCache (string CacheKey, object objObject)
{
System. Web. Caching. Cache objCache = HttpRuntime. Cache;
ObjCache. Insert (CacheKey, objObject );
}

/// <Summary>
/// Set the Cache value of the specified CacheKey for the current application
/// </Summary>
/// <Param name = "CacheKey"> </param>
/// <Param name = "objObject"> </param>
Public static void SetCache (string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
System. Web. Caching. Cache objCache = HttpRuntime. Cache;
ObjCache. Insert (CacheKey, objObject, null, absoluteExpiration, slidingExpiration );
}
}
}

 

Second, in the winform Program

 

View Code namespace HZ
{
Using System. Collections. Generic;

/// <Summary>
/// Globally unified cache class
/// </Summary>
Public class Cache
{
Private SortedDictionary <string, string> dic = new SortedDictionary <string, string> ();
Private static volatile Cache instance = null;
Private static object lockHelper = new object ();

Private Cache ()
{

}
Public void Add (string key, string value)
{
Dic. Add (key, value );
}
Public void Remove (string key)
{
Dic. Remove (key );
}

Public string this [string index]
{
Get
{
If (dic. ContainsKey (index ))
Return dic [index];
Else
Return null;
}
Set {dic [index] = value ;}
}

Public static Cache Instance
{
Get
{
If (instance = null)
{
Lock (lockHelper)
{
If (instance = null)
{
Instance = new Cache ();
}
}
}
Return instance;
}
}
}
}

 

 

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.