Ubuntu腾讯云主机安装分布式memcache服务器,C#中连接云主机进行存储的示例

时间:2023-03-08 23:55:28
Ubuntu腾讯云主机安装分布式memcache服务器,C#中连接云主机进行存储的示例

Ubuntu腾讯云主机安装分布式memcache服务器,C#中连接云主机进行存储的示例(github代码:https://github.com/qq719862911/MemcacheTestDemo)

1、腾讯云安装memcache服务器,并且启动服务器。

1)安装Memcache服务端

sudo apt-get install memcached

安装完Memcache服务端以后,我们需要启动该服务:

memcached -d -m 128 -p 11111 -u root

这里需要说明一下memcached服务的启动参数:

-p 监听的端口 
-l 连接的IP地址, 默认是本机 
-d start 启动memcached服务 
-d restart 重起memcached服务 
-d stop|shutdown 关闭正在运行的memcached服务 
-d install 安装memcached服务 
-d uninstall 卸载memcached服务 
-u 以的身份运行 (仅在以root运行的时候有效) 
-m 最大内存使用,单位MB。默认64MB 
-M 内存耗尽时返回错误,而不是删除项 
-c 最大同时连接数,默认是1024 
-f 块大小增长因子,默认是1.25-n 最小分配空间,key+value+flags默认是48 
-h 显示帮助

2、C#中连接云memcache的存储数据。

1)Nuget安装 : EnyimMemcached  (C#中的memcache客户端)

2)在console程序的main中书写此代码。

    static void Main(string[] args)
{
MemcachedClientConfiguration mcConfig = new MemcachedClientConfiguration();
mcConfig.AddServer("119.29.176.32:11111");//云服务器的公网地址加 memcache的端口
using (MemcachedClient client =new MemcachedClient(mcConfig))
{
client.Store(Enyim.Caching.Memcached.StoreMode.Set, "name", "haiyi",TimeSpan.FromSeconds());
var name = client.Get<string>("name");
Console.WriteLine(name);
/*调试 模式 存储数据*/
//IStoreOperationResult result = client.ExecuteStore(Enyim.Caching.Memcached.StoreMode.Set, "name", "haiyi", TimeSpan.FromSeconds(30));
//Console.WriteLine( result.StatusCode+" is success: "+result.Success+ " InnerResult" + result.InnerResult);
// var getResult = client.ExecuteGet<string>("name");
//Console.WriteLine(getResult.InnerResult+" statuCode:"+getResult.StatusCode+" success:"+getResult.Success+ " value=" +getResult.Value); //+getResult.Exception.Message
}
Console.Read();
}