分布式缓存系统Memcached在Asp.net下的应用

Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来降低读取数据库的次数。从而提高动态、数据库驱动站点的速度。

Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的。可是client能够用不论什么语言来编写。并通过memcached协议与守护进程通信。

站下的session性能并不高,所以造成人们一种印象,大型WEB项目使用JAVA的错觉,致使非常多人吐槽微软不给力,事实上这好比拉不出怪地球引力,本文介绍Memcached在ASP.net Web项目中的应用,智联招聘,招商银行,农业银行等都是採用解决方式,在性能上是绝对不亚于不论什么大型站点.同一时候Memcached还能非常方便建立起server集群,对于大型解决方式,server集群的重要性不言而喻;

1.准备工作.
要在项目中使用到Memcached,须要准备好例如以下条件:
server环境:安装Memcached服务到server上
a.下载Memcached安装文件
b.以管理员身份执行CMD 在下载的Memcached服务安装路径下安装Memcached服务(命令行:X:memcached.exe -d install)
C.检查服务安装

d.启动服务 命令行 memcached.exe –d start  当然能够直接在计算机服务管理来操作
到这里Memcached服务就搭建完毕了,那么怎样运用到.NET项目中区呢?
2.下载.NET Memcached lbr
从文件..trunkclientlibsrcclientlibin2.0Debug下拷贝出4个DLL文件Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll,加入引用到项目中去
3.初始化Memcached


从“服务”中能够看到Memcached Server处于“正在执行”的状态中。假设是停止的,右键“启动服务”就能够了,当然能够memcached.exe –d start来启动




基本的缓存代码:

using System;
using System.Data;
using System.Web;
using Memcached.ClientLibrary;

public class Cache_Info
{
    private readonly static string CacheKey = "Info_key";

    /// <summary>
    /// 缓存是否存在
    /// </summary>
    /// <param name="pMC"></param>
    /// <param name="pKey"></param>
    /// <returns></returns>
    private static bool IsCache(MemcachedClient pMC,string pKey) 
    {
        if (pMC.KeyExists(pKey))
        {
            return true;
        }
        else 
        {
            return false;
        }
    }

    /// <summary>
    /// 覆盖缓存
    /// </summary>
    /// <param name="pKey"></param>
    /// <param name="pObject"></param>
    /// <returns></returns>
    private static bool StoreCache(string pKey,object pObject) 
    {
        MemcachedClient mc = new MemcachedClient();
        mc.EnableCompression = true;
        bool _result = false;
        if (IsCache(mc, pKey))
        {
            if (mc.Get(pKey) == null)
            {
                mc.Set(pKey, pObject);//缓存存在,强行覆盖
            }
            else 
            {
                mc.Replace(pKey, pObject);//缓存存在。强行覆盖
            }
            _result = true;
        }
        else 
        {
            mc.Add(pKey, pObject);//第一次载入缓存
            _result = true;
        }
        return _result;
    }

    /// <summary>
    /// 清除缓存
    /// </summary>
    /// <param name="pKey"></param>
    /// <returns></returns>
    public static bool RemoveCache(string pKey) 
    {
        MemcachedClient mc = new MemcachedClient();
        mc.EnableCompression = true;
        return mc.Delete(pKey);
    }

    /// <summary>
    /// 获取数据
    /// </summary>
    /// <returns></returns>
    public static DataTable GetInfo()
    {
        #region 通过缓存来获取DataTable的数据
        MemcachedClient mc = new MemcachedClient();
        mc.EnableCompression = true;
        if (mc.Get(CacheKey) != null)
        {
            return mc.Get(CacheKey) as DataTable; //直接从缓存取数据
        }
        else
        {
            DataTable dt=DB_Info.GetInfo();  //第一次载入将数据存入缓存中
            if (StoreCache(CacheKey, dt))
            {
                return mc.Get(CacheKey) as DataTable;
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region 直接从数据库获取DataTable
        //return DB_Info.GetInfo();
        #endregion

    }
          
}

代码下载地址:

Memcached

原文地址:https://www.cnblogs.com/brucemengbm/p/6706382.html