分布式缓存系统memcahce入门教程

运维 系统运维 分布式
Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,本文是分布式缓存系统memcahce入门那些事儿,希望能帮您分忧。

这篇开始决定把系列文章的名字改掉,想了个好名字,反正不是玩单机版的就行了。好了,这篇我们看看一种非持久化的缓存服务器memcache,说到缓存本能反映就是cache,session什么的,是的,可以说这些都是基于.net进程的,通俗点也就做不了多机器的共享,典型的一个就是SSO。

专题推荐:回味那些经典的分布式文件系统

一: 安装

memcahce像redis,mongodb一样都需要开启他们自己的服务端,我们下载Memcached_1.2.5.zip,然后放到C盘,修改文件

名为memcached。

1:install

install可以说是万能通用命令,首先我们转到memcached目录,然后memcached.exe -d install 即可。

2:start

现在我们只要启动start即可,要注意的就是memecache默认的端口是11211,当然我也不想重新指定端口了。

3:stop,uninstall

这两个就不截图了,一个是停止,一个是卸载,反正都是万能通用命令。

二:驱动程序

memcache的服务器我们就已经开启好了,由于在公司最近一直都在用php,算了还是用C#驱动吧,谁让这是.net

社区呢,下载C#驱动,既然是缓存服务器,只要有基本的CURD,我想应该就差不多了。

  1.  1 using System; 
  2.  2 using System.Collections.Generic; 
  3.  4 namespace BeIT.MemCached 
  4.  5 { 
  5.  6 class Example 
  6.  7 { 
  7.  8 public static void Main(string[] args) 
  8.  9 { 
  9. 10  //通过配置文件初始化memcache实例 
  10. 11  MemcachedClient cache = MemcachedClient.GetInstance("MyConfigFileCache"); 
  11. 13  //编辑(可以模拟session操作,缓存20分钟) 
  12. 14  cache.Set("name", "一线码农", DateTime.Now.AddMinutes(20)); 
  13. 16  //获取 
  14. 17  var result = cache.Get("name"); 
  15. 19  Console.WriteLine("获取name的缓存数据为: " + result); 
  16. 21  //删除 
  17. 22  cache.Delete("name"); 
  18. 24  Console.WriteLine("\n成功删除cache中name的数据"); 
  19. 26  result = cache.Get("name"); 
  20. 28  Console.WriteLine("\n再次获取cache中name的数据为:" + (result ?? "null") + "\n"); 
  21. 30  //查看下memecahce的运行情况 
  22. 31  foreach (KeyValuePair<string, Dictionary<string, string>> host in cache.Status()) 
  23. 32  { 
  24. 33  Console.Out.WriteLine("Host: " + host.Key); 
  25. 34  foreach (KeyValuePair<string, string> item in host.Value) 
  26. 35  { 
  27. 36  Console.Out.WriteLine("\t" + item.Key + ": " + item.Value); 
  28. 37  } 
  29. 38  Console.Out.WriteLine(); 
  30. 39  } 
  31. 41  Console.Read(); 
  32. 42  } 
  33. 43 } 
  34. 44 } 

我们再定义下配置文件,既然memcache可以用于分布式,那就避免不了将cache分摊到几台服务器上去,可以看到,下面的

配置也是非常简单的,当然分配的法则自然是memcache自身的算法决定的,最后别忘了在另一台服务器上开放一个端口就它

就行了。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="beitmemcached" type="System.Configuration.NameValueSectionHandler" />
  </configSections>
  <appSettings>
  </appSettings>
  <beitmemcached>
    <add key="MyConfigFileCache" value="127.0.0.1:11211" />
    <!--<add key="MyConfigFileCache" value="127.0.0.1:11211,127.0.0.1:8888" />-->
  </beitmemcached>
</configuration>

 

下面是打包程序:BeITMemcached,也可以到codegoogle去下载。

【编辑推荐】

  1. 部署分布式文件系统需要注意什么?
  2. 分布式文件系统DFS详细解读
  3. 如何区分分布式/集群/并行文件系统?
  4. FastDFS分布式文件系统的安装及配置
  5. 开源分布式文件系统FastDFS和MogileFS对比
  6. 浅谈淘宝技术发展:分布式时代——服务化

 

【责任编辑:黄丹 TEL:(010)68476606】

责任编辑:黄丹 来源: 博客
相关推荐

2009-11-09 09:25:24

Memcached入门

2022-03-22 11:35:10

数据建模PostgreSQLCitus

2019-02-18 11:16:12

Redis分布式缓存

2022-04-07 17:13:09

缓存算法服务端

2017-12-12 14:51:15

分布式缓存设计

2018-12-14 10:06:22

缓存分布式系统

2023-05-05 06:13:51

分布式多级缓存系统

2009-02-06 09:38:38

memcached分布式缓存系统ASP.NET

2023-05-12 11:52:21

缓存场景性能

2023-05-29 14:07:00

Zuul网关系统

2013-06-13 11:29:14

分布式分布式缓存

2023-05-12 08:23:03

分布式系统网络

2023-02-28 07:01:11

分布式缓存平台

2023-10-26 18:10:43

分布式并行技术系统

2023-02-11 00:04:17

分布式系统安全

2017-10-27 08:40:44

分布式存储剪枝系统

2023-01-13 07:39:07

2018-02-07 10:46:20

数据存储

2015-12-14 17:35:21

GemFire12306分布式

2015-09-21 09:20:11

C#Couchbase使用
点赞
收藏

51CTO技术栈公众号