天天看点

Windows下redis 安装与PHP使用

一、 安装redis及启用服务

1 下载redis客户端

2 解压到你所需要的目录中

3 创建redis.conf文件

redis.conf代码  

# redis configuration file example  

# by default redis does not run as a daemon. use ‘yes‘ if you need it.  

# note that redis will write a pid file in /var/run/redis.pid when daemonized.  

daemonize no  

# when run as a daemon, redis write a pid file in /var/run/redis.pid by default.  

# you can specify a custom pid file location here.  

pidfile /var/run/redis.pid  

# accept connections on the specified port, default is 6379  

port 6379  

# if you want you can bind a single interface, if the bind option is not  

# specified all the interfaces will listen for connections.  

#  

# bind 127.0.0.1  

# close the connection after a client is idle for n seconds (0 to disable)  

timeout 300  

# set server verbosity to ‘debug‘  

# it can be one of:  

# debug (a lot of information, useful for development/testing)  

# notice (moderately verbose, what you want in production probably)  

# warning (only very important / critical messages are logged)  

loglevel debug  

# specify the log file name. also ‘stdout‘ can be used to force  

# the demon to log on the standard output. note that if you use standard  

# output for logging but daemonize, logs will be sent to /dev/null  

logfile stdout  

# set the number of databases. the default database is db 0, you can select  

# a different one on a per-connection basis using select <dbid> where  

# dbid is a number between 0 and ‘databases‘-1  

databases 16  

################################ snapshotting  #################################  

# save the db on disk:  

#   save <seconds> <changes>  

#   will save the db if 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 900 sec (15 min) if at least 1 key changed  

#   after 300 sec (5 min) if at least 10 keys changed  

#   after 60 sec if at least 10000 keys changed  

save 900 1  

save 300 10  

save 60 10000  

# compress string objects using lzf when dump .rdb databases?  

# for default that‘s set to ‘yes‘ as it‘s almost always a win.  

# if you want to save some cpu in the saving child set it to ‘no‘ but  

# the dataset will likely be bigger if you have compressible values or keys.  

rdbcompression yes  

# the filename where to dump the db  

dbfilename dump.rdb  

# for default save/load db in/from the working directory  

# note that you must specify a directory not a file name.  

dir ./  

################################# replication #################################  

# master-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 for 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 <masterip> <masterport>  

# if the master is password protected (using the "requirepass" 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 <master-password>  

################################## security ###################################  

# require clients to issue auth <password> before processing any other  

# commands.  this might be useful in environments in which you do not trust  

# others with access to the host running redis-server.  

# this should stay commented out for backward compatibility and because most  

# people do not need auth (e.g. they run their own servers).  

# requirepass foobared  

################################### limits ####################################  

# set the max number of connected clients at the same time. by default there  

# is no limit, and it‘s up to the number of file descriptors the redis process  

# is able to open. the special value ‘0‘ means no limts.  

# once the limit is reached redis will close all the new connections sending  

# an error ‘max number of clients reached‘.  

# maxclients 128  

# don‘t use more memory than the specified amount of bytes.  

# when the memory limit is reached redis will try to remove keys with an  

# expire set. it will try to start freeing keys that are going to expire  

# in little time and preserve keys with a longer time to live.  

# redis will also try to remove objects from free lists if possible.  

# if all this fails, redis will start to reply with errors to commands  

# that will use more memory, like set, lpush, and so on, and will continue  

# to reply to most read-only commands like get.  

# warning: maxmemory can be a good idea mainly if you want to use redis as a  

# ‘state‘ 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 if  

# it is going to use too much memory in the long run, and you‘ll have the time  

# to upgrade. with maxmemory after the limit is reached you‘ll start to get  

# errors for write operations, and this may even lead to db inconsistency.  

# maxmemory <bytes>  

############################## append only mode ###############################  

# by default redis asynchronously dumps the dataset on disk. if you can live  

# with the idea that the latest records will be lost if something like a crash  

# happens this is the preferred way to run redis. if instead you care a lot  

# about your data and don‘t want to that a single record can get lost you should  

# enable the append only mode: when this mode is enabled redis will append  

# every write operation received in the file appendonly.log. 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 if you  

# like (you have to comment the "save" statements above to disable the dumps).  

# still if append only mode is enabled redis will load the data from the  

# log file at startup ignoring the dump.rdb file.  

# the name of the append only file is "appendonly.log"  

# important: check the bgrewriteaof to check how to rewrite the append  

# log file in background when it gets too big.  

appendonly no  

# the fsync() call tells the operating system to actually write data on disk  

# instead to wait for more data in the output buffer. some os will really flush  

# data on disk, some other os will just try to do it asap.  

# redis supports three different modes:  

# no: don‘t fsync, just let the os flush the data when it wants. faster.  

# always: fsync after every write to the append only log . slow, safest.  

# everysec: fsync only if one second passed since the last fsync. compromise.  

# the default is "always" that‘s the safer of the options. it‘s up to you to  

# understand if you can relax this to "everysec" that will fsync every second  

# or to "no" that will let the operating system flush the output buffer when  

# it want, for better performances (but if you can live with the idea of  

# some data loss consider the default persistence mode that‘s snapshotting).  

appendfsync always  

# appendfsync everysec  

# appendfsync no  

############################### advanced config ###############################  

# glue small output buffers together in order to send small replies in a  

# single tcp packet. uses a bit more cpu but most of the times it is a win  

# in terms of number of queries per second. use ‘yes‘ if unsure.  

glueoutputbuf yes  

# use object sharing. can save a lot of memory if you have many common  

# string in your dataset, but performs lookups against the shared objects  

# pool so it uses more cpu and can be a bit slower. usually it‘s a good  

# idea.  

# when object sharing is enabled (shareobjects yes) you can use  

# shareobjectspoolsize to control the size of the pool used in order to try  

# object sharing. a bigger pool size will lead to better sharing capabilities.  

# in general you want this value to be at least the double of the number of  

# very common strings you have in your dataset.  

# warning: object sharing is experimental, don‘t enable this feature  

# in production before of redis 1.0-stable. still please try this feature in  

# your development environment so that we can test it better.  

# shareobjects no  

# shareobjectspoolsize 1024  

 4 在命令行下启动服务

Windows下redis 安装与PHP使用

启动成功后显示(此窗口会一直滚动,不要关闭,否则redis就停止了)

Windows下redis 安装与PHP使用

5 设置redis服务客户端

Windows下redis 安装与PHP使用

之后会显示

Windows下redis 安装与PHP使用

6 测试

Windows下redis 安装与PHP使用

window下安装成功

二、php中使用

1 添加phpredis扩展

 首先,查看所用php编译版本v6/v9 在phpinfo()中查看

Windows下redis 安装与PHP使用

2 下载扩展 地址:(注意所支持的php版本)

3

将下载的php_redis.dll放在php扩展目录中(ext),并修改配置文件php.ini(添加extension=php_redis.dll)

4 重新启动服务,查看phpinfo(),下面表示成功;

Windows下redis 安装与PHP使用

5 用php测试

php代码  

$redis = new redis();  

$redis->connect("192.168.138.2","6379");  //php客户端设置的ip及端口  

//存储一个 值  

$redis->set("say","hello world");  

echo $redis->get("say");     //应输出hello world  

//存储多个值  

$array = array(‘first_key‘=>‘first_val‘,  

          ‘second_key‘=>‘second_val‘,  

          ‘third_key‘=>‘third_val‘);  

$array_get = array(‘first_key‘,‘second_key‘,‘third_key‘);  

$redis->mset($array);  

var_dump($redis->mget($array_get));