一、简介
redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)和zset(有序集合)。
这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。
二、下载redis
Redis 官网 :http://redis.io/
下载页面:http://redis.io/download
官方文档:http://redis.io/documentation
Windows 版本下载:https://github.com/dmajkic/redis/downloads
三、环境搭建
1. 放到磁盘里面。
2. 根据操作系统 进入相应的目录 ,启动redis服务端 redis-server.exe redis.conf
3. 根据操作系统 进入相应的目录 ,启动redis客户端。
4. 测试redis缓存机制。
redis-cli.exe -h 127.0.0.1 -p 6379
set keytest valuestest
get keytest
5. 效果如下:

5. 测试调换顺序调用的实例。
这个应用可以用在验证码的校验用以及缓存。
第一个号码 18276487300 发送了一个验证码”1234“,然后存到key-values 里面。
第二个号码 18276487301 发送了一个验证码”2345“,然后存到key-values 里面。
但是第二个验证码先进行验证,所以会先通过18276487301 来获取验证码。
这样就防止了一些验证码不知道对应哪个手机号码了。
后面会根据java配置到项目中应用。
四、redis.conf 配置文件
根据操作系统 进入相应的目录 ,会看到一个配置文件redis.conf。
配置文件的原文如下:
<span># Redis configuration file example
# Note on units: when memory size is needed, it is possible to specifiy
# it in the usual form of 1k 5GB 4M and so forth:
#
# 1k </span>=> 1000<span> bytes
# 1kb </span>=> 1024<span> bytes
# 1m </span>=> 1000000<span> bytes
# 1mb </span>=> 1024*1024<span> bytes
# 1g </span>=> 1000000000<span> bytes
# 1gb </span>=> 1024*1024*1024<span> bytes
#
# units are </span><span>case</span><span> insensitive so 1GB 1Gb 1gB are all the same.
# By </span><span>default</span> Redis does not run as a daemon. Use ‘yes‘ <span>if</span><span> you need it.
# Note that Redis will write a pid file in </span>/var/run/<span>redis.pid when daemonized.
daemonize no
# When running daemonized, Redis writes a pid file in </span>/var/run/<span>redis.pid by
# </span><span>default</span><span>. You can specify a custom pid file location here.
pidfile </span>/var/run/<span>redis.pid
# Accept connections on the specified port, </span><span>default</span> is 6379<span>.
# If port </span>0<span> is specified Redis will not listen on a TCP socket.
port </span>6379<span>
# If you want you can bind a single </span><span>interface</span>, <span>if</span><span> the bind option is not
# specified all the interfaces will listen </span><span>for</span><span> incoming connections.
#
# bind </span>127.0.0.1<span>
# Specify the path </span><span>for</span> the unix socket that will be used to listen <span>for</span><span>
# incoming connections. There is no </span><span>default</span><span>, so Redis will not listen
# on a unix socket when not specified.
#
# unixsocket </span>/tmp/<span>redis.sock
# unixsocketperm </span>755<span>
# Close the connection after a client is idle </span><span>for</span> N seconds (0<span> to disable)
timeout </span>0<span>
# Set server verbosity to </span>‘debug‘<span>
# it can be one of:
# debug (a lot of information, useful </span><span>for</span> development/<span>testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important </span>/<span> critical messages are logged)
loglevel verbose
# Specify the log file name. Also </span>‘stdout‘<span> can be used to force
# Redis to log on the standard output. Note that </span><span>if</span><span> you use standard
# output </span><span>for</span> logging but daemonize, logs will be sent to /dev/<span>null</span><span>
logfile stdout
# To enable logging to the system logger, just set </span>‘syslog-enabled‘<span> to yes,
# and optionally update the other syslog parameters to suit your needs.
# syslog</span>-<span>enabled no
# Specify the syslog identity.
# syslog</span>-<span>ident redis
# Specify the syslog facility. Must be USER or between LOCAL0</span>-<span>LOCAL7.
# syslog</span>-<span>facility local0
# Set the number of databases. The </span><span>default</span> database is DB 0<span>, you can select
# a different one on a per</span>-connection basis using SELECT <dbid><span> where
# dbid is a number between </span>0 and ‘databases‘-1<span>
databases </span>16<span>
################################ SNAPSHOTTING #################################
#
# Save the DB on disk:
#
# save </span><seconds> <changes><span>
#
# Will save the DB </span><span>if</span><span> both the given number of seconds and the given
# number of write operations against the DB occurred.
#
# In the example below the behaviour will be to save:
# after </span>900 sec (15 min) <span>if</span> at least 1<span> key changed
# after </span>300 sec (5 min) <span>if</span> at least 10<span> keys changed
# after </span>60 sec <span>if</span> at least 10000<span> keys changed
#
# Note: you can disable saving at all commenting all the </span>"save"<span> lines.
save </span>900 1<span>
save </span>300 10<span>
save </span>60 10000<span>
# Compress string objects using LZF when dump .rdb databases</span>?<span>
# For </span><span>default</span> that‘s set to ‘yes‘ as it‘<span>s almost always a win.
# If you want to save some CPU in the saving child set it to </span>‘no‘<span> but
# the dataset will likely be bigger </span><span>if</span><span> you have compressible values or keys.
rdbcompression yes
# The filename where to dump the DB
dbfilename dump.rdb
# The working directory.
#
# The DB will be written inside </span><span>this</span><span> directory, with the filename specified
# above using the </span>‘dbfilename‘<span> configuration directive.
#
# Also the Append Only File will be created inside </span><span>this</span><span> directory.
#
# Note that you must specify a directory here, not a file name.
dir .</span>/<span>
################################# REPLICATION #################################
# Master</span>-<span>Slave replication. Use slaveof to make a Redis instance a copy of
# another Redis server. Note that the configuration is local to the slave
# so </span><span>for</span><span> example it is possible to configure the slave to save the DB with a
# different interval, or to listen to another port, and so on.
#
# slaveof </span><masterip> <masterport><span>
# If the master is password </span><span>protected</span> (using the "requirepass"<span> configuration
# directive below) it is possible to tell the slave to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the slave request.
#
# masterauth </span><master-password><span>
# When a slave lost the connection with the master, or when the replication
# is still in progress, the slave can act in two different ways:
#
# </span>1) <span>if</span> slave-serve-stale-data is set to ‘yes‘ (the <span>default</span><span>) the slave will
# still reply to client requests, possibly with out of data data, or the
# data set may just be empty </span><span>if</span><span>this</span><span> is the first synchronization.
#
# </span>2) <span>if</span> slave-serve-stale data is set to ‘no‘<span> the slave will reply with
# an error </span>"SYNC with master in progress"<span> to all the kind of commands
# but to INFO and SLAVEOF.
#
slave</span>-serve-stale-<span>data yes
# Slaves send PINGs to server in a predefined interval. It</span>‘s possible to change
# <span>this</span> interval with the repl_ping_slave_period option. The <span>default</span> value is 10<span>
# seconds.
#
# repl</span>-ping-slave-period 10<span>
# The following option sets a timeout </span><span>for</span> both Bulk transfer I/<span>O timeout and
# master data or ping response timeout. The </span><span>default</span> value is 60<span> seconds.
#
# It is important to make sure that </span><span>this</span><span> value is greater than the value
# specified </span><span>for</span> repl-ping-slave-<span>period otherwise a timeout will be detected
# every time there is low traffic between the master and the slave.
#
# repl</span>-timeout 60<span>
################################## SECURITY ###################################
# Require clients to issue AUTH </span><PASSWORD><span> before processing any other
# commands. This might be useful in environments in which you </span><span>do</span><span> not trust
# others with access to the host running redis</span>-<span>server.
#
# This should stay commented out </span><span>for</span><span> backward compatibility and because most
# people </span><span>do</span><span> not need auth (e.g. they run their own servers).
#
# Warning: since Redis is pretty fast an outside user can </span><span>try</span><span> up to
# 150k passwords per second against a good box. This means that you should
# use a very strong password otherwise it will be very easy to </span><span>break</span><span>.
#
# requirepass foobared
# Command renaming.
#
# It is possilbe to change the name of dangerous commands in a shared
# environment. For instance the CONFIG command may be renamed into something
# of hard to guess so that it will be still available </span><span>for</span> internal-<span>use
# tools but not available </span><span>for</span><span> general clients.
#
# Example:
#
# rename</span>-<span>command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
#
# It is also possilbe to completely kill a command renaming it into
# an empty string:
#
# rename</span>-command CONFIG ""<span>
################################### LIMITS ####################################
# Set the max number of connected clients at the same time. By </span><span>default</span><span> there
# is no limit, and it</span>‘s up to the number of file descriptors the Redis process
# is able to open. The special value ‘0‘<span> means no limits.
# Once the limit is reached Redis will close all the </span><span>new</span><span> connections sending
# an error </span>‘max number of clients reached‘<span>.
#
# maxclients </span>128<span>
# Don</span>‘t use more memory than the specified amount of bytes.
# When the memory limit is reached Redis will <span>try</span><span> to remove keys with an
# EXPIRE set. It will </span><span>try</span><span> to start freeing keys that are going to expire
# in little time and preserve keys with a longer time to live.
# Redis will also </span><span>try</span> to remove objects from free lists <span>if</span><span> possible.
#
# If all </span><span>this</span><span> fails, Redis will start to reply with errors to commands
# that will use more memory, like SET, LPUSH, and so on, and will </span><span>continue</span><span>
# to reply to most read</span>-<span>only commands like GET.
#
# WARNING: maxmemory can be a good idea mainly </span><span>if</span><span> you want to use Redis as a
# </span>‘state‘<span> server or cache, not as a real DB. When Redis is used as a real
# database the memory usage will grow over the weeks, it will be obvious </span><span>if</span><span>
# it is going to use too much memory in the </span><span>long</span> run, and you‘ll have the time
# to upgrade. With maxmemory after the limit is reached you‘ll start to get
# errors <span>for</span> write operations, and <span>this</span><span> may even lead to DB inconsistency.
#
# maxmemory </span><bytes><span>
# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
# is reached</span>?<span> You can select among five behavior:
#
# </span><span>volatile</span>-lru -><span> remove the key with an expire set using an LRU algorithm
# allkeys</span>-lru -><span> remove any key accordingly to the LRU algorithm
# </span><span>volatile</span>-random -><span> remove a random key with an expire set
# allkeys</span>->random -><span> remove a random key, any key
# </span><span>volatile</span>-ttl -><span> remove the key with the nearest expire time (minor TTL)
# noeviction </span>-> don‘t expire at all, just return an error on write operations
<span>#
# Note: with all the kind of policies, Redis will </span><span>return</span><span> an error on write
# operations, when there are not suitable keys </span><span>for</span><span> eviction.
#
# At the date of writing </span><span>this</span><span> commands are: set setnx setex append
# incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
# sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
# zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
# getset mset msetnx exec sort
#
# The </span><span>default</span><span> is:
#
# maxmemory</span>-policy <span>volatile</span>-<span>lru
# LRU and minimal TTL algorithms are not precise algorithms but approximated
# algorithms (in order to save memory), so you can select as well the sample
# size to check. For instance </span><span>for</span><span>default</span><span> Redis will check three keys and
# pick the one that was used less recently, you can change the sample size
# using the following configuration directive.
#
# maxmemory</span>-samples 3<span>
############################## APPEND ONLY MODE ###############################
# By </span><span>default</span><span> Redis asynchronously dumps the dataset on disk. If you can live
# with the idea that the latest records will be lost </span><span>if</span><span> something like a crash
# happens </span><span>this</span><span> is the preferred way to run Redis. If instead you care a lot
# about your data and don</span>‘t want to that a single record can get lost you should
# enable the append only mode: when <span>this</span><span> mode is enabled Redis will append
# every write operation received in the file appendonly.aof. This file will
# be read on startup in order to rebuild the full dataset in memory.
#
# Note that you can have both the async dumps and the append only file </span><span>if</span><span> you
# like (you have to comment the </span>"save"<span> statements above to disable the dumps).
# Still </span><span>if</span><span> append only mode is enabled Redis will load the data from the
# log file at startup ignoring the dump.rdb file.
#
# IMPORTANT: Check the BGREWRITEAOF to check how to rewrite the append
# log file in background when it gets too big.
appendonly no
# The name of the append only file (</span><span>default</span>: "appendonly.aof"<span>)
# appendfilename appendonly.aof
# The fsync() call tells the Operating System to actually write data on disk
# instead to wait </span><span>for</span><span> more data in the output buffer. Some OS will really flush
# data on disk, some other OS will just </span><span>try</span> to <span>do</span><span> it ASAP.
#
# Redis supports three different modes:
#
# no: don</span>‘t fsync, just let the OS flush the data when it wants. Faster.
<span># always: fsync after every write to the append only log . Slow, Safest.
# everysec: fsync only </span><span>if</span><span> one second passed since the last fsync. Compromise.
#
# The </span><span>default</span> is "everysec" that‘s usually the right compromise between
# speed and data safety. It‘s up to you to understand if you can relax this to
# "no"<span> that will will let the operating system flush the output buffer when
# it wants, </span><span>for</span> better performances (but <span>if</span><span> you can live with the idea of
# some data loss consider the </span><span>default</span> persistence mode that‘s snapshotting),
# or on the contrary, use "always" that‘s very slow but a bit safer than
<span># everysec.
#
# If unsure, use </span>"everysec"<span>.
# appendfsync always
appendfsync everysec
# appendfsync no
# When the AOF fsync policy is set to always or everysec, and a background
# saving process (a background save or AOF log background rewriting) is
# performing a lot of I</span>/<span>O against the disk, in some Linux configurations
# Redis may block too </span><span>long</span> on the fsync() call. Note that there is no fix <span>for</span><span>
# </span><span>this</span><span> currently, as even performing fsync in a different thread will block
# our synchronous write(</span>2<span>) call.
#
# In order to mitigate </span><span>this</span> problem it‘s possible to use the following option
# that will prevent fsync() from being called in the main process <span>while</span><span> a
# BGSAVE or BGREWRITEAOF is in progress.
#
# This means that </span><span>while</span><span> another child is saving the durability of Redis is
# the same as </span>"appendfsync none"<span>, that in pratical terms means that it is
# possible to lost up to </span>30<span> seconds of log in the worst scenario (with the
# </span><span>default</span><span> Linux settings).
#
# If you have latency problems turn </span><span>this</span> to "yes"<span>. Otherwise leave it as
# </span>"no"<span> that is the safest pick from the point of view of durability.
no</span>-appendfsync-on-<span>rewrite no
# Automatic rewrite of the append only file.
# Redis is able to automatically rewrite the log file implicitly calling
# BGREWRITEAOF when the AOF log size will growth by the specified percentage.
#
# This is how it works: Redis remembers the size of the AOF file after the
# latest rewrite (or </span><span>if</span><span> no rewrite happened since the restart, the size of
# the AOF at startup is used).
#
# This base size is compared to the current size. If the current size is
# bigger than the specified percentage, the rewrite is triggered. Also
# you need to specify a minimal size </span><span>for</span> the AOF file to be rewritten, <span>this</span><span>
# is useful to avoid rewriting the AOF file even </span><span>if</span><span> the percentage increase
# is reached but it is still pretty small.
#
# Specify a precentage of zero in order to disable the automatic AOF
# rewrite feature.
auto</span>-aof-rewrite-percentage 100<span>
auto</span>-aof-rewrite-min-<span>size 64mb
################################## SLOW LOG ###################################
# The Redis Slow Log is a system to log queries that exceeded a specified
# execution time. The execution time does not include the I</span>/<span>O operations
# like talking with the client, sending the reply and so forth,
# but just the time needed to actually execute the command (</span><span>this</span><span> is the only
# stage of command execution where the thread is blocked and can not serve
# other requests in the meantime).
#
# You can configure the slow log with two parameters: one tells Redis
# what is the execution time, in microseconds, to exceed in order </span><span>for</span><span> the
# command to get logged, and the other parameter is the length of the
# slow log. When a </span><span>new</span><span> command is logged the oldest one is removed from the
# queue of logged commands.
# The following time is expressed in microseconds, so </span>1000000<span> is equivalent
# to one second. Note that a negative number disables the slow log, </span><span>while</span><span>
# a value of zero forces the logging of every command.
slowlog</span>-log-slower-than 10000<span>
# There is no limit to </span><span>this</span><span> length. Just be aware that it will consume memory.
# You can reclaim memory used by the slow log with SLOWLOG RESET.
slowlog</span>-max-len 1024<span>
################################ VIRTUAL MEMORY ###############################
### WARNING</span>! Virtual Memory is deprecated in Redis 2.4<span>
### The use of Virtual Memory is strongly discouraged.
### WARNING</span>! Virtual Memory is deprecated in Redis 2.4<span>
### The use of Virtual Memory is strongly discouraged.
# Virtual Memory allows Redis to work with datasets bigger than the actual
# amount of RAM needed to hold the whole dataset in memory.
# In order to </span><span>do</span> so very used keys are taken in memory <span>while</span><span> the other keys
# are swapped into a swap file, similarly to what operating systems </span><span>do</span><span>
# with memory pages.
#
# To enable VM just set </span>‘vm-enabled‘<span> to yes, and set the following three
# VM parameters accordingly to your needs.
vm</span>-<span>enabled no
# vm</span>-<span>enabled yes
# This is the path of the Redis swap file. As you can guess, swap files
# can</span>‘t be shared by different Redis instances, so make sure to use a swap
# file <span>for</span> every redis process you are running. Redis will complain <span>if</span><span> the
# swap file is already in use.
#
# The best kind of storage </span><span>for</span> the Redis swap file (that‘s accessed at random)
<span># is a Solid State Disk (SSD).
#
# </span>*** WARNING *** <span>if</span> you are using a shared hosting the <span>default</span><span> of putting
# the swap file under </span>/<span>tmp is not secure. Create a dir with access granted
# only to Redis user and configure Redis to create the swap file there.
vm</span>-swap-file /tmp/<span>redis.swap
# vm</span>-max-<span>memory configures the VM to use at max the specified amount of
# RAM. Everything that deos not fit will be swapped on disk </span>*<span>if</span>*<span> possible, that
# is, </span><span>if</span><span> there is still enough contiguous space in the swap file.
#
# With vm</span>-max-memory 0<span> the system will swap everything it can. Not a good
# </span><span>default</span>, just specify the max amount of RAM you can in bytes, but it‘s
<span># better to leave some margin. For instance specify an amount of RAM
# that</span>‘s more or less between 60 and 80% of your free RAM.
vm-max-memory 0<span>
# Redis swap files is split into pages. An object can be saved using multiple
# contiguous pages, but pages can</span>‘t be shared between different objects.
# So <span>if</span><span> your page is too big, small objects swapped out on disk will waste
# a lot of space. If you page is too small, there is less space in the swap
# file (assuming you configured the same number of total swap file pages).
#
# If you use a lot of small objects, use a page size of </span>64 or 32<span> bytes.
# If you use a lot of big objects, use a bigger page size.
# If unsure, use the </span><span>default</span><span> :)
vm</span>-page-size 32<span>
# Number of total memory pages in the swap file.
# Given that the page table (a bitmap of free</span>/<span>used pages) is taken in memory,
# every </span>8 pages on disk will consume 1 <span>byte</span><span> of RAM.
#
# The total swap size is vm</span>-page-size * vm-<span>pages
#
# With the </span><span>default</span> of 32-bytes memory pages and 134217728<span> pages Redis will
# use a </span>4 GB swap file, that will use 16 MB of RAM <span>for</span><span> the page table.
#
# It</span>‘s better to use the smallest acceptable value for your application,
# but the <span>default</span><span> is large in order to work in most conditions.
vm</span>-pages 134217728<span>
# Max number of VM I</span>/<span>O threads running at the same time.
# This threads are used to read</span>/write data from/<span>to swap file, since they
# also encode and decode objects from disk to memory or the reverse, a bigger
# number of threads can help with big objects even </span><span>if</span> they can‘t help with
# I/<span>O itself as the physical device may not be able to couple with many
# reads</span>/<span>writes operations at the same time.
#
# The special value of </span>0 turn off threaded I/<span>O and enables the blocking
# Virtual Memory implementation.
vm</span>-max-threads 4<span>
############################### ADVANCED CONFIG ###############################
# Hashes are encoded in a special way (much more memory efficient) when they
# have at max a given numer of elements, and the biggest element does not
# exceed a given threshold. You can configure </span><span>this</span><span> limits with the following
# configuration directives.
hash</span>-max-zipmap-entries 512<span>
hash</span>-max-zipmap-value 64<span>
# Similarly to hashes, small lists are also encoded in a special way in order
# to save a lot of space. The special representation is only used when
# you are under the following limits:
list</span>-max-ziplist-entries 512<span>
list</span>-max-ziplist-value 64<span>
# Sets have a special encoding in just one </span><span>case</span><span>: when a set is composed
# of just strings that happens to be integers in radix </span>10<span> in the range
# of </span>64<span> bit signed integers.
# The following configuration setting sets the limit in the size of the
# set in order to use </span><span>this</span><span> special memory saving encoding.
set</span>-max-intset-entries 512<span>
# Similarly to hashes and lists, sorted sets are also specially encoded in
# order to save a lot of space. This encoding is only used when the length and
# elements of a sorted set are below the following limits:
zset</span>-max-ziplist-entries 128<span>
zset</span>-max-ziplist-value 64<span>
# Active rehashing uses </span>1 millisecond every 100<span> milliseconds of CPU time in
# order to help rehashing the main Redis hash table (the one mapping top</span>-<span>level
# keys to values). The hash table implementation redis uses (see dict.c)
# performs a lazy rehashing: the more operation you run into an hash table
# that is rhashing, the more rehashing </span>"steps" are performed, so <span>if</span><span> the
# server is idle the rehashing is never complete and some more memory is used
# by the hash table.
#
# The </span><span>default</span> is to use <span>this</span> millisecond 10<span> times every second in order to
# active rehashing the main dictionaries, freeing memory when possible.
#
# If unsure:
# use </span>"activerehashing no" <span>if</span><span> you have hard latency requirements and it is
# not a good thing in your environment that Redis can reply form time to time
# to queries with </span>2<span> milliseconds delay.
#
# use </span>"activerehashing yes" <span>if</span> you don‘t have such hard requirements but
<span># want to free memory asap when possible.
activerehashing yes
################################## INCLUDES ###################################
# Include one or more other config files here. This is useful </span><span>if</span><span> you
# have a standard template that goes to all redis server but also need
# to customize a few per</span>-<span>server settings. Include files can include
# other files, so use </span><span>this</span><span> wisely.
#
# include </span>/path/to/<span>local.conf
# include </span>/path/to/other.conf
解析过的配置文件:
<span>#是否以后台守护进程运行,默认为no, 取值yes, no
daemonize no
#pid文件存放路径
pidfile </span>/var/run/<span>redis.pid
#配置redis端口,默认6379
port </span>6379<span>
#绑定ip。默认绑定所有本机ip,一般用在服务器多ip下,可以只监听内网服务器ip,保证服务安全
bind </span>127.0.0.1<span>
#sock文件
unixsocket </span>/tmp/<span>redis.sock
#客户端超时时间,单位秒
timeout </span>300<span>
#log级别,支持四个级别,debug,notice,verbose,warning
loglevel verbose
#log文件路径
logfile
#log输出到标准设备,logs不写文件,输出到空设备,</span>/deb/<span>null</span><span>
logfile stdout
#保存快照的频率,在多长时间内执行一定数量的写操作时,保存快照的频率,可以设置多个条件。如果都注释掉,则不做内存数据持久化。如果只是把redis只用作cache,不开启持久化功能
save </span><seconds> <changes><span>
save </span>900 1<span>
#是否使用压缩
rdbcompression
#快照数据库名称
dbfilename
#数据库存放路径
dir
#redis主从 做法 在从上填上主的IP和端口号 主上不用做任何设置
slaveof </span><masterip> <masterport><span>
#主库服务器口令,如果主服务器未打开requirepass,则不需要此项
masterauth </span><master-password><span>
#在master服务器挂掉或者同步失败时,从服务器是否继续提供服务
slave</span>-serve-stale-<span>data yes
#设置redis服务密码,如果开启,则客户端连接时需要 </span>-<span>a 指定密码,否则操作会提示无权限
requirepass foobared
#命令改名,相当于linux alias,可以用改功能屏蔽一些危险命令
rename</span>-<span>command
#最大连接数;</span>0<span> 表示不限制
maxclients </span>128<span>
#最大使用内存(分配的内存),推荐生产环境下做相应调整,我们用的是只用来做高速缓存,限制2G。默认情况下,redis会占用可用的所有内存
maxmemory </span><bytes><span>
#过期策略,提供六种策略
maxmemory</span>-policy <span>volatile</span>-<span>lru
</span><span>volatile</span>-lru <span>//</span><span>删除过期和lru 的key(默认值) </span>
allkeys-lru <span>//</span><span>删除lru算法的key </span><span>volatile</span>-random <span>//</span><span>随机删除即将过期key </span>
allkeys->random <span>//</span><span>随机删除 </span><span>volatile</span>-ttl <span>//</span><span>删除即将过期的 </span>
noeviction <span>//</span><span>永不过期,返回错误 </span><span>
#是否开启appendonlylog,开启的话每次写操作会记一条log。相当于mysql的binlog;不同的是,每次redis启动都会读此文件构建完整数据。即使删除rdb文件,数据也是安全的
appendonly
#日志文件的名称,默认appendonly.aof
appendfilename appendonly.aof
#异步写append file 的策略。类似mysql事物log写方式。三种
appendfsync
appendfsync always </span><span>//</span><span>同步,每次写都要flush到磁盘,安全,速度慢。 </span>
appendfsync everysec <span>//</span><span>每秒写(默认值,推荐值)同mysql </span>
appendfsync no <span>//</span><span>交给操作系统去做flush的动作 </span><span>
#虚拟内存开关
vm</span>-<span>enabled no
#swap文件,不同redis swap文件不能共享。而且生产环境下,不建议放在tmp目录
vm</span>-swap-file /tmp/<span>redis.swap
#vm大小限制。</span>0:不限制,建议60-80%<span> 可用内存大小
vm</span>-max-memory 0<span>
#根据缓存内容大小调整,默认32字节
vm</span>-page-size 32<span>
#page数。每 </span>8 page,会占用1字节内存。vm-page-size * vm-<span>pages 等于 swap 文件大小
vm</span>-pages 134217728<span>
#vm 最大io线程数。注意: </span>0<span> 标志禁止使用vm
vm</span>-max-threads 4
原文:http://www.cnblogs.com/liuhongfeng/p/4704443.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!