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

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

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
using System; using System.Data; using System.Configuration; using System.Linq; using System.Xml.Linq; using System.IO; using System.Collections.Generic; using System.Threading; using System.Diagnosti

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Xml.Linq;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using System.Diagnostics;

/// <summary>
/// 本类主要负责处理根把相关参数设置缓存结果
/// </summary>
public class CacheSvr
{
    public delegate string ProcessDataDelegate(params object[] args);
    public static string cacheDir = "D:\tmpFile";
    public static Dictionary<string, string> cacheTable = new Dictionary<string, string>();
    public static ReaderWriterLockSlim lockx = new ReaderWriterLockSlim();
    public static Dictionary<string, Thread> threadTable = new Dictionary<string, Thread>();

    public CacheSvr()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    #region 文件缓存逻辑
    /// <summary>
    /// 保存程序执行结果致文件
    /// </summary>
    /// <param name="thisDelegate">所要执行的方法</param>
    /// <param name="fileName">文件名</param>
    /// <param name="args">参数列表</param>
    /// <returns></returns>
    public static void saveDataFile(CacheItem item)
    {
        string result = item.doProcess();
        if (result.Length > 0)
        {
            write(result, item.CacheName);
        }
    }

    /// <summary>
    /// 写文件代码
    /// </summary>
    /// <param name="text">写入内容</param>
    /// <param name="fileName">文件名</param>
    public static void write(String text, string fileName)
    {
        try
        {
            String path = cacheDir;
            path = path + fileName + ".cache";
            Stream fsc = File.Create(path);
            fsc.Close();
            Stream fs = File.Open(path, FileMode.Open, FileAccess.Write);
            TextWriter tw = new StreamWriter(fs, System.Text.Encoding.GetEncoding(936));
            tw.Write(text);
            tw.Close();
            fs.Close();
        }
        catch
        {

        }
    }

    public static string readFile(string fileName)
    {
        string content = "";
        try
        {
            //Stopwatch watch = new Stopwatch();
            //watch.Start();
            String path = cacheDir;
            path = path + fileName + ".cache";
            Stream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            StreamReader tr = new StreamReader(fs, System.Text.Encoding.GetEncoding(936));
            content = tr.ReadToEnd();
            //content += "\r\n <!--read from file " + path + "--> \r\n";
            tr.Close();
            fs.Close();
            //watch.Stop();
            //content = content + "<!--执行时间为:" + watch.ElapsedMilliseconds + "-->\r\n";
        }
        catch
        {
            content = "";
        }
        return content;
    }
    #endregion

    #region 内存缓存模块
    /// <summary>
    /// 内存缓存
    /// </summary>
    /// <param name="thisDelegate">所需执行的方法</param>
    /// <param name="cacheName">内存中的名字</param>
    /// <param name="args">参数列表</param>
    /// <returns></returns>
    public static void saveDataCache(CacheItem item)
    {
        string result = item.doProcess();
        if (result.Length > 0)
        {
            cache(result, item.CacheName);
        }
    }

    /// <summary>
    /// 向内存中写入内容
    /// </summary>
    /// <param name="text"></param>
    /// <param name="cacheName"></param>
    public static void cache(string text, string cacheName)
    {
        lockx.EnterWriteLock();
        try
        {
            //text += "\r\n<!--缓存更新,时间为:" + DateTime.Now.ToString() + "-->\r\n";
            cacheTable[cacheName] = text;
        }
        catch { }
        finally { lockx.ExitWriteLock(); }
    }

    public static void writelog(String text, string fileName)
    {
        try
        {
            String path = cacheDir;
            path = path + fileName + ".cache";
            if (!File.Exists(path))
            {
                Stream fsc = File.Create(path);
                fsc.Close();
            }
            Stream fs = File.Open(path, FileMode.Append, FileAccess.Write);
            TextWriter tw = new StreamWriter(fs, System.Text.Encoding.GetEncoding(936));
            tw.Write(text);
            tw.Close();
            fs.Close();
        }
        catch
        {

        }
    }

    public static string readCache(string cacheName)
    {
        string content = "";
        lockx.EnterReadLock();
        try
        {
            bool result = cacheTable.TryGetValue(cacheName, out content);
            //if (result)
            //{
            //    content += "\r\n <!--read from memory--> \r\n";
            //}
            return content;
        }
        catch
        {
            return "";
        }
        finally
        {
            lockx.ExitReadLock();
        }
    }
    #endregion

    #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;
    }


精彩图集

赞助商链接