龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

使用异步缓存来保证高流量网页及时更新与低错误率(2)

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
#region 线程处理模块 public static void UpdateCache(object obj) { CacheItem thisCache = (CacheItem)obj; writelog("线程:" + thisCache.CacheName + "--启动于:" + DateTime.Now.ToString() +

    #region 线程处理模块  
 
    public static void UpdateCache(object obj)  
    {  
        CacheItem thisCache = (CacheItem)obj;  
        writelog("线程:" + thisCache.CacheName + "--启动于:" + DateTime.Now.ToString() + "\r\n", "exceptions");  
        while (true)  
        {  
            try 
            {  
                saveDataCache(thisCache);  
            }  
            catch 
            {  
 
            }  
            Thread.Sleep(thisCache.memoryTime * 1000);//内存20秒更新  
        }  
    }  
 
    public static void UpdateFile(object obj)  
    {  
        CacheItem thisCache = (CacheItem)obj;  
        while (true)  
        {  
            try 
            {  
                saveDataFile(thisCache);  
            }  
            catch 
            {  
 
            }  
            Thread.Sleep(thisCache.fileTime * 1000);//文件2分钟缓存  
        }  
    }  
 
    public static Thread getThread(string threadName)  
    {  
        Thread tmpThread = null;  
        lockx.EnterReadLock();  
        try 
        {  
            if (threadTable.Keys.Contains(threadName))  
            {  
                tmpThread = threadTable[threadName];  
            }  
        }  
        catch 
        {  
        }  
        finally 
        {  
            lockx.ExitReadLock();  
        }  
        return tmpThread;  
    }  
 
    /// <summary>  
    /// 添加缓存  
    /// </summary>  
    /// <param name="item">缓存对象</param>  
    public static void saveCache(CacheItem item)  
    {  
        string mCacheName = item.CacheName + "_memory";  
        string fCacheName = item.CacheName + "_file";  
        Thread memoryCache = getThread(mCacheName);  
        Thread fileCache = getThread(fCacheName);  
        if (memoryCache == null)  
        {  
            memoryCache = new Thread(new ParameterizedThreadStart(UpdateCache));  
            memoryCache.IsBackground = true;  
            memoryCache.Start(item);  
        }  
        if (fileCache == null)  
        {  
            fileCache = new Thread(new ParameterizedThreadStart(UpdateFile));  
            fileCache.IsBackground = true;  
            fileCache.Start(item);  
        }  
        lockx.EnterWriteLock();  
        try 
        {  
            threadTable[mCacheName] = memoryCache;  
            threadTable[fCacheName] = fileCache;  
        }  
        catch { }  
        finally { lockx.ExitWriteLock(); }  
    }  
 
    public static string readCacheContent(CacheItem item)  
    {  
        //Stopwatch watch = new Stopwatch();  
        //watch.Start();  
        string content = readCache(item.CacheName);  
        if (content==null || content.Length < 0)  
        {  
            content = readFile(item.CacheName);  
            saveCache(item);  
        }  
        //读取缓存失败  
        if (content != null && content.Length <= 0)  
        {  
            //throw (new Exception("读取内容失败:" + item.CacheName));  
            content = item.doProcess();  
            //saveCache(item);  
        }  
        return content;  
        //watch.Stop();  
        //content += ("\r\n<!--方法执行时间:" + watch.ElapsedMilliseconds + "-->\r\n");  
    } 
    #endregion 
 
    #region 公共处理  
    public static string saveCache(string cacheName, ProcessDataDelegate mainMathod, object[] args)  
    {  
        using (CacheItem tmpCache = new CacheItem())  
        {  
            tmpCache.CacheName = cacheName;  
            tmpCache.CacheDelegate = mainMathod;  
            tmpCache.CacheArgs = args;  
            string content = "";  
            content = readCacheContent(tmpCache);  
            return content;  
        }  
    } 
    #endregion  


精彩图集

赞助商链接