转自:http://blog.csdn.net/devgis/article/details/8212917

缘起: 在数据驱动的web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载。缓存是解决这个问题的好办法。但是ASP.NET中的虽然已经可以实现对页面局部进行缓存,但还是不够灵活。此时Memcached或许是你想要的。

Memcached是什么?
Memcached是由Danga Interactive开发的,高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。

Memcached能缓存什么?
通过在内存里维护一个统一的巨大的hash表,Memcached能够用来存储各种格式的数据,包括图像、视频、文件以及数据库检索的结果等。

Memcached快么?

非 常快。Memcached使用了libevent(如果可以的话,在linux下使用epoll)来均衡任何数量的打开链接,使用非阻塞的网络I/O,对 内部对象实现引用计数(因此,针对多样的客户端,对象可以处在多样的状态), 使用自己的页块分配器和哈希表, 因此虚拟内存不会产生碎片并且虚拟内存分配的时间复杂度可以保证为O(1).。

Danga Interactive为提升Danga Interactive的速度研发了Memcached。目前,LiveJournal.com每天已经在向一百万用户提供多达两千万次的页面访问。而这 些,是由一个由web服务器和数据库服务器组成的集群完成的。Memcached几乎完全放弃了任何数据都从数据库读取的方式,同时,它还缩短了用户查看 页面的速度、更好的资源分配方式,以及Memcache失效时对数据库的访问速度。

Memcached的特点
Memcached的缓存是一种分布式的,可以让不同主机上的多个用户同时访问, 因此解决了共享内存只能单机应用的局限,更不会出现使用数据库做类似事情的时候,磁盘开销和阻塞的发生。

Memcached的使用 
一 
Memcached服务器端的安装 (此处将其作为系统服务安装)
下载文件:memcached 1.2.1 for Win32 binaries (Dec 23, 2006)
    1 解压缩文件到c:\memcached
   2 命令行输入 'c:\memcached\memcached.exe -d install' 
    3 命令行输入 'c:\memcached\memcached.exe -d start' ,该命令启动 Memcached ,默认监听端口为 11211
通过 memcached.exe -h 可以查看其帮助
二   .NET memcached client library
   下载文件:https://sourceforge.net/projects/memcacheddotnet/

里面有.net1.1 和 .net2.0的两种版本 还有一个不错的例子。

三 应用

1 将Commons.dll,ICSharpCode.SharpZipLib.dll,log4net.dll,Memcached.ClientLibrary.dll 等放到bin目录
2 引用Memcached.ClientLibrary.dll
3 代码

  后记: 是个不错的东西 ,使用起来也很方便,php ,ruby 的项目中用这个的很多,但是.net项目中用的较少(恕俺孤陋寡闻) 。希望有兴趣的朋友们 多多交流 。 看到页首了么各位兄弟? 不用我多说了吧,

