mencache的使用二

本文介绍如何在C#中使用Memcached客户端进行缓存操作。包括配置连接池、设置缓存参数及基本的增删查改操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 在这里说的是在C#中的使用,在C#中使用是需要引入驱动的,

可以在网上找,这里推荐一个链接http://sourceforge.net/projects/memcacheddotnet/

将Memcached.ClientLibrary;引入。另外的 commands.dll log4net.dll,ICSharpCode.SharpZipLib.dll

等,应该是被以来的,下面的代码显示,使用到的类只在Memcacahed.ClientLibrary程序集中。其中log4net是

memcache记录日志所用。

简单的代码如下:

using Memcached.ClientLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace memcache
{
    class Program
    {
        static void Main(string[] args)
        {

            string[] server = { "10.21.160.31:11211"};
            SockIOPool pool = SockIOPool.GetInstance();//在Memcached.ClientLibrary程序集里。
            pool.SetServers(server);
            pool.InitConnections = 3;
            pool.MinConnections = 3;
            pool.MaxBusy = 5;
            pool.SocketConnectTimeout = 1000;
            pool.SocketTimeout = 3000;
            pool.MaintenanceSleep = 30;
            pool.Failover = true;
            pool.Nagle = false;
            pool.Initialize();
            //获得客户端实例
            MemcachedClient client = new MemcachedClient();//在Memcached.ClientLibrary程序集里
            client.EnableCompression = false;
            client.Add("keyd1", "value12dfsdf", DateTime.Now.AddHours(1), 0);
            Console.WriteLine(client.Get("keyd1").ToString());
            Console.ReadKey();
        }
    }
}

 

转载于:https://www.cnblogs.com/xiaoai123/p/6473137.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值