redis 应用

1:首先下载redis:redis-2.0.2.zip(32bit),解压。

从下面地址下:http://code.google.com/p/servicestack/wiki/RedisWindowsDownload,看到下面有redis-2.0.2.zip(32bit),就是他了,下载完成后,解压到D:\redis-2.0.2.

2:创建redis.conf文件:

这是一个配置文件,指定了redis的监听端口,timeout等。如下面有:port6379。

把下面内容COPY到一新建文件中,取名redis.conf,再保存到redis-2.0.2目录下:

#Redisconfigurationfileexample

#BydefaultRedisdoesnotrunasadaemon.Use'yes'ifyouneedit.

#NotethatRediswillwriteapidfilein/var/run/redis.pidwhendaemonized.

daemonizeno

#Whenrunasadaemon,Rediswriteapidfilein/var/run/redis.pidbydefault.

#Youcanspecifyacustompidfilelocationhere.

pidfile/var/run/redis.pid

#Acceptconnectionsonthespecifiedport,defaultis6379

port6379

#Ifyouwantyoucanbindasingleinterface,ifthebindoptionisnot

#specifiedalltheinterfaceswilllistenforconnections.

#

#bind127.0.0.1

#ClosetheconnectionafteraclientisidleforNseconds(0todisable)

timeout300

#Setserververbosityto'debug'

#itcanbeoneof:

#debug(alotofinformation,usefulfordevelopment/testing)

#notice(moderatelyverbose,whatyouwantinproductionprobably)

#warning(onlyveryimportant/criticalmessagesarelogged)

logleveldebug

#Specifythelogfilename.Also'stdout'canbeusedtoforce

#thedemontologonthestandardoutput.Notethatifyouusestandard

#outputforloggingbutdaemonize,logswillbesentto/dev/null

logfilestdout

#Setthenumberofdatabases.ThedefaultdatabaseisDB0,youcanselect

#adifferentoneonaper-connectionbasisusingSELECT<dbid>where

#dbidisanumberbetween0and'databases'-1

databases16

################################SNAPSHOTTING#################################

#

#SavetheDBondisk:

#

#save<seconds><changes>

#

#WillsavetheDBifboththegivennumberofsecondsandthegiven

#numberofwriteoperationsagainsttheDBoccurred.

#

#Intheexamplebelowthebehaviourwillbetosave:

#after900sec(15min)ifatleast1keychanged

#after300sec(5min)ifatleast10keyschanged

#after60secifatleast10000keyschanged

save9001

save30010

save6010000

#CompressstringobjectsusingLZFwhendump.rdbdatabases?

#Fordefaultthat'ssetto'yes'asit'salmostalwaysawin.

#IfyouwanttosavesomeCPUinthesavingchildsetitto'no'but

#thedatasetwilllikelybebiggerifyouhavecompressiblevaluesorkeys.

rdbcompressionyes

#ThefilenamewheretodumptheDB

dbfilenamedump.rdb

#Fordefaultsave/loadDBin/fromtheworkingdirectory

#Notethatyoumustspecifyadirectorynotafilename.

dir./

#################################REPLICATION#################################

#Master-Slavereplication.UseslaveoftomakeaRedisinstanceacopyof

#anotherRedisserver.Notethattheconfigurationislocaltotheslave

#soforexampleitispossibletoconfiguretheslavetosavetheDBwitha

#differentinterval,ortolistentoanotherport,andsoon.

#

#slaveof<masterip><masterport>

#Ifthemasterispasswordprotected(usingthe"requirepass"configuration

#directivebelow)itispossibletotelltheslavetoauthenticatebefore

#startingthereplicationsynchronizationprocess,otherwisethemasterwill

#refusetheslaverequest.

#

#masterauth<master-password>

##################################SECURITY###################################

#RequireclientstoissueAUTH<PASSWORD>beforeprocessinganyother

#commands.Thismightbeusefulinenvironmentsinwhichyoudonottrust

#otherswithaccesstothehostrunningredis-server.

#

#Thisshouldstaycommentedoutforbackwardcompatibilityandbecausemost

#peopledonotneedauth(e.g.theyruntheirownservers).

#

#requirepassfoobared

###################################LIMITS####################################

#Setthemaxnumberofconnectedclientsatthesametime.Bydefaultthere

#isnolimit,andit'suptothenumberoffiledescriptorstheRedisprocess

#isabletoopen.Thespecialvalue'0'meansnolimts.

#OncethelimitisreachedRediswillcloseallthenewconnectionssending

#anerror'maxnumberofclientsreached'.

#

#maxclients128

#Don'tusemorememorythanthespecifiedamountofbytes.

#WhenthememorylimitisreachedRediswilltrytoremovekeyswithan

#EXPIREset.Itwilltrytostartfreeingkeysthataregoingtoexpire

#inlittletimeandpreservekeyswithalongertimetolive.

