.net下使用memcached分布式缓存数据

Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。

本文主要是在vs2010下,使用Memcached Providers 1.2 .NET 3.5操作memcached缓存数据。按照如下步骤进行:

1.添加引用

2.web.config配置文件

configuration下首个节点(必须是首个节点不然会报错,配置过configSections节点的人应该知道)

<configSections>
    <section name="cacheProvider" type="MemcachedProviders.Cache.CacheProviderSection, MemcachedProviders"
        allowDefinition="MachineToApplication" restartOnExternalChanges="true"/>
    <sectionGroup name="enyim.com">
      <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" />
    </sectionGroup>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>
 <enyim.com>
    <memcached>
      <servers>
        <!-- 服务器地址及端口号-->
        <add address="127.0.0.1" port="11211" />
      </servers>
      <socketPool minPoolSize="10" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00" />
    </memcached>
  </enyim.com>
  <cacheProvider defaultProvider="MemcachedCacheProvider">
    <providers>
      <add name="MemcachedCacheProvider"
           type="MemcachedProviders.Cache.MemcachedCacheProvider, MemcachedProviders"
           keySuffix="_MySuffix_" defaultExpireTime="2000"/>
    </providers>
  </cacheProvider>
2.添加引用

using MemcachedProviders.Cache;
using Enyim.Caching;
3.简单操作
             MemcachedClient client = new MemcachedClient("enyim.com/memcached");
            client.Store(Enyim.Caching.Memcached.StoreMode.Set,"test1","hello你好");//存储方式1
            var a= client.Get("test1");//获取值方式1
            string aa = null;
           bool isOk= DistCache.Add("aa", null);//为null不能存储,存储会失败
            aa = "bb";
            aa = DistCache.Get<string>("aa");//获取存储值方式2
            bool storeBool = false;
            isOk = DistCache.Add("aa",storeBool);//存储值方式2
            storeBool = DistCache.Get<bool>("aa");//获取值方式2
            string[] arry = {"aa","bb" };
            isOk = DistCache.Add("arry",arry);//存储数组
            string[] arry2 = DistCache.Get<string[]>("arry");//获取数组值

4.针对对象操作

新建一个类

    [Serializable()]//c#序列化,必须要的,不然会存储失败
    public class Product
    {

        public int ProductId { get; set; }

        public string ProductName { get; set; }

        public string ProductCompany { get; set; }

        public DateTime SignDate { get; set; }

        public DateTime UpdateData { get; set; }

    }
往memcached添加Product对象

 public IList<Product> GetAllPersonByEntity()
        {
            MemcachedClient client = new MemcachedClient("enyim.com/memcached"); 
            IList<Product> list = DistCache.Get<IList<Product>>("productPersonList");
            if (list==null)
            {
                list = new IList<Product>();
                 Product pr1 = new Product();
                pr1.ProductCompany = "A公司";
                pr1.ProductName = "A产品";
                pr1.SignDate = DateTime.Now;
                pr1.UpdateData = DateTime.Now;
                list.Add(pr1);//可以添加多个值
 bool isOk = DistCache.Add("productPersonList", list);//往缓存中添加product对象
            }
            return list;
        }

以上就是在.net下对memcached进行的简单操作。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值