博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

winfrom中使用cache

Posted on 2009-07-22 22:33  codingsilence  阅读(160)  评论(0编辑  收藏  举报

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Threading;


namespace NPao.Tools.EntityDesign
{
    
public class CacheConfig
    
{
        
private static HttpRuntime _httpRuntime;

        
public static Cache Cache
        
{
            
get
            
{
                EnsureHttpRuntime();
                
return HttpRuntime.Cache;
            }

        }


        
private static void EnsureHttpRuntime()
        
{
            
if (null == _httpRuntime)
            
{
                
try
                
{
                    Monitor.Enter(
typeof(CacheConfig));
                    
if (null == _httpRuntime)
                    
{
                       
// Create an Http Content to give us access to the cache.
                        _httpRuntime = new HttpRuntime();
                   }

                }

                
finally
                
{
                    Monitor.Exit(
typeof(CacheConfig));
                }

            }

        }


    }

}


使用:
string s = CacheConfig.Cache["txt"] as string;
            if (!string.IsNullOrEmpty(s))
                MessageBox.Show(s);
            else
                CacheConfig.Cache.Insert("txt",txtConnStr.Text);