#Rediswillalsotrytoremoveobjectsfromfreelistsifpossible.

#

#Ifallthisfails,Rediswillstarttoreplywitherrorstocommands

#thatwillusemorememory,likeSET,LPUSH,andsoon,andwillcontinue

#toreplytomostread-onlycommandslikeGET.

#

#WARNING:maxmemorycanbeagoodideamainlyifyouwanttouseRedisasa

#'state'serverorcache,notasarealDB.WhenRedisisusedasareal

#databasethememoryusagewillgrowovertheweeks,itwillbeobviousif

#itisgoingtousetoomuchmemoryinthelongrun,andyou'llhavethetime

#toupgrade.Withmaxmemoryafterthelimitisreachedyou'llstarttoget

#errorsforwriteoperations,andthismayevenleadtoDBinconsistency.

#

#maxmemory<bytes>

##############################APPENDONLYMODE###############################

#BydefaultRedisasynchronouslydumpsthedatasetondisk.Ifyoucanlive

#withtheideathatthelatestrecordswillbelostifsomethinglikeacrash

#happensthisisthepreferredwaytorunRedis.Ifinsteadyoucarealot

#aboutyourdataanddon'twanttothatasinglerecordcangetlostyoushould

#enabletheappendonlymode:whenthismodeisenabledRediswillappend

#everywriteoperationreceivedinthefileappendonly.log.Thisfilewill

#bereadonstartupinordertorebuildthefulldatasetinmemory.

#

#Notethatyoucanhaveboththeasyncdumpsandtheappendonlyfileifyou

#like(youhavetocommentthe"save"statementsabovetodisablethedumps).

#StillifappendonlymodeisenabledRediswillloadthedatafromthe

#logfileatstartupignoringthedump.rdbfile.

#

#Thenameoftheappendonlyfileis"appendonly.log"

#

#IMPORTANT:ChecktheBGREWRITEAOFtocheckhowtorewritetheappend

#logfileinbackgroundwhenitgetstoobig.

appendonlyno

#Thefsync()calltellstheOperatingSystemtoactuallywritedataondisk

#insteadtowaitformoredataintheoutputbuffer.SomeOSwillreallyflush

#dataondisk,someotherOSwilljusttrytodoitASAP.

#

#Redissupportsthreedifferentmodes:

#

#no:don'tfsync,justlettheOSflushthedatawhenitwants.Faster.

#always:fsyncaftereverywritetotheappendonlylog.Slow,Safest.

#everysec:fsynconlyifonesecondpassedsincethelastfsync.Compromise.

#

#Thedefaultis"always"that'sthesaferoftheoptions.It'suptoyouto

#understandifyoucanrelaxthisto"everysec"thatwillfsynceverysecond

#orto"no"thatwilllettheoperatingsystemflushtheoutputbufferwhen

#itwant,forbetterperformances(butifyoucanlivewiththeideaof

#somedatalossconsiderthedefaultpersistencemodethat'ssnapshotting).

appendfsyncalways

#appendfsynceverysec

#appendfsyncno

###############################ADVANCEDCONFIG###############################

#Gluesmalloutputbufferstogetherinordertosendsmallrepliesina

#singleTCPpacket.UsesabitmoreCPUbutmostofthetimesitisawin

#intermsofnumberofqueriespersecond.Use'yes'ifunsure.

glueoutputbufyes

#Useobjectsharing.Cansavealotofmemoryifyouhavemanycommon

#stringinyourdataset,butperformslookupsagainstthesharedobjects

#poolsoitusesmoreCPUandcanbeabitslower.Usuallyit'sagood

#idea.

#

#Whenobjectsharingisenabled(shareobjectsyes)youcanuse

#shareobjectspoolsizetocontrolthesizeofthepoolusedinordertotry

#objectsharing.Abiggerpoolsizewillleadtobettersharingcapabilities.

#Ingeneralyouwantthisvaluetobeatleastthedoubleofthenumberof

#verycommonstringsyouhaveinyourdataset.

#

#WARNING:objectsharingisexperimental,don'tenablethisfeature

#inproductionbeforeofRedis1.0-stable.Stillpleasetrythisfeaturein

#yourdevelopmentenvironmentsothatwecantestitbetter.

#shareobjectsno

#shareobjectspoolsize1024

3:在cmd下面执行以下命令,指定它使用我们的redis.conf,同时也是启动,把redis运行起来,这里指定用redis.conf的配置运行服务器

D:\redis-2.0.2>redis-server.exeredis.conf

4:开一新DOS窗口cmd.执行以下命令,这是Redis的客户端程序:

redis-cli.exe-h172.18.5.1-p6379

172.18.5.1是我本机IP地址,端口6379就是上面配置文件中指定的监听端口

执行完成后,应该能看到redis启动了,这时在第一个cmd窗口可以看到连接信息。

执行一条保存keyvalue操作

setmystock300156

再查询一下

getmystock

=================================

相关推荐