版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Redis CookbookRedis CookbookTiago Macedo and Fred Oliveira Cambridge Farnham Köln Sebastopol TokyoRedis Cookbookby Tiago Macedo and Fred OliveiraCopyright © 2011 Tiago Macedo and Fred Oliveira. Printed in the United States of America.Published by OReilly Media, Inc., 1005 Gravenstein Highw
2、ay North, Sebastopol, CA 95472.OReilly books may be purchased for educational, business, or sales promotional use. Online editionsare also available for most titles (). For more information, contact ourcorporate/institutional sales department: (800) 998-9938 or corporate.Editors: Andy Oram and Mike
3、HendricksonProduction Editor: Jasmine PerezProofer: OReilly Production ServicesCover Designer: Karen MontgomeryInterior Designer: David FutatoPrinting History:August 2011:First Edition.Nutshell Handbook, the Nutshell Handbook logo, and the OReilly logo are registered trademarks of OReilly Media, Inc
4、. Redis Cookbook, the image of the mouse opossum, and related trade dress are trademarks of OReilly Media, Inc.Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and OReilly Media, Inc. was
5、 aware of a trademark claim, the designations have been printed in caps or initial caps.While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information con- tai
6、ned herein.ISBN: 978-1-449-30504-8 LSI1311195806Table of ContentsPrefaceix1.An Introduction to Redis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .When to use Redis Problem SolutionInstalling Redis Problem Solution DiscussionUsing Redis Data Types P
7、roblemSolution Discussion1111333377772.s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99999Using Redis from the ProblemSolution DiscussionUsing Redis from Problem Solution Discussiond Linewith redis-py10101010Using Red
8、is from Ruby with redis-rb ProblemSolution DiscussionUsing Redis with Ruby on Rails ProblemSolution11111111121212vDiscussion123.Leveraging Redis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using Redis as a Key/Value Store ProblemSolution
9、 DiscussionInspecting Your Data ProblemSolution DiscussionImplementing OAuth on Top of Redis ProblemSolution DiscussionUsing Rediss Pub/Sub Functionality to Create a Chat System ProblemSolution DiscussionImplementing an Inverted-Index Text Search with Redis ProblemSolution DiscussionAnalytics and Ti
10、me-Based Data ProblemSolution DiscussionImplementing a Job Queue with Redis ProblemSolution DiscussionExtending Redis Problem Solution Discussion1515151516202021212222222226262627303031313535353539393939424243434.Redis Administration and Maintenance45Configuring Persistence ProblemSolution Discussio
11、nStarting a Redis Slave Problem454545464747vi | Table of ContentsSolution DiscussionHandling a Dataset Larger Than Memory ProblemSolution DiscussionUpgrading Redis Problem Solution DiscussionBacking up Redis Problem Solution DiscussionSharding Redis Problem Solution Discussion47474848484849494951515
12、1515253535354Appendix: Finding Help55Table of Contents | viiPrefaceIntroductionRedis is a data structure server with an in-memory dataset for speed. It is called a data structure server and not simply a key value store because Redis implements data struc- tures allowing keys to contain binary safe s
13、trings, hashes, sets and sorted sets, as well as lists. This combination of flexibility and speed makes Redis the ideal tool for many applications.Redis first started in early 2009 as a key value store developed by Salvatore Sanfilippo in order to improve the performance of his own LLOOGG, an analyt
14、ics product. Redis grew in popularity after getting support from people and companies in the developer world and has since been supported by VMware, who hired Salvatore and Pieter Noordhuis to work full-time on the project.Today, Redis is used by companies large and small doing both large and small
15、tasks.Companies like Engine Yard, Craigslist, Disqus, Digg, and Blizzard are part ofthe growing list of Redis adopters. An extended list of people working with Redis isavailable on the projects official site at.There are often several ways to solve problems using Redis. This book, while not a tutori
16、al on Redis, key value stores, or data structures, gives you recipes for solving specific problems with Redis that you can then adapt to your own problem set. Many of these recipes have come up because weve used them in our own jobs, solving our own problems.Each of these recipes solves a specific p
17、roblem using Redis, including a quick intro- duction to the problem, the solution, and a longer discussion with insight into how the solution works. Redis is, while simple in nature, quite extensive when it comes to func- tionality to manipulate and store data. This volume will thus not cover every
18、single d extensively. It will, however, give you the basics on solving specific problemswith it, in hopes that our solutions guide you to your own.ixConventions Used in This BookThe following typographical conventions are used in this book:ItalicIndicates new terms, URLs,Constant widthaddresses, fil
19、enames, and file extensions.Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.Constant width boldShowsds or other text that should be typed literally by the u
20、ser.Constant width italicShows text that should be replaced with user- mined by context.d values or by values deter-This icon signifies a tip, suggestion, or general note.This icon indicates a warning or caution.Using Code ExamplesThis book is here to help you get your job done. In general, you may
21、use the code in this book in your programs and documentation. You do not need to contact us for permission unless youre reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a
22、 CD-ROM of examples from OReilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your products documentation does require permission.We appreciate, but
23、 do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Redis Cookbook by Tiago Macedo and Fred Oliveira (OReilly). Copyright 2011 Tiago Macedo and Fred Oliveira, 978-1-449-30504-8.”If you feel your use of code examples falls outside fair u
24、se or the permission given above,feelto contact us at permissions.x | PrefaceSafari® Books OnlineSafari Books Online is an on-demand digital library that lets you easilysearch over 7,500 technology and creative reference books and find the answers you need quickly.s toWith a subscription, you c
25、anany page and watch anyfrom our library online.books on your cell phone and mobile devices. Access new titles before they areavailable for print, and get exclusive access to manuscripts in development and postfeedback for the authors. Copy and paste code samples, organize your favorites, down- load
26、 chapters, bookmark key sections, create notes, print out pages, and benefit from tons of other time-saving features.OReilly Media has uploaded this book to the Safari Books Onlirvice. To have fulldigital access to this book and others on similar topics from OReilly and other pub-lishers, sign up fo
27、rat.How to Contact UsPlease address comments and questions concerning this book to the publisher: OReilly Media, Inc.1005 Gravenstein Highway North Sebastopol, CA 95472(in the United States or Canada) (international or local)(fax)We have a wge for this book, where we list errata, examples, and any a
28、dditionalinformation. You can access this page at:To comment or ask technical questions about this book, sendbookquestionsto:For more information about our books, courses, conferences, and news, see our websiteatFind us on Follow us on Watch us on.:Preface | xiAcknowledgementsWe thank Pieter Noordhu
29、is for thoroughly reviewing several chapters of our book, our editor Andy Oram for his work on making us look good, Salvatore Sanfilippo for hiswords of encouragement, and our respective companies for the extra this book.time to writexii | PrefaceCHAPTER 1An Introduction to RedisThis chapter discuss
30、es some of Rediss basic concepts. Well look into when Redis is agreat fit, how to install the server and Rediss data types.d-lineon your machines, andWhen to use RedisProblemNearly every application has to store data, and often lots of fast-changing data. Until recently, most applications stored the
31、ir data using relational database management systems (RDBMS for short) like Oracle, MySQL, or PostgreSQL. Recently, however, a new paradigm of data storage has emerged from the need to store schema-less data in a more effective wayNoSQL. Choosing whether to use SQL or NoSQL is often an important fir
32、st step in the design of a successful application.SolutionThere are two important thing to consider when choosing whether to use SQL or NoSQL to store your data: its nature and your usage pattern. Some data is a great fitfor a relational storage engine, while other data benefits from the schema-natu
33、reof a NoSQL engine like Redis or its alternatives. If you dont rely on a particular RDBMS feature and need the performance or scalability of a NoSQL database, that might in fact be the ideal choice. So in order to decide whether your data should be stored in a RDBMS or NoSQL engine, you need to loo
34、k into a few specific things that will help you make a decision. Also bear in mind that quite often the ideal solution will be to use both.1Are your application and data a good fit for NoSQL?When working on the web, chances are your data and data mkeep changing withadded functionality and business u
35、pdates. Evolving the schema to support thesechanges in a relational database is a painful process, especially if you cant really afford downtimewhich people most often cant these days, because applications are expected to run 24/7. As a case in point, in a recent presentation on MongoDB, Jeremy Zawo
36、dny of Craigslist mentioned how changing the schema on their database typically takes a two month-long toll on their post archival service.Examples of data that are a particularly good fit for nonrelation storage are tranionals, historical data, and server logs. These are normally highly dynamic, ch
37、angingquite often, and their storage tends to grow quite quickly, further compounding theproblem of adjusting the schema to store them. They also dont typically feel “rela- tional”that is, the data in them doesnt tend to fan out in relationships to other types of data. Thats a good indication that t
38、hey can use something other than a RDBMS.Another way to gauge the fit for NoSQL is to look at whether you find yourself denormalizing your data for performance reasons, and no longer benefit from some of the advantages of a relational system, such as consistency and redundancy checks.One thing to ke
39、ep in mind is that NoSQL databases generally dont provide ACID(atomicity, consistency, isolation, durability), oronly partially. This allows themto make a few tradeoffs that wouldnt be possible otherwise. Redis provides partialACID compliance by design due to the fact that it is single thed (which g
40、uaranteesconsistency and isolation), and full compliance if configured with appendfsync always, providing durability as well.Performance can also be a key factor. NoSQL databases are generally faster, particularly for write operations, making them a good fit for applications that are write-heavy.All
41、 this being said, and even though NoSQL feels more flexible, there are also greatarguments to befor storing relational data in a RDBMS. If you have predictabledata that is a great fit for normalization, you can reap the benefits of using a relational data storage engine. Always look at the data befo
42、re making a decision.Dont believe the hypeNoSQL databases such as Redis are fast, scale easily, and are a great fit for many modern problems. But as with everything else, it is important to always choose the right tool for the job. Play to the strengths of your tools by looking at what youre storing
43、, how often youll access it, and how data (and its schema) might change over time.Once youve weighted all the options, picking between SQL (for stable, predictable, relational data) and NoSQL (for temporary, highly dynamic data) should be an easy task. Doing this kind of thinking in advance will sav
44、e you many headaches in future data migration efforts.2 | Chapter 1: An Introduction to RedisThere are also big differences between NoSQL databases that you shouldfor.For example, MongoDB (a popular NoSQL database) is a featueavy documentdatabase that allows you to perform range queries, regular exp
45、ression searches, index- ing, and MapReduce. You should weigh all the factors when choosing your database. As we said earlier, the questions boil down to what your data looks like and what your usage pattern is.For example, Redis is extremely fast, making it perfectly suited for applications that ar
46、e write-heavy, data that changes often, and data that naturally fits one of Rediss data structures (for instance, analytics data). A scenario where you probably shouldnt use Redis is if you have a very large dataset of which only a small part is “hot” (accessed often) or a case where your dataset do
47、esnt fit in memory.Installing RedisProblemYou want to install Redis on your computer.SolutionThere are several ways to install Redis on your computer or server, but the best and most flexible option is to compile it yourself. Nevertheless, depending on your distri- bution or operating system, there
48、are other options.DiscussionCompiling From SourceRedis evolves very quickly and package maintainers have a hard time keeup withthe latest developments. Since Redis doesnt have any external dependencies, compi-lation and installation are very straightforward, so we recommend youyourself.Redis should
49、build cleanly in most Linux distributions, Mac OS X, Solaris, and Cygwin on Windows.1. Downloading the sourceYou can download Redis from the official site or directly from theproject, either using Git or your browser to fetch a snapshot of one the branches or tags. This allows you get to get develop
50、ment versions, release candidates, etc.2. CompilingRedis compilation is straightforward. The only required tools should be a C com- piler (normally GCC) and Make. If you want to run the test suite, you also need Tcl 8.5.Installing Redis | 3After unpacking the source and changing your terminal path t
51、o the source direc- tory, just type:makeThis will compile Redis, which on a modern computer should take less than 10 seconds. If youre using a x86_64 system but would like an x86 build (which uses less memory but also has a much lower memory limit), you can do so by passing along 32bit to Make:make
52、32bitAfter compiling Redis, particularly if youre building a development version, you should run the test suite to ensure that the server is behaving as expected.make test3. InstallingAfter compiling Redis, you can go ahead and run it:cd src && ./redis-serverHowever, you might find it more c
53、onvenient to install it to another location in your system. The Makefile wlll also help you do that:make installThis will install Redis binaries to /usr/local/bin. If you wish to install to another location, you can pass it to make. For instance:make install /opt/localThis will install the binaries
54、in /opt/local/bin.After installating the Redis server, you should also copy the configuration file (redis.conf) to a path of your choice, the default being /etc/redis.conf. If your con- figuration file is in a different path from the default, you can pass it along as a parameter to redis-server:/usr
55、/local/bin/redis-server /alternate-location-for-redis-config.confInstalling on LinuxMost modern Linux distributions have Redis packages available for installation, but keep in mind that these are normally not up-to-date. However, if you prefer to use these, the installation procedure is much simpler
56、:Debian/Ubuntusudo apt-get install redis-serverFedora/CentOSsudo yum install redis4 | Chapter 1: An Introduction to RedisGentoosudo emerge redisThis approach has a few advantages: by using your package management system, you can more easily keep software up-to-date, and youll most likely get at leas
57、t security and stability updates. Besides that, youll also get startup scripts and an environmentmore suited to your distribution (users, log files, database location, etc).Installing on WindowsAlthough Redis is not officially supported on Windows for several reasonsnotablythe lack of a copy-on-write fork()the commuprovides a few ports. Due to
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2023-2024學(xué)年湖北省襄陽市棗陽市吳店二中九年級(jí)(上)第一次月考數(shù)學(xué)試卷
- 魯教版八年級(jí)數(shù)學(xué)上冊(cè)第三章數(shù)據(jù)的分析3從統(tǒng)計(jì)圖分析數(shù)據(jù)的集中趨勢(shì)課件
- 山東省菏澤市2024-2025學(xué)年高三上學(xué)期期中考試 物理 含答案
- 貴州省2024年中考化學(xué)真題(含答案)
- 內(nèi)蒙古呼和浩特市賽罕區(qū)市級(jí)名校2023-2024學(xué)年中考數(shù)學(xué)模擬試卷含解析
- 內(nèi)蒙古包頭市、巴彥淖爾市2023-2024學(xué)年中考數(shù)學(xué)全真模擬試題含解析
- 浙江省杭州市周邊重點(diǎn)中學(xué)四校2024-2025學(xué)年高二上學(xué)期10月聯(lián)考物理試題
- 公積金基礎(chǔ)知識(shí)題庫(kù)單選題100道及答案解析
- 廉政黨課課件教學(xué)課件
- 第二章+軸對(duì)稱圖形(小結(jié)與思考)(單元復(fù)習(xí))2023-2024學(xué)年八年級(jí)數(shù)學(xué)上冊(cè)同步課堂
- GB/T 15822.1-2024無損檢測(cè)磁粉檢測(cè)第1部分:總則
- (新版)煙草行業(yè)法律法規(guī)知識(shí)競(jìng)賽參考試題庫(kù)-上(單選、多選題)
- 中國(guó)家用美容儀行業(yè)市場(chǎng)研究分析及發(fā)展趨向研判報(bào)告
- 2024年反洗錢知識(shí)競(jìng)賽參考題庫(kù)400題(含答案)
- 2024年移動(dòng)網(wǎng)格經(jīng)理(認(rèn)證考試)備考試題庫(kù)大全-上單選、多選題匯
- xxx人民醫(yī)院檢查檢驗(yàn)結(jié)果互認(rèn)專項(xiàng)檢查暨診療行為規(guī)范自查報(bào)告
- 國(guó)企投資崗筆試題目
- SLT824-2024 水利工程建設(shè)項(xiàng)目文件收集與歸檔規(guī)范
- 七年級(jí)上冊(cè)語文統(tǒng)編版5 《秋天的懷念》活動(dòng)式公開課一等獎(jiǎng)創(chuàng)新教學(xué)設(shè)計(jì)(表格式)
- 年產(chǎn)120萬件汽車鋁合金輪轂建設(shè)項(xiàng)目可行性研究報(bào)告模板-立項(xiàng)拿地
評(píng)論
0/150
提交評(píng)論