高级应用

来源:互联网 发布:mac 工作日程安排软件 编辑:程序博客网 时间:2024/05/17 00:07
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

一、连接池

    1、特点:

①、优点:性能

②、缺点:可能存在多个没有被使用的连接,资源浪费

2、连接池

①包含在ADO中的每个.NET数据提供程序都可实现连接池。

②每个连接池都与一个独立的连接字符串及其事务上下文关联。每次打开一个新的连接,数据提供者会尝试将指定的连接字符串与连接池的字符串进行匹配。如果失败,则创建新连接并加入连接池。

③连接池创建之后,系统会创建一些连接对象并将它们加入连接池,直至达到额定的最小连接对象数量。以后根据需要创建新的连接,直到达到最大连接数量。

④.NET默认是使用连接池。如果想禁用,则可以使用以下方式:

Ⅰ、使用SqlConnection对象时,在连接字符串加入:Pooling=false

Ⅱ、使用OleDBConnection对象时,在连接字符串加入:OLEDBServices=-4

    3、提示和技巧

①打开连接应迟,关闭连接应早

②在关闭数据库连接前确保关闭了所有用户定义的事务

③至少保证连接池中有一个连接可用

二、缓存

    1、特点

①、优点:提高性能,稳定性,可用性

②、缓存

Ⅰ、在.NET中,提供了专门用于缓存数据的Cache对象,它的应用范围是应用程序域。生存期是和应用程序紧密相关的,每当应用程序启动的时候就重新创建Cache对象,每当应用程序启动的时候就重新创建Cache对象。它与Application对象的主要区别就是提供了专门用于缓存管理的性能,比如依赖和过期策略。

Ⅱ、Cache对象定义在System.Web.Caching命名空间,可以使用HttpContext类的Cache属性或Page对象的Cache属性来得到Cache的引用,Cache对象除了存储键值以外,还可以存储.NET框架的对象。

2、依赖和过期策略

①文件策略:当硬盘上的某个(某些)文件更改时,强制移除缓存数据

   CacheDependencycDependency=newCacheDependency(Server.MapPath(“authors.xml”));

   Cache.Insert(“CachedItem”,item,cDependency);

②键值依赖:指定缓存中的某个数据项更改时移除。

   //Createacacheentry

   Cache[“key1”]=“Value1”;

   //Makekey2dependentonkey1

   String[]dependencyKey=newString[1];

   dependencyKey[0]=“Key1”;

   CacheDependencydependency=newCacheDependency(null,dependencyKey);

   Cache.Insert(“key2”,“Value2”,dependency);

③基于时间的过期策略:绝对和相对

   //Absoluteexpiration

   Cache.Insert(“CachedItem”,item,null,DateTime.Now,AddSeconds(5),Cache.NoSlidingExpiration);

   //Slidingexpiration

   Cache.Insert(“”,item,null,Cache.NoAbsoluteExpiration,TimeSpan.FromSeconds(5));

④数据库依赖(建议不要使用):数据库中相关的数据发生变化,则缓存失效

   3、使用缓存:由于数据会过期,使用缓存时必须检查数据的有效性

       stringdata=(string)Cache[“MyItem”];    

       if(data==null)

       {

           data=GetData();

           Cache.Insert(“MyItem”,data);

       }

   4、缓存回调:当缓存失效时,自动调用

       CacheItemRemoveCallbackonRemove=newCacheItemRemovedCallack(this.RemoveCallback);1<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击