版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Redis And PythonPyCon India, 2011Sunil AroraRaising Hands.How many of you have used Redis before ?How many of you have got a laptop ?About MeI tweet atSunil Arora / _sunil_I work atShopSociallyI blog at Todays talkWhat is RedisHow it works and what you can do with itReal life use-casesReal life use-
2、casesSome hand-on with RedisRedis - Brief HistoryInitially written to improve performance of Web Analytics product LLOOGG out of his startupSalvatore Sanfilippoantirez Redis Brief HistoryReleased in March 2009 (Open Source, BSD licensed)VMWare hired Salvatore in March, 2010Then Pieter Noordhuis (key
3、 contributor) was hiredWhat is Redis ?Its between lot of stuff, so difficult to categorize it preciselyWhat is Redis ?I see Redis definitely more as a flexible tool that as a solution specialized to solve a specific problem: his mixed soul of cache, store, and messaging server shows this very well-S
4、alvatore SanfilippoPicture by herzogbr Redis is.Remote Data Structure ServerRedis is.Supports rich data types of computer science- Strings, Lists, Sets, Sorted Sets, Hashes.Rich sets of primitives (commands) to manipulate these types Predictive complexity measurementsA few fundamentalsWritten in C (
5、no external dependency)Uses memory as main storageUses disk for persistenceSingle ThreadedEvery command performs atomic execution PerformanceScreamingly fast performance50K read/write operations per seconds100K+ read/write ops per second on a regular EC2 instance Installation$ git clone $ cd redis$
6、make$ ./src/redis-server.$./src/redis-cliRedis PINGPONGPython LibrariesRedis-py - Python client libraryTxredisapi - An asynchronous Python client for the Redis database, based on Twisted.Redisco - ORM for redis along the lines Ohm for Rubyredis-pyThe most popular python client libraryAndy McCurdy (
7、)Github: $easy_install redis OR$pip install redisOptional$easy_install hiredis$pip install hiredisLets get started.$ cd redis$ ./src/redis-server. from redis import Redis redis_client = Redis() redis_client.keys() help(redis_client)Redis KeysNot binary safe.Should not contain space or newline charac
8、terA few rules about keys:Too long keys are not a good ideaToo short keys is also not a good idea“object-type:id:field” can be a nice idea, i.e. “user:1001:name”Operations on KeysKEYSEXISTSDELEXPIREOBJECTPERSISTRANDOMKEYRENAMETYPETTLEXPIREATMOVELets play with keysredis_client.keys()redis_client.exis
9、ts(key)redis_client.delete(key)redis_client.type(key).Data StructuresStringsListsSetsSorted SetsHashesStringsSETGETMSETMGETSETEXGETSETSETNXINCRINCRBYDECRDECRBYStrings with redis client redis_client.set(key, value) redis_client.get(key) redis_client.delete(key)Fetch multiple keys at oncemget/msetredi
10、s_client.mset(key1: val1, key2: val2)redis_client.mget(key1, key2,.)ExpirationSet a value with expireredis_client.setex(key, value, 2) #key to expire in 2 secsredis_client.expire(key, 2)redis_client.get(key)NoneExpire SemanticsKey with expiry known as volatile keysWhenever a volatile key is modified
11、, its expiry is reset - To avoid inconsistency in following casesReplicationAppend Only LogUsesTo store transient states in your web applicationUsesWho is online?UsesRedis as LRU cache ( )Atomic Incrementshelp(redis_client.incr)help(redis_client.decr) redis_client.incr(counter, 1)1 redis_client.incr
12、(counter)2 redis_client.incr(counter)3UsesHigh Speed counters (views/clicks/votes/likes.)UsesAPI Rate LimitingUsesGenerating unique IDsListsOrdered list of binarysafe stringsDoubly linked listMemory footprint optimized for smaller listO(1) insertion/deletion at both endsLists - operationsLPUSHRPUSHL
13、SETLRANGELPOPBLPOPBRPOPBRPOPLPUSHLINSERTRPOPRPOPLPUSHLPUSHXRPUSHXUsesWeb apps are full of lists :)UsesCapped ListUsesReal time message QueueBackground Worker queues (Resque, Celery) UsesSocial Activity Streams or notificationsSetsAn unordered collection of distinct byte stringsNothing different from
14、 the data type in pythonSets - OperationsSADDSCARDSREMSISMEMBERSMEMBERSSPOPSRANDMEMBEROperations between SetsSMOVESUNIONSDIFFSINTERSets - OperationsSDIFFSTORESINTERSTORESUNIONSTORESets - UsesPicking random items from a set using SRANDMEMBEREx.Picking a random article from daily newsPick a random Ad
15、for servingPick a random option for A/BNote: Time complexity is O(1). Compare it with SQLs “order by Rand()”Sets - UsesTo model Relations in social graphEx.redis_client.sadd(friends:john, jenny, maria)redis_client.sadd(friends:ben, maria, kate)redis_client.sinter(friends:john, friends:ben)Relations
16、(friend/followers)Common followlist for A and Bsinter(users:A:follows, users:B:follows)Unique to B compared to Csdiff(users:B:follows, users:C:follows) Mutual relationship (friend as well as follower)sinter(users:B:followers, users:B:friends)Who does not follow me back ?sdiff(users:B:friends, users:
17、B:followers)Who am I not following back?sdiff(users:B:followers, user:B:friends)Which of my friends are online right now? SINTER online_people my_friendsWhich of my friends are online right now?RENAME online:fresh online:stale #every minuteSUNIONSTORE online:fresh online:stale#friend jack connectsSA
18、DD online:fresh jackSUNIONSTORE online online:fresh online:stale#who is online ?SINTER online friendsSorted SetsOrdered sets on the basis of scoreSorted SetsZADDZCARDZCOUNTZINCRBYZINTERSTOREZRANGEZRANGEBYSCOREZRANKZREMZREMRANGEBYRANKZREMRANGEBYSCOREZREVRANGEZREVRANGEBYSCOREZSCOREZUNIONSTORESorted Se
19、ts Use CaseTo build index on your datasetUses - Realtime Leaderboardszincrby(leaderboard, john, 2)zincrby(leaderboard, jack, 5)zincrby(leaderboard, kate, 1)zincrby(leaderboard, kate, 10).zrange(leaderboard, 0, -1, withscores = True)Uses - Realtime LeaderboardsMost download resourcesMost popular arti
20、cles on the websiteWeekly popular listMost downloaded stuff between date1 and date2. plete with Sored Sets HashesEquivalent to Python dictionary or Ruby hash or Java hashmapOperationshmset users:1 username: jim, score: 23hgetall users:1HincrbyUseful for storing structured datahmset user:1:preference
21、s flash_shown: yes, bgcolor: #fffExpire user:1:preferences Hmgetall user:1:preferences #returns entire dict Redis TransactionsUsing Multi/Watchp = redis_client.pipeline()p.lpush(a, 1)p.ltrim(a, 0, 100)p.executeLimitationSince commands are queued, Cant read valuesNo conditional executionIf not high write contention scenario, WATCH can be usedUse Redis Scripting to write your own primitiveDesigning with RedisData layout design on basis of QueryNo Query OptimizerManually build indexesDurabilitySnapshotting modeBinary dump every x secs or y opsAppen
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 婴儿用匙餐叉和餐刀产业规划专项研究报告
- 2024年私人财产损害补偿协议样本
- 2024企业融资解决方案服务协议
- 2024专业燃气供应协议格式范本
- 电子购物用户登录协议
- 手机租赁协议
- 游泳池循环水处理合同
- 国际学校宿舍管理员聘用合同
- 制造业档案管理设备故障预案
- 2024年城市综合体建设项目装修合同
- 《放飞梦想追求卓越》主题班会班主任反思
- 二年级音乐节奏训练课-动起来教学教案
- 《中国特色社会主义政治经济学(第二版)》第三章社会主义所有制制度
- 决策心理学第二讲课件
- 人卫第七版医学统计学课后答案及解析-李康、贺佳主编
- 舞蹈教室使用记录表
- 三年级上册美术课件-第7课 黄色和蓝色的画 人美版 (共21张PPT)
- 手术器械传递的原则与方法课件
- 五年级上册英语课件-Unit5 What do they do?(第一课时)|译林版(三起) (共20张PPT)
- 小学五年级整本书阅读方案
- 广西建筑施工企业三类人员-公共知识考试题库(含答案)
评论
0/150
提交评论