贴个我写的.net里面cache的类的使用

DataCache.cs文件

 

代码
 1  using  System;
 2  using  System.Web;
 3 
 4  namespace  Common
 5  {
 6       ///   <summary>
 7       ///  缓存相关的操作类
 8       ///  zj
 9       ///  2006.4.1
10       ///   </summary>
11       public   class  DataCache
12      {
13           ///   <summary>
14           ///  获取当前应用程序指定CacheKey的Cache值
15           ///   </summary>
16           ///   <param name="CacheKey"></param>
17           ///   <returns></returns>
18           public   static   object  GetCache( string  CacheKey)
19          {
20              System.Web.Caching.Cache objCache  =  HttpRuntime.Cache;
21               return  objCache[CacheKey];
22          }
23 
24           ///   <summary>
25           ///  设置当前应用程序指定CacheKey的Cache值
26           ///   </summary>
27           ///   <param name="CacheKey"></param>
28           ///   <param name="objObject"></param>
29           public   static   void  SetCache( string  CacheKey,  object  objObject)
30          {
31              System.Web.Caching.Cache objCache  =  HttpRuntime.Cache;
32              objCache.Insert(CacheKey, objObject);
33          }
34 
35           ///   <summary>
36           ///  设置当前应用程序指定CacheKey的Cache值
37           ///   </summary>
38           ///   <param name="CacheKey"></param>
39           ///   <param name="objObject"></param>
40           public   static   void  SetCache( string  CacheKey,  object  objObject, DateTime absoluteExpiration,TimeSpan slidingExpiration )
41          {
42              System.Web.Caching.Cache objCache  =  HttpRuntime.Cache;
43              objCache.Insert(CacheKey, objObject, null ,absoluteExpiration,slidingExpiration);
44          }
45      }
46  }
47 

 

 

 

myweb.cs

 

代码
 1  using  System;
 2  using  System.Collections.Generic;
 3  using  System.Text;
 4 
 5  namespace  Common
 6  {
 7       public   static   class  MyWeb
 8      {       
 9           ///   <summary>
10           ///  缓存数据
11           ///   </summary>
12           ///   <param name="data"> 数据 </param>
13           ///   <param name="cachekey"> 缓存键值 </param>
14           ///   <param name="CacheMinutes"> 缓存时间,单位分 </param>
15           ///   <returns></returns>
16           public   static   object  GetByCache( object  data, string  cachekey, int  CacheMinutes)
17          {
18               object  objModel  =  Common.DataCache.GetCache(cachekey);
19               if  (objModel  ==   null )
20              {
21                   try
22                  {
23                      objModel  =  data;
24                       if  (objModel  !=   null )
25                      {
26                          Common.DataCache.SetCache(cachekey, objModel, DateTime.Now.AddMinutes(CacheMinutes), TimeSpan.Zero);
27                      }
28                  }
29                   catch  { }
30              }
31               return  objModel;
32          }
33 
34      }
35  }
36 

 

 

实际应用:

下面是个读取一条新闻:

 

代码
  #region  读取一条记录
        
///   <summary>
        
///  得到一个对象实体
        
///   </summary>
         public  LcShop.Shell.Entities.News GetModel( int  newsId)
        {

            StringBuilder strSql 
=   new  StringBuilder();
            strSql.Append(
" select  top 1 newsId,title,sortId,coverImg,contents,isshow,CurTime,px,SiteID from News  " );
            strSql.Append(
"  where newsId=@newsId  " );
            SqlParameter[] parameters 
=  {
                    
new  SqlParameter( " @newsId " , SqlDbType.Int, 4 )};
            parameters[
0 ].Value  =  newsId;

            LcShop.Shell.Entities.News model 
=   new  LcShop.Shell.Entities.News();
            SqlDataReader dr 
=  SqlHelper.ExecuteReader(SqlHelper.WebConnectionString, CommandType.Text, strSql.ToString(), parameters);
            
if (dr.Read())
            {
                
if  (dr[ " newsId " ].ToString()  !=   "" )
                {
                    model.newsId 
=   int .Parse(dr[ " newsId " ].ToString());
                }
                model.title 
=  dr[ " title " ].ToString();
                
if  (dr[ " sortId " ].ToString()  !=   "" )
                {
                    model.sortId 
=   int .Parse(dr[ " sortId " ].ToString());
                }
                
if  (dr[ " SiteID " ].ToString()  !=   "" )
                {
                    model.SiteID 
=   int .Parse(dr[ " SiteID " ].ToString());
                }
                model.CoverImg 
=  dr[ " CoverImg " ].ToString();
                model.contents 
=  dr[ " contents " ].ToString();
                
if  (dr[ " isshow " ].ToString()  !=   "" )
                {
                    
if  ((dr[ " isshow " ].ToString()  ==   " 1 " ||  (dr[ " isshow " ].ToString().ToLower()  ==   " true " ))
                    {
                        model.isshow 
=   true ;
                    }
                    
else
                    {
                        model.isshow 
=   false ;
                    }
                }
                
if  (dr[ " CurTime " ].ToString()  !=   "" )
                {
                    model.cur_time 
=  DateTime.Parse(dr[ " CurTime " ].ToString());
                }
                
if  (dr[ " px " ].ToString()  !=   "" )
                {
                    model.px 
=   int .Parse(dr[ " px " ].ToString());
                }
                dr.Dispose();
                
return  model;
            }
            
else
            {
                dr.Dispose();
                
return   null ;
            }
        }
        
#endregion

 

 

 

 

我个人认为,这样封装的话.如果要对数据用缓存很简单就构造了一个缓存取数据的方法.

你可能感兴趣的:(cache)