namespace Memcached.MemcachedBench
 {
     using System;
     using System.Collections;
 
     using Memcached.ClientLibrary;
 
     public class MemcachedBench 
      {
          [STAThread]
         public static void Main(String[] args) 
          {
             string[] serverlist = { "10.0.0.131:11211", "10.0.0.132:11211" };
 
             //初始化池
              SockIOPool pool = SockIOPool.GetInstance();
              pool.SetServers(serverlist);
 
              pool.InitConnections = 3;
              pool.MinConnections = 3;
             pool.MaxConnections = 5;               pool.SocketConnectTimeout = 1000;
              pool.SocketTimeout = 3000;
 
              pool.MaintenanceSleep = 30;
              pool.Failover = true;
 
              pool.Nagle = false;
              pool.Initialize();
 
             // 获得客户端实例
              MemcachedClient mc = new MemcachedClient();
              mc.EnableCompression = false;
 
              Console.WriteLine("------------测   试-----------");
              mc.Set("test", "my value");  //存储数据到缓存服务器,这里将字符串"my value"缓存,key 是"test"
 
             if (mc.KeyExists("test"))   //测试缓存存在key为test的项目
              {
                  Console.WriteLine("test is Exists");
                  Console.WriteLine(mc.Get("test").ToString());  //在缓存中获取key为test的项目
              }
             else
              {
                  Console.WriteLine("test not Exists");
              }
 
              Console.ReadLine();
 
              mc.Delete("test");  //移除缓存中key为test的项目
 
             if (mc.KeyExists("test"))
              {
                  Console.WriteLine("test is Exists");
                  Console.WriteLine(mc.Get("test").ToString());
              }
             else
              {
                  Console.WriteLine("test not Exists");
              }
              Console.ReadLine();
             
              SockIOPool.GetInstance().Shutdown();  //关闭池, 关闭sockets
          }
      }
 } 1. 下载memcache(http://jehiah.cz/projects/memcached-win32)的windows稳定版http://jehiah.cz/projects/memcached-win32/files/memcached-1.2.1-win32.zip
2. 解压,把memcached.exe 放到c:\windows\system32目录下,然后打开命令行窗口输入下面命令
c:
cd \windows\system32
memcached.exe -d install
memcached.exe -d start        该命令启动 Memcached,默认监听端口为 11211
3. 下载     Memcached Client http://enyimmemcached.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=13095
配置config文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="enyim.com">
            <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" />
        </sectionGroup>
        <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching" />
    </configSections>
    <enyim.com>
        <memcached>
            <servers>
                <!-- put your own server(s) here-->
                <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>
    <memcached keyTransformer="Enyim.Caching.TigerHashTransformer, Enyim.Caching">
        <servers>
            <add address="127.0.0.1" port="11211" />
            
        </servers>
        <socketPool minPoolSize="2" maxPoolSize="100" connectionTimeout="00:00:10" deadTimeout="00:02:00" />
    </memcached>
</configuration>
这里的port:11211是, memcached-1.2.1-win32在安装时默认使用的port.当然你可以用memcached.exe -p 端口号来自行设置。
第二步, 新建TestMemcachedApp的console project
引用Enyim.Caching.dll
基础代码如下:
//create a instance of MemcachedClient
MemcachedClient mc = new MemcachedClient();
// store a string in the cache
mc.Store(StoreMode.Set, "MyKey", "Hello World");
// retrieve the item from the cache
Console.WriteLine(mc.Get("MyKey"));
完整代码如下,
using System;
using System.Collections.Generic;
using System.Text;
using Enyim.Caching;
using Enyim.Caching.Memcached;
using System.Net;
using Enyim.Caching.Configuration;
namespace DemoApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // create a MemcachedClient
            // in your application you can cache the client in a static variable or just recreate it every time
            MemcachedClient mc = new MemcachedClient();
            
            // store a string in the cache
            mc.Store(StoreMode.Set, "MyKey", "Hello World");
            // retrieve the item from the cache
            Console.WriteLine(mc.Get("MyKey"));
            // store some other items
            mc.Store(StoreMode.Set, "D1", 1234L);
            mc.Store(StoreMode.Set, "D2", DateTime.Now);
            mc.Store(StoreMode.Set, "D3", true);
            mc.Store(StoreMode.Set, "D4", new Product());
            mc.Store(StoreMode.Set, "D5", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });            
            Console.WriteLine("D1: {0}", mc.Get("D1"));
            Console.WriteLine("D2: {0}", mc.Get("D2"));
            Console.WriteLine("D3: {0}", mc.Get("D3"));
            Console.WriteLine("D4: {0}", mc.Get("D4"));
            byte[] tmp = mc.Get<byte[]>("D5");
            // delete them from the cache
            mc.Remove("D1");
            mc.Remove("D2");
            mc.Remove("D3");
            mc.Remove("D4");
            // add an item which is valid for 10 mins
            mc.Store(StoreMode.Set, "D4", new Product(), new TimeSpan(0, 10, 0));
            Console.ReadLine();
        }
        // objects must be serializable to be able to store them in the cache
        [Serializable]
        class Product
        {
            public double Price = 1.24;
            public string Name = "Mineral Water";
            public override string ToString()
            {
                return String.Format("Product {{{0}: {1}}}", this.Name, this.Price);
            }
        }
    }
}
下载memcached服务安装地址:http://www.danga.com/memcached/
Client API下载地址:http://www.danga.com/memcached/apis.bml

