缓存应用--Memcached分布式缓存简介
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了缓存应用--Memcached分布式缓存简介,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5737字,纯文字阅读大概需要9分钟。
内容图文
<H2 style=‘background-position: 0% 100%; margin: 0px; padding: 6px 0px; color: rgb(51, 51, 51); letter-spacing: -1px; font-family: Verdana, "Lucida Grande", Geneva, Arial, sans-serif; font-size: 18px; font-weight: normal; background-image: url("http://common.cnblogs.com/Skins/Minyx2_Lite/images/dotsH.gif"); background-repeat: repeat no-repeat;‘>缓存应用--Memcached分布式缓存简介<SMALL style=‘text-align: right; color: rgb(171, 171, 171); line-height: 18px; font-family: Verdana, "Lucida Grande", Geneva, Arial, sans-serif; font-size: 12px; display: block;‘>2011-01-17 23:47 by 贺臣, 2 阅读, 28 评论, 收藏, 编辑
<DIV style=‘color: rgb(51, 51, 51); line-height: 1.8; font-family: Georgia, "Times New Roman", Times, sans-serif; word-break: break-all;‘ class="entry" data-find="_5">
一.<SPAN style=‘line-height: normal; font-family: "Times New Roman"; font-size: 7pt;‘><SPAN style=‘font-family: verdana, "courier new"; font-size: large;‘ class="Apple-style-span">
Memcached 是一个高性能的分布式内存 对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象 来减少读取数据库的次数,从而提供动态、数据库驱动网站的速度。
相信很多人都用过缓存,在 .net 中也有内置的缓存机制,还有很多第三方工具如apache,nginx等可以做静态资源的缓存,同时我们也可 以制定自己的缓存机制,缓存数据库查询的数据以减少对数据库的频繁操作。但是很多时候我们总是感觉这些缓存总不尽人意, Memcached可以解决你不少的烦恼问题。 最少在我的学习中解决了我不少问题,所以决定记录下来分享。
Memcached基于一 个存储键/值对的hashmap。其守护进程是用C<SPAN style=‘color: black; line-height: 1.8; font-family: "宋 体"; font-size: 10.5pt;‘>写的,但是客户端可以用任何语言来编写(本文使用C#作为例子),并通过memcached协议与守护进程通信。可 能这些东西都太高深了,我们暂不做研究。
二.<SPAN style=‘line-height: normal; font-family: "Times New Roman"; font-size: 7pt;‘> 分布式缓存
其实 Memcached作为一个分布式缓存数据服务,但是每个服务之间根本没有进行相互通信,这里可能与
我理解的分布式有点区别,可能是我才疏学浅,也可能是每个人思考问题的角度不同。Memcached 客户端就是通过一种分布式算法将数据保存到不同的Memcached服务器上,将数据进行缓存。分布
式缓存,可以而知memcached可以进行大数据量的缓存。这点可以弥补我们之前很多人都遇到的将
数据缓存到应用服务器上,而且只能缓存少量数据,否则对应用服务器的影响非常大。
Memcached应用机制图:
0px 0px 0px 40px; padding: 0px; border: currentColor; width: 738px; color: rgb(102, 102, 102); font-size: 13px; background-image: url("http://common.cnblogs.com/Skins/Minyx2_Lite/images/bq.gif"); background-repeat: no-repeat no-repeat; background-color: rgb(255, 255, 255);‘ >
这个图是有点简陋了,但是问题还是能够描述的清楚的,缓存机制的基本原理就是先查询数据保存到memcached中,地址在此请求就直接从Memcached缓存中取数据,这样就可以减少对服务器请求压力。
三.<SPAN style=‘line-height: normal; font-family: "Times New Roman"; font-size: 7pt;‘> Memcached 特征
(1)<SPAN style=‘line-height: normal; font-family: "Times New Roman"; font-size: 7pt;‘> 协议简单: 不使用复杂的xml格式,而是使用文本格式
(2)<SPAN style=‘line-height: normal; font-family: "Times New Roman"; font-size: 7pt;‘> 基于libevent的事件处理机制 (不懂)
(3)<SPAN style=‘line-height: normal; font-family: "Times New Roman"; font-size: 7pt;‘> 内置内存存储方式: 数据存在在内存中,所以重启机器会导致数据丢失
(4)Memcached相互不通信的分布式: Memcached 服务器之间不会进行通信,数据都是通过客户端的分布式算法存储到各个服务器中
四.<SPAN style=‘line-height: normal; font-family: "Times New Roman"; font-size: 7pt;‘> Memcached的安装
首先这里是在windows系统上做测试,Memcached在linux等非windows平台上性能会更高。
下载Memcached 服务端: http://memcached.org/ (官网寻找适用的版本)
1 解压缩文件到D:\Program Files\Memcached
2 命令行输入 D:\Program Files\Memcached\memcached.exe -d install
3 命令行输入 D:\Program Files\Memcached\memcached\memcached.exe -d start,该命令启动 Memcached ,默认监听端口为 11211当然我们可以在windows 服务中查看到此此服务:
<SPAN style=‘line-height: normal; font-family: "Times New Roman"; font-size: 7pt;‘><SPAN style=‘font-family: verdana, "courier new"; font-size: large;‘ class="Apple-style-span">五 .NET 平台下使用Memcached
因为个人首先接触的客户端是memcacheddotnet ,所以习惯了适用这个核心库作为客户端。下载的工作都非常简单,因为客户端都帮我们实现了分布式程序算法,我们关心的只要怎样去存在获取这些数据。下面简单介绍一下:
1 static void Main(string[] args)
2 {
3 SockIOPool pool = SockIOPool.GetInstance();
4 string[] servers = { "127.0.0.1:11211" };
5 pool.SetServers(servers);
6 pool.MinConnections = 3;
7 pool.MaxConnections = 5;
8 pool.InitConnections = 3;
9 pool.SocketConnectTimeout = 5000;
10 pool.Initialize();
11
12 MemcachedClient client = new MemcachedClient();
13 client.EnableCompression = false;
14 Console.WriteLine("-----------------------Memcached Set 设置值--------------------------");
15 client.Set("key1","value1");
16 Console.WriteLine(client.Get("key1"));
17 Console.WriteLine("-----------------------Memcached Add 设置值--------------------------");
18 client.Add("key2","value2");
19 Console.WriteLine(client.Get("key2"));
20 client.Set("key2","value1 value2");
21 Console.WriteLine(client.Get("key2"));
22 Console.WriteLine("-----------------------Memcached Replace 设置值--------------------------");
23 client.Replace("key2", "value3");
24 Console.WriteLine(client.Get("key2"));
25
26 Console.WriteLine("-----------------------Memcached 键值是否存在--------------------------");
27 if (client.KeyExists("key2"))
28 {
29 Console.WriteLine("键key2 存在");
30 }
31 if (client.KeyExists("hechen")==false)
32 {
33 Console.WriteLine("键hechen 不存在");
34 }
35
36 Console.WriteLine("-----------------------Memcached 删除数据--------------------------");
37 client.Add("key4","value4");
38 Console.WriteLine("key4==>" + client.Get("key4"));
39 client.Delete("key4");
40 if (!client.KeyExists("key4"))
41 {
42 Console.WriteLine("key4 已将删除");
43 }
44
45 Console.WriteLine("-----------------------Memcached 数据过期--------------------------");
46 client.Add("key5","value5",DateTime.Now.AddMilliseconds(5000));
47 Console.WriteLine(client.Get("key5"));
48 System.Threading.Thread.Sleep(6000);
49 Console.WriteLine("过期: "+ client.Get("key5"));
50 }
上面的例子虽然简单,包括一些基本的用法,在一般使用Memcached缓存数据的时候都用得到。
Memcached 初始化数据
SockIOPool 用于初始化分布式缓存池的对象,上面设置了多种属性,我相信这些属性的意思大家都懂。这里要注意的是Initialize () ,只有调用这个方式之后,才能初始化数据。才能使用连接缓存池。
Memcached 设置缓存值的三种方式
有心的可以测试一下这三个方法: Set () 如果缓存中存在相同的键值这替换原有的,Add() 只是添加数据,如果存在相同键的不再添加。 Replace() 则用于替换已有的相同的键值。
Memcached 缓存数据过期
很多时候我们并不希望数据永久缓存,一般都会有一个过期时间。上面的添加缓存数据就设置了缓存时间,可以达到过期缓存的效果。
案例下载地址 点击下载
此篇文章到此结束,内容比较简单,而且很多都是别人写过的东西。不过为了个人的学习还是积累起来,对于缓存部分后续文章继续更新。
Memcached 分布式缓存也不是如此简单,这里记录希望对自己和各位都有一定的帮助。有意见多多拍砖头。。。个人不介意的
内容总结
以上是互联网集市为您收集整理的缓存应用--Memcached分布式缓存简介全部内容,希望文章能够帮你解决缓存应用--Memcached分布式缓存简介所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。