Python版本如下:
TutorialCachingStory
This is a story of Caching note: this is an overview of basic memcached use case, and how memcached clients work
Two plucky adventurers, Programmer and Sysadmin, set out on a journey. Together they make websites. Websites with webservers and databases. Users from all over the Internet talk to the webservers and ask them to make pages for them.
The webservers ask the databases for junk they need to make the pages. Programmer codes, Sysadmin adds webservers and database servers.
One day the Sysadmin realizes that their database is sick! It's spewing bile and red stuff all over! Sysadmin declares it has a fever, a load average of 20! Programmer asks Sysadmin, "well, what can we do?" Sysadmin says, "I heard about this great thing called memcached. It really helped livejournal!" "Okay, let's try it!" says the Programmer.
Our plucky Sysadmin eyes his webservers, of which he has six. He decides to use three of them to run the 'memcached' server. Sysadmin adds a gigabyte of ram to each webserver, and starts up memcached with a limit of 1 gigabyte each. So he has three memcached instances, each can hold up to 1 gigabyte of data. So the Programmer and the Sysadmin step back and behold their glorious memcached!
"So now what?" they say, "it's not DOING anything!" The memcacheds aren't talking to anything and they certainly don't have any data. And NOW their database has a load of 25!
Our adventurous Programmer grabs the pecl/memcache client library manual, which the plucky Sysadmin has helpfully installed on all SIX webservers. "Never fear!" he says. "I've got an idea!" He takes the IP addresses and port numbers of the THREE memcacheds and adds them to an array in
python
MEMCACHE_SERVERS = [
"10.1.1.1", //web1
"10.1.1.2", //web2
"10.1.1.3", //web3
]
Then he makes an object, which he cleverly calls '$memcache'.
memcache = Memcache()
for server in MEMCACHE_SERVERS:
memcache.addServer(server)
Now Programmer thinks. He thinks and thinks and thinks. "I know!" he says. "There's this thing on the front page that runs SELECT * FROM hugetable WHERE timestamp > lastweek ORDER BY timestamp ASC LIMIT 50000; and it takes five seconds!" "Let's put it in memcached," he says. So he wraps his code for the SELECT and uses his $memcache object. His code asks: Are the results of this select in memcache? If not, run the query, take the results, and PUT it in memcache! Like so:
huge_data_for_frong_page = memcache.get("huge_data_for_frong_page")
if not huge_data_for_frong_page:
huge_data_for_frong_page = []
sql = "SELECT * FROM hugetable WHERE timestamp > lastweek ORDER BY timestamp ASC LIMIT 50000"
res = db.curse.excute(sql) #不安全,因为没有sql转义。
if res:
for r in res:
huge_data_for_frong_page .append(r)
//cache for 5 minutes
memcache.set("huge_data_for_frong_page", huge_data_for_frong_page , 30 * 5)
// use $huge_data_for_frong_page how you please
Programmer pushes code. Sysadmin sweats. BAM! DB load is down to 10! The website is pretty fast now. So now, the Sysadmin puzzles, "What the HELL just happened!?" "I put graphs on my memcacheds! I used cacti, and this is what I see! I see traffic to one memcached, but I made three :(." So, the Sysadmin quickly learns the ascii protocol and telnets to port 11211 on each memcached and asks it:
Hey, 'get huge_data_for_front_page' are you there?
The first memcached does not answer...
The second memcached does not answer...
The third memcached, however, spits back a huge glob of crap into his telnet session! There's the data! Only once memcached has the key that the Programmer cached!
Puzzled, he asks on the mailing list. They all respond in unison, "It's a distributed cache! That's what it does!" But what does that mean? Still confused, and a little scared for his life, the Sysadmin asks the Programmer to cache a few more things. "Let's see what happens. We're curious folk. We can figure this one out," says the Sysadmin.
"Well, there is another query that is not slow, but is run 100 times per second. Maybe that would help," says the
Programmer. So he wraps that up like he did before. Sure enough, the server loads drops to 8!
So the Programmer codes more and more things get cached. He uses new techniques. "I found them on the list and the faq! What nice blokes," he says. The DB load drops; 7, 5, 3, 2, 1!
"Okay," says the Sysadmin, "let's try again." Now he looks at the graphs. ALL of the memcacheds are running! All of them are getting requests! This is great! They're all used!
So again, he takes keys that the Programmer uses and looks for them on his memcached servers. 'get this_key' 'get that_key' But each time he does this, he only finds each key on one memcached! Now WHY would you do this, he thinks?
And he puzzles all night. That's silly! Don't you want the keys to be on all memcacheds?
"But wait", he thinks "I gave each memcached 1 gigabyte of memory, and that means, in total, I can cache three gigabytes of my database, instead of just ONE! Oh man, this is great," he thinks. "This'll save me a ton of cash. Brad Fitzpatrick, I love your ass!"
"But hmm, the next problem, and this one's a puzzler, this webserver right here, this one runing memcached it's old, it's sick and needs to be upgraded. But in order to do that I have to take it offline! What will happen to my poor memcache cluster? Eh, let's find out," he says, and he shuts down the box. Now he looks at his graphs. "Oh noes, the DB load, it's gone up in stride! The load isn't one, it's now two. Hmm, but still tolerable. All of the other memcacheds are still getting traffic. This ain't so bad. Just a few cache misses, and I'm almost done with my work. So he turns the machine back on, and puts memcached back to work. After a few minutes, the DB load drops again back down to 1, where it should always be.
"The cache restored itself! I get it now. If it's not available it just means a few of my requests get missed. But it's not enough to kill me. That's pretty sweet."
So, the Programmer and Sysadmin continue to build websites. They continue to cache. When they have questions, they ask the mailing list or read the faq again. They watch their graphs. And all live happily ever after.
Author: Dormando via IRC. Edited by Brian Moon for fun. Further fun editing by Emufarmers. Further Edited for python code version by ME!
Totally speaking , It is very funny!
分享到:
相关推荐
【标题】"wiki(非常不错的wiki源码)"指的是一个用于创建和管理知识库的开源软件,可能是指HDWiki,这是一个广泛应用于构建企业或个人知识分享平台的系统。它提供了便捷的方式来组织、编辑和检索信息,使得知识的共享...
摘要:本文档介绍如何使用Word中的宏将doc格式自动转换成wiki格式,并附带完整的宏说明。 知识点一:什么是Wiki格式? Wiki格式是一种基于网页的文档格式,主要用于在线协作和文档分享。它使用简单的标记语言来...
OIWiki项目组在2021年10月3日发布的文档中,介绍了该项目的简介、如何参与、格式手册以及使用Docker部署OIWiki等方面的内容。文档中还详细涵盖了OI比赛相关知识,包括比赛简介、赛事、题型、学习路线、学习资源和...
**editor.md介绍** editor.md是一款开源的在线Markdown编辑器,特点包括: 1. **实时预览**:提供了边写边看的实时预览功能,提高编辑效率。 2. **丰富的编辑功能**:支持代码高亮、表格、流程图、数学公式等多种...
DokuWiki是一个强大的开源wiki引擎,专为那些寻求简洁、高效知识管理解决方案的个人和团队设计。这个系统基于PHP开发,无需数据库支持,这使得它易于安装和维护,尤其适用于那些资源有限或对数据库操作不熟悉的小型...
DokuWiki是一款开源的、基于文本的wiki系统,主要用于创建和维护结构化的文档。它适合小型团队或个人用于知识管理、文档共享和项目协作。在本案例中,我们讨论的是针对DokuWiki的三个插件:AddNewPage、XHEditor和...
vimwiki 在 vim官网所有插件中 好评排第三。 此包中含有五个文件: vimwiki1.2 包 vimwiki2.0 最新包,刚刚...介绍及文档 Vimwiki1.1.1QR.pdf Vimwiki syntax markup.html vimwiki_Description.txt 感谢vimwiki创作者
【标题】"word转wiki工具"指的是将Microsoft Word文档转换为MediaWiki格式的软件或工具。MediaWiki是一种广泛用于创建维基网站的开源标记语言,例如维基百科。这种工具通常能够帮助用户方便地将Word文档中的内容导入...
DokuWiki 是一个容易使用,用途多样的开源 Wiki 软件,并且不需要数据库。它因简洁易读的语法受到用户的喜爱。而容易维护、备份和整合则使它成为管理员的最爱。内置的访问控制列表和认证连接器使 DokuWiki 在企业...
### Wiki使用入门与教程详解 #### 一、Wiki简介 ...通过以上介绍,我们可以了解到Wiki不仅是一个简单的文档管理系统,更是一个强大的知识共享平台。掌握这些基本的操作方法对于有效管理和利用Wiki空间至关重要。
**DokuWiki与Editor.md编辑器的整合** DokuWiki是一款开源、轻量级的wiki系统,以其简单易用和强大的文本格式化能力而受到欢迎。它主要使用Markdown语法进行内容编写,使得用户能够轻松创建和编辑页面。然而,原生...
Wiki作为一种协作式的网页编辑工具,因其易用性和开放性成为构建知识库和协作平台的重要工具。Wiki的语法是用户编辑和格式化Wiki页面内容的基础。编辑Wiki页面简单直接,无需复杂的程序编写知识,通过熟悉几个基本的...
基于中文维基百科语料训练出的wiki.zh.text.model,压缩包中包含4个模型文件,提供...包括 wiki.zh.text.model 、wiki.zh.text.vector、wiki.zh.text.model.wv.vectors.npy、wiki.zh.text.model.trainables.syn1neg.npy
基于Wiki协作系统的学科知识库构建 本论文旨在探讨基于Wiki协作系统的学科知识库构建,旨在解决当前学科知识库发展中所面临的问题。文章首先分析了当前学科知识库发展中所面临的问题,包括用户互动不足、知识资源...
【标题】"wiki开源代码asp.net+sql"所涉及的知识点涵盖了多个方面,主要集中在Web开发领域,包括了Wiki系统的基础构建、编程语言C#、Web应用框架ASP.NET以及数据库管理系统SQL Server。以下是对这些知识点的详细阐述...
DokuWiki是一款高度优化的开源wiki引擎,专为创建、管理和分享知识而设计。这款软件在中文环境中同样表现优秀,支持在Windows和Linux操作系统上稳定运行,无论是个人使用还是企业级应用,都能满足用户对文档协作和...
**Go语言介绍** Go语言,又称为Golang,是由Google公司于2009年推出的一种静态类型的、编译型的、并发型的、垃圾回收的编程语言。它由Robert Griesemer、Rob Pike和Ken Thompson设计,目标是提供一种简单易学、高...
DokuWiki是一款优秀的开源Wiki系统,以其轻量化、高效能的特点,成为了许多企业构建内部知识库的首选方案。它由PHP编写,无需数据库支持,便于安装和维护,尤其适合中小型企业或团队使用。 一、DokuWiki的特点与...
".net版本的Wiki"指的是使用.NET框架构建的Wiki应用程序,可能是一个开源项目或商业解决方案,允许用户通过Web界面创建、编辑和组织信息。 在这个压缩包中,我们看到两个与许可证相关的文本文件:DbEntry.License....