您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > Redis-快速缓存一切
Redis-快速缓存一切高性能NoSQL数据库介绍NoSQL大行其道APP高并发请求海量化数据Disk内存图灵奖得主JimGray:“内存是新的硬盘,硬盘是新的磁带”memcached3100msec=FastappsrequirementsmaxE2Eresponsetime,underanyload50msec=averageInternetlatency50msec=requiredappresponsetime(includesprocessing&multiDBaccesses)1msec=requiredDBresponsetimeTheonlydatabasetomeetrequirement=高性能的要求4DBperformancecomparison@1msec@1msec@1msec@20msec@10-50msec@10-50msec@100msec@100msec@100msecDB性能比较5Realworldusecase:•500+GB•400Kwrites/sec•1500reads/sec•37.5KBaverageobjectsizeEfficiencyNoextraworkatapplevel1.5Gbps120GbpsTonesofworkatapplevelNoSQL6Nodescluster150+Nodescluster与MySQL性能比较IMDB(In-MemoryDB)QueryEngineQueryEngineStorageManagerMemory(Cache)DiskDiskMemoryIn-MemorydatastoreDiskbaseddatastoreSpeedDurabilityDiskisoptionalRedis分享大纲Redis是什么Redis的特性Redis的API及适用场景Redis的配置案例分析IntroductiontoRedisRedisisanopensource,BSDlicensed,advancedkey-valuecacheandstore.Itisoftenreferredtoasadatastructureserversincekeyscancontainstrings,hashes,lists,sets,sortedsets,bitmapsandhyperloglogs.年2月份,YouPorn的技术人员EricPickup在Google群组宣告他们网站改用RedisDB后。扛住了每天1亿PV浏览量,每秒30万请求,已经坚持2周。Redis是什么sponsoredby:WhatisRedisis...an“advancedkey-valuestore”bySALVATORESANFILIPPO(@antirez)Redis基本信息•REmoteDIctionaryServer•Creator:意大利程序员SalvatoreSanfilippo(antirez)•Released:2009•WritteninANSIC•Singlethreaded•OpenSource•BackedbyVMWare•EarlyadoptedbyGitHubCREATETABLEredis(kVARCHAR(512MB)NOTNULL,vVARCHAR(512MB),PRIMARYKEY(k));Redis的特点锋利,非常快小巧,简单易上手实用,功能强大非常适合解决服务端开发某些使用关系数据异常棘手问题,居家旅行必备谁在用Redis性能测试redis-benchmark-r1000000-n2000000-tget,set,lpush,lpop,mset-P16-qOnmylaptop:SET:513610requestspersecondGET:641436requestspersecondLPUSH:845665requestspersecondLPOP:783392requestspersecondMSET(10keys):89988requestspersecondOnasmalldigitaloceanserver($5/month)SET:227816requestspersecondGET:228258requestspersecondLPUSH:251098requestspersecondLPOP:251572requestspersecondMSET(10keys):43918requestspersecondTheRedisManifesto1.ADSLforAbstractDataTypes2.Memorystorageis#13.FundamentaldatastructuresforafundamentalAPI4.TwolevelsofAPI5.Codeislikeapoem;it'snotjustsomethingwewritetoreachsomepracticalresult6.We'reagainstcomplexity7.WeoptimizeforjoyRedisvsMemcacheMemcached操作实例键(keys)值(values)page:index.htmlhtmlhead[...]user:101:sessionxDrSdEwd4dSlZkEkj+login_count“100001”user:100:last_login_time“102736485756”所有的操作都是基于字符串、或者是2进制的数据格式、操作起来大部分是GET、SET这2个命令操作,易于使用和Memcached一样、使用简单、性能高效(1G1M)异步持久化存储支持多种数据类型;Strings(字符)、Lists(链表)、Sets(集合)、ZSets(有序集合)、Hashes(哈希)更多、更方便API接口管理数据;MorecommandsandMore….IsRedisRedisvsMemcacheKey={DataStructures}Redis数据类型实例键(keys)值(values)page:index.htmlhtmlhead[...]users_logged_in_today{1,2,3,4,5}latest_post_ids[201,204,209,..]users_and_scoresjoe~1.3483bert~93.4fred~283.22chris~23774.17StringSetsListZSetsRedisvsMemcacheRedisvsMemcacheRedisvsMemcacheRedis安装与运行•官网下载:•构建:makePREFIX=/some/other/directoryinstall•运行:•./redis-server/path/to/redis.conf•./redis-server--port9999--slaveof127.0.0.16379•./redis-server/etc/redis/6379.conf--logleveldebug•Windows用户•unofficialversion://github.com/ServiceStackApps/RedisAdminUIRedisLiveRedis的APIStrings字符./redis-cliSETmystring“helloworld”Cclientkeyvalue./redis-cliGETmystringhelloworldreturn://try.redis.io/Key-valuewhat?!RedisCloudsetnamebingoohuangOK0.115msec,complexity:O(1)RedisCloudgetnamebingoohuang0.091msec,complexity:O(1)RedisCloudexpirename120(integer)10.04msec,complexity:O(1)RedisKey命名约定object-type:id:fielduser:1000:passwordcomment:1234:reply.tocomment:1234:reply-to最大允许key大小512MB.Atomic操作RedisCloudsetcount0OK0.15msec,complexity:O(1)RedisCloudincrcount(integer)10.251msec,complexity:O(1)GETSETMGETSETNXSETEXMSETMSETNXStrings字符INCRINCRBYDECRDECRBYAPPENDSUBSTRExpiration当把Redis当作缓存服务使用时,你可以设置数据的过期时间,不管是数据类型(string,list,zsets,sets,hash)是什么,此系列命令都有效EXPIREpaitoubing1234TTLpaitoubing==1234秒秒DeletingKeysDELpaitoubingEXISTSpaitoubing==0(false)OrderedlistsRedisCloudlpushfriendsbingooRedisCloudlpushfriendsdingooRedisCloudlpushfriendspingoo(integer)30.247msec,complexity:O(1)RedisCloudllenfriends(integer)30.11msec,complexity:O(1)ListsabcdefLPUSHRPUSHLPOPRPOPLPUSHpaitoubingaListsabcdefLLEN==6xLREM1bLRANGE23LINDEX5队列服务QueuesabcdefRPUSHLPOPRPUSHpaitoubingabcRPUSHpaitoubingdefLPOPpaitoubing==“abc”LPOPpaitoubing==“def”LPOPpaitoubing==(nil)RedisCloudLRANGEfriends001)pingoo0.073msec,complexity:O(S+N)RedisCloudLRANGEfriends0-11)pingoo2)dingoo3)bingoo0.146msec,complexity:O(S+N)练习LPUSHkey1=1LPUSHkey2=2LPUSHkey3=3LRANGEkey0-‐1=3=2=1RPOPkey=1LRANGEkey0-‐1=3=2LLENkey=2LTRIMkey01=OKSetsSADDkey1=1SADDkey2=2SADDkey3=3SMEMBERSkey=3=1=2SISMEMBERkey1=1SISMEMBERkey5=0SRANDMEMBERkey=RANDSREMkey3=1集合Setsuid:1:followersbrucetomzhangsanlisiwangermazixiaoyueyuefengjieuid:2:followersxiaoyueyuefengjieliyuchunfurongjiejieSREMuid:1:followerswangermazixSMOVEuid:1:followersuid:2:followerslisiSADDuid:2:followerstom集合Setsuid:1:followersbr
本文标题:Redis-快速缓存一切
链接地址:https://www.777doc.com/doc-4209800 .html