小古Blog
C#使用memcached高速缓存
2012-8-27 小古
 Memcached是开源高性能分布式缓存组件,目前已经广泛应用各类互联网领域. 具有多种语言的客户端开发包,包括:Perl/PHP/JAVA/C/Python/Ruby/C#/MySQL. 这此站点已经在使用YouTube,Reddit,Zynga,Facebook,Orange它. 看

下面的图比较形象.

MemcachedEffect



同样它也是扩平台的,今天我们来看windows下的服务端以及使用.net的客户端访问它.我们

选择了Enyim Memcached这个客户端.

首先,我们安装windows版的服务端,你可以从这个站点下载. 安装时使用命令行:



memcached.exe –d install


memcached.exe -d start 该命令启动 Memcached,默认监听端口为 11211

memcached.exe -p 端口号 自行设置监听端口



这样将Windows service中创建一个memcached的服务. 同样你可以使用 –d uninstall 来卸载它. 运行时将默认监听11211

IP:127.0.0.1,如下图所示:

memcache1


接着创建一个控制台程序引用Enyim.Caching.dll, 并在配置文件:


   1:  <configuration>
   2:    <configSections>
   3:      <sectionGroup name="enyim.com">
   4:        <section name="memcached" type="Enyim.Caching.Configuration.MemcachedClientSection, Enyim.Caching"/>
   5:      </sectionGroup>
   6:    </configSections>
   7:    <enyim.com>
   8:      <memcached protocol="Binary">
   9:        <servers>
  10:          <add address="127.0.0.1" port="11211"/>
  11:        </servers>
  12:      </memcached>
  13:    </enyim.com>
  14:    </configuration>



测试一下:


   1:              using (var mc = new MemcachedClient())
   2:              {
   3:                  mc.Store(StoreMode.Set, "mytime", DateTime.Now.ToString());
   4:                  string temp = mc.Get<string>("mytime");
   5:   
   6:                  Console.Write(temp);
   7:                  Console.Read();
   8:              }
 

示例2:


配置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);

            }

        }

    }

}

发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容