asp.net缓存、企业程序库缓存及格式化日期

格式化日期
<%#   string.Format("{0:yyyy-MM-dd}",DataBinder.Eval(Container.DataItem,"RoadGoodDate")   %>
<%#   DataBinder.Eval(Container.DataItem,"RoadGoodDate","{0:yyyy-MM-dd}")   %>

asp.net有页面缓存与API缓存及每请求缓存

页面缓存使用

<%@ OutputCache Duration="60" VaryByParam="*" %> 

API缓存
使用HttpRuntime.Cache.insert()    不返回值
HttpRuntime.Cache.Add()    返回cache引用,类型object
Call the Insert method, passing it an instance of the CacheDependency object 

The following code example adds an item named CacheItem3 that 
is dependent on another item in the cache named CacheItem2:

C#  Copy Code 
string[] dependencies = "CacheItem2" };
Cache.Insert(
"CacheItem3""Cached Item 3",
    
new System.Web.Caching.CacheDependency(null, dependencies));
 
The following code example shows an item named CacheItem4 added to the cache and having a file dependency 
set on the file named XMLFile.xml:

C#  Copy Code 
Cache.Insert(
"CacheItem4""Cached Item 4",
    
new System.Web.Caching.CacheDependency(
    Server.MapPath(
"XMLFile.xml")));
 
The following code example shows how to create multiple dependencies. It adds a key dependency on another item 
in the cache named CacheItem1 and a file dependency on the file named XMLFile.xml.

C#  Copy Code 
System.Web.Caching.CacheDependency dep1 
= 
    
new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml"));
string[] keyDependencies2 = "CacheItem1" };
System.Web.Caching.CacheDependency dep2 
= 
    
new System.Web.Caching.CacheDependency(null, keyDependencies2);
System.Web.Caching.AggregateCacheDependency aggDep 
= 
    
new System.Web.Caching.AggregateCacheDependency();
aggDep.Add(dep1);
aggDep.Add(dep2);
Cache.Insert(
"CacheItem5""Cached Item 5", aggDep);
 
Call the Insert method, passing it an absolute or sliding expiration time. 

The following code example adds an item to the cache with an absolute expiration of one minute:

C#  Copy Code 
Cache.Insert(
"CacheItem6""Cached Item 6",
    
null, DateTime.Now.AddMinutes(1d), 
    System.Web.Caching.Cache.NoSlidingExpiration);
 
Call the Insert method, specifying a value from the CacheItemPriority enumeration. 

The following code example adds an item to the cache with a priority value of High:

C#  Copy Code 
Cache.Insert(
"CacheItem8""Cached Item 8",
    
null, System.Web.Caching.Cache.NoAbsoluteExpiration,
    System.Web.Caching.Cache.NoSlidingExpiration,
    System.Web.Caching.CacheItemPriority.High, 
null);
 
Call the Add method, which returns an 
object representing the item. 

The following code example adds an item to the cache named CacheItem9 and sets the value of the variable CachedItem9 to be the item that was added.

C#  Copy Code 
string CachedItem9 = (string)Cache.Add("CacheItem9",
    
"Cached Item 9"null,
    System.Web.Caching.Cache.NoAbsoluteExpiration,
    System.Web.Caching.Cache.NoSlidingExpiration, 
    System.Web.Caching.CacheItemPriority.Default,
    
null);
 


每请求缓存则意味着只将数据缓存为该请求的持续时间。
使用HttpContext.Items["key"]=value   
items是一个属性,返回Idictionary类型,idictionary每一项为System.Collections.DictionaryEntry实体。


企业程序库:

配置web.config文件:
 

<cachingConfiguration defaultCacheManager="Default Cache Manager">
    
<backingStores>
      
<add name="inMemory" 
    type
="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching" />
    
</backingStores>

    
<cacheManagers>
      
<add name="Default Cache Manager"     
    expirationPollFrequencyInSeconds
="60"
    maximumElementsInCacheBeforeScavenging
="1000" 
    numberToRemoveWhenScavenging
="10"
    backingStoreName
="inMemory" />
      
<add name="Loading Scenario Cache Manager" 
    expirationPollFrequencyInSeconds
="60" 
    maximumElementsInCacheBeforeScavenging
="1000" 
    numberToRemoveWhenScavenging
="10"
    backingStoreName
="inMemory" />
    
</cacheManagers>
  
</cachingConfiguration>


添加dll引用,添加名字空间using Microsoft.Practices.EnterpriseLibrary.Caching;
使用如下代码:

            CacheManager cm = CacheFactory.GetCacheManager();
            QKnowledge qk 
= new QKnowledge(1"hi,catch");
            cm.Add(
"qk1", qk);
            QKnowledge qk1 
= (QKnowledge)cm.GetData("qk1");
            cm.Remove(
"qk1");






优化程序速度
http://www.microsoft.com/china/msdn/library/webservices/asp.net/us0501ASPNETPerformance.mspx?mfr=true
      

posted on 2006-04-07 13:21  Sunny  阅读(693)  评论(0编辑  收藏  举报