C#使用Memcached缓存数据示例



1.安装Memcached服务器端

①.将memcached.exe复制到D:\Server;
②.在CMD窗口执行下边命令安装并启动Memcached服务:
D:\Server>memcached.exe -d install
D:\Server>memcached.exe -d start

2.C#存取数据示例

C#通常是引用第三方类库在Memcached存取数据,这边我们推荐使用Enyim

App.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>
    </configSections>
    <enyim.com>
        <memcached>
            <servers>
                <add address="127.0.0.1" port="11211" />
            </servers>
        </memcached>
    </enyim.com>
</configuration>

Program.cs:
using System;
using Enyim.Caching;
using Enyim.Caching.Memcached;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (MemcachedClient client = new MemcachedClient())
            {
                Person p1 = new Person() { Name = "zhang", Age = 20 };
                Person p2 = new Person() { Name = "li", Age = 30 };

                //1.保存
                client.Store(StoreMode.Set, p1.Name, p1);
                client.Store(StoreMode.Set, p2.Name, p2);

                //2.修改
                Person p3 = client.Get<Person>(p2.Name);
                if (p3 != null)
                {                    
                    p3.Age = 35;
                    client.Store(StoreMode.Set, p3.Name, p3);
                    Console.WriteLine("{0},{1}", p3.Name, p3.Age);
                }

                //3.删除
                client.Remove(p1.Name);
            }


            Console.WriteLine("ok");
            Console.ReadKey();
        }
    }

    [Serializable]
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

3.常见问题

问:Memcached服务默认端口是多少?
答:11211。

问:引用Enyim提示强名称验证失败怎么办?
答:参见http://www.mzwu.com/article.asp?id=3741

问:Memcached有哪些常用设置参数?
答:
引用内容 引用内容
-d install        安装memcached服务
-d uninstall      卸载memcached服务
-d start          启动memcached服务
-d restart        重起memcached服务
-d stop|shutdown  关闭正在运行的memcached服务

4.参考资料

[1]..NET中使用Memcached的相关资源整理:http://www.cnblogs.com/dudu/archive/2009/07/19/1526407.html
[2].Windows下的.NET+ Memcached安装:http://www.cnblogs.com/luluping/archive/2009/01/14/1375453.html
[3].使用Memcached提高.NET应用程序的性能:http://zhoufoxcn.blog.51cto.com/792419/528212/

评论: 6 | 引用: 0 | 查看次数: 9904
dnawo[2014-08-07 00:05 AM | 访问 http://www.mzwu.com/ | Mail To:dnawo@sohu.com | 220.200.12.191 | del | 回复回复]
6#
换台机子试下看-_-#
114690238[2014-08-06 09:18 AM | | Mail To:114690238@QQ.com | 58.22.123.82 | del | 回复回复]
5#
也是不行= =,会不会是我memcached安装错了,可是我用这个telnet localhost 11211连接,可以连接上啊
dnawo[2014-08-05 09:04 PM | 访问 http://www.mzwu.com/ | Mail To:dnawo@sohu.com | 220.200.14.40 | del | 回复回复]
4#
@114690238,试下我的:http://www.mzwu.com/pic/201408/demo.rar
114690238[2014-08-05 03:46 PM | | Mail To:114690238@QQ.com | 58.22.123.82 | del | 回复回复]
地板
@dnawo,好神奇= =我用了你的那个版本的试了,得到的返回值还是null,找不出哪出问题了,
dnawo[2014-08-01 11:22 PM | 访问 http://www.mzwu.com/ | Mail To:dnawo@sohu.com | 175.42.10.171 | del | 回复回复]
板凳
@114690238,文章代码都有测试过的,刚又运行了一次,正常。会不会是版本不一样造成的?我测试用Memcached 1.4.13,下载地址:http://pan.baidu.com/s/1o6oGjqU
114690238[2014-08-01 03:45 PM | | Mail To:114690238@QQ.com | 58.22.123.82 | del | 回复回复]
沙发
你好,这p3得到的返回值怎么是null= =
发表评论
登录后再发表评论!