(转)C# 中使用分布式缓存系统Memcached的更多相关文章

  1. 分布式缓存系统 Memcached 整体架构

    分布式缓存系统 Memcached整体架构 Memcached经验分享[架构方向] Memcached 及 Redis 架构分析和比较

  2. 分布式缓存系统Memcached简介与实践

    缘起: 在数据驱动的web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是ASP.NET中的虽然已经可以实现对页面局部进行缓存,但还是不够灵 ...

  3. 分布式缓存系统Memcached简介与实践(.NET memcached client library)

    缘起: 在数据驱动的web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是ASP.NET中的虽然已经可以实现对页面局部进行缓存,但还是不够灵 ...

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

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

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

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

  6. 分布式缓存系统Memcached简介与以及在.net下的实践(转)

    缘起: 在数据驱动的web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是ASP.NET中的虽然已经可以实现对页面局部进行缓存,但还是不够灵 ...

  7. php分布式缓存系统 Memcached 入门

    Memcached 是一个分布式的缓存系统, 但是 Memcachd 到底是什么意思,有什么作用呢?缓存一般用来保存一些经常被存取的数据和资源(例如:浏览器会将访问过的网页会话缓存起来),因为通过缓存 ...

  8. 分布式缓存系统Memcached[分享]

    个人网站:http://www.51pansou.com memcached视频下载:memcached视频教程 memcached源码下载:memcached源码 Memcached是什么? Mem ...

  9. 分布式缓存系统 Memcached 快速入门

    Memcached介绍   官网地址      Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提 ...

随机推荐

  1. ef linq 中判断实体中是否包含某集合

    我有一个需求,问题有很多标签,在查询时,需要筛选包含查询标签的一个集合(List<int>),以前的做法是先查询出来符合查询标签条件的标签id的结果集A,再查询问题时,加上判断是否包含该标 ...

  2. Dottrace 10.0.2 使用心得

    开发环境vs2015 软件:JetBrains dotTrace 10.0.2 刚开始不知道怎么下手,多看了一会还有一位仁兄的解释.算是对某个功能小有入门了. 当前会查看某个方法在抓取快照时间它的执行 ...

  3. spring boot 项目属性配置

    一.系统配置文件 1.application.properties是新建springboot项目后默认得配置文件 配置的示例如下 server.port= server.context-path=/g ...

  4. Python实现的复杂的计算器的代码

    用Python实现复杂的计算器,可以按照“()”.乘除.加减的优先级进行混合运算.主旨是对正则表达式进行学习. 设计思路: 1.在计算式中搜索最后一个“(”,再搜索和它匹配的“)”,截取两个括号间的表 ...

  5. Mac网络命令 老命令重新学

    网络与通信操作 命令名 功能描述 使用举例 telnet 远程登录 telnet hpc.sp.net.edu.cn rlogin 远程登录 rlogin hostname -l username r ...

  6. mybatis 为什么要设置jdbcType

    转载自:http://makemyownlife.iteye.com/blog/1610021 前天遇到一个问题 异常显示如下: 引用 Exception in thread "main&q ...

  7. 导入mysql报错问题

    今天数据导入报错:Got a packet bigger than‘max_allowed_packet’bytes的问题 2个解决方法: 1.临时修改:mysql>set global max ...

  8. 过河卒(NOIP2002)

    题目链接:过河卒 直接模拟?会T掉60分. 所以我们可以采用递推,怎么想到的? 因为卒子只能向下或向右走,所以走到一个点的方法数,等于走到它上面点的方法数加上走到它左边点的方法数,这样就可以地推了. ...

  9. POJ-3252 Avenger

    题意:在区间中,他们化成2进制的数的0的个数大于等于1的数有多少个. 思路:我们需要记录上一次0和1的个数,此外我们还要特别注意一下前导0. 如果前面全是0的时候我们就要注意下一位是不是还是0,如果一 ...

  10. codeforces C. Functions again

    题意:给定了一个公式,让你找到一对(l,r),求解出公式给定的F值. 当时没有想到,我把(-1)^(i-l)看成(-1)^i,然后思路就完全错了.其实这道题是个简单的dp+最长连续子序列. O(n)求 ...