Windows Redis Installation and PHP usage
I. Installing REDIS and enabling services
1 Download Redis Client
?
Http://code.google.com/p/servicestack/wiki/RedisWindowsDownload#Download_32bit_Cygwin_builds_for_Windows
?
2 Unzip to the directory you need
?
3 Creating a redis.conf file
?
?
# Redis configuration file example# By default Redis does not run as a daemon. Use ' yes ' if you need it.# Note that Redis would 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 was 6379port 6379# If you want You can bind A single interface, if the BIND option was not# specified all the interfaces would listen for connections.## bind 127.0.0.1# Close the connection after a client are 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 are want in Production probably) # warning (only very important/critical messages is logged) loglevel debug# Specify the log file na Me. Also ' stdout ' can is used to force# the demon-Log on the STANdard output. Note If you use standard# output for logging but daemonize, logs'll be sent To/dev/nulllogfile stdout# Set the num ber of databases. The default database is DB 0, you can select# a different one on a per-connection basis using Select
where# dbid is a number between 0 and ' databases ' -1databases 16################################ snapshotting ########## ######################### Save the DB on disk:## save
# # would save the DB if both the given number of seconds and the given# number of write operations against the DB OCCU rred.## in the example below the behaviour would be is to save:# after-SEC (min) If at least 1 key changed# after SEC (5 min) If at least changed# after the SEC if at least 10000 keys Changedsave 1save 10save 60 10 000# Compress String objects using LZF when dump. RDB databases?# for default this ' s set to ' yes ' as it's almost always a win.# if you want to save some CPU on the saving child set it to ' No ' but# the dataset would likely be bigger If you have C Ompressible values or keys.rdbcompression yes# the filename where to dump the Dbdbfilename dump.rdb# for default save/load DB In/from The working directory# Note that's 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 was local to the slave# so for example it was possible to configure the slave to save the D B with a# different interval, or to listen to another ports, and so on.## slaveof
# If The master is password protected (using the "Requirepass" configuration# directive below) It's possible to tell the Slave to authenticate before# starting the replication synchronization process, otherwise the master will# refuse the Slav E request.## Masterauth
################################## SECURITY #################################### Require clients to issue AUTH
Before processing any other# commands. This might is useful in environments in which you does not trust# others with access to the host running redis-server.## Thi s should stay commented out for backward compatibility and because most# people does 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# are able to open. The special value ' 0 ' means no limts.# Once the limit is reached Redis would close all the new connections sending# an Erro R ' Max number of clients reached '. # maxclients 128# Don ' t use more memory than the specified amount of bytes.# when the M Emory limit is reached Redis would try to remove keys with an# EXPIRE set. It'll try to start freeing keys this is going to expire# in little time and preserve keys with a longer time to live.# Redis would also try to remove objects from free lists if possible.## if all this fails, Redis would start to reply With errors to commands# that would use the more memory, like SET, Lpush, and so on, and would continue# to the most reply Only commands like get.## Warning:maxmemory can is a good idea mainly if you want to use Redis as a# ' state ' server or CA Che, not as a real DB. When Redis is used as a real# database the memory usage would grow over the weeks, it'll be obvious if# it's going to us E Too much memory in the long run, and you'll have the time# to upgrade. With MaxMemory after the limit was reached you'll start to get# errors for write operations, and this may even leads to DB I nconsistency.## MaxMemory
############################## APPEND only MODE ################################ by default Redis asynchronously dumps T The He dataset on disk. If you can live# with the idea that the latest records would be be lost if something like a crash# happens this is the Preferr Ed to run Redis. If instead a lot# about your data and don ' t want to that a single record can get lost you should# enable the AppE nd only mode:when This mode is enabled Redis would append# every write operation received in the file Appendonly.log. This file will# is read on startup in order to rebuild the full dataset in memory.## Note so 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). # Stil L If append only mode is enabled Redis would 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 if it gets too big.appendonly no# the Fsync () call tells the Operating System to Actua Lly write data on disk# instead to wait for more data in the output buffer. Some OS would really flush# data on disk, Some other OS would 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 your to# understand if you can relax this to "everysec" that'll fsync every second# or to "no" that would let T He operating system flush the output buffer when# it want, for better performances (and if you can live with the idea of# Some data loss consider the default persistence mode that ' s snapshotting). Appendfsync always# appendfsync everysec# Append Fsync no############################### Advanced CONFIG ################################ Glue Small output buffers together in order to send small replie s in a# single TCP packet. Uses a bit more CPUs but most of the times it's a win# in terms of number of queries per second. Use the ' yes ' if UNSURE.GLUEOUTPUTBUF yes# use object sharing. Can save a lot of memory if you had many common# string in your datasets, but performs lookups against the shared objects# Pool so it uses more CPUs and can be a bit slower. Usually it's a good# idea.## when object sharing are enabled (Shareobjects Yes) you can use# shareobjectspoolsize to contro L The size of the pool used in order to try# object sharing. A bigger pool size would leads to better sharing capabilities.# in general you want this value to is at least the double of The number of# very common strings you had in your dataset.## warning:object sharing are experimental, and don ' t enable this feature# in production before of Redis 1.0-stable. Still try this feature in# your develOpment environment so we can test it better.# shareobjects no# shareobjectspoolsize 1024
?
? 4 start the service under the command line
? ??
Display after successful startup (this window will always scroll, do not close, or Redis will stop)
?
5 Setting up the Redis service client
?
will then show
?
6 Testing
?
Installation succeeded Under Window
?
Second, the use of PHP
1 Adding Phpredis Extensions
? First, look at the PHP compiled version v6/v9 in Phpinfo ()
?
?
2 Download Extension Address: Https://github.com/nicolasff/phpredis/downloads (note the supported PHP versions)
?
3 Put the downloaded Php_redis.dll in the PHP extension directory (EXT), and modify the configuration file PHP.ini (add Extension=php_redis.dll)
?
4 Restart the service, view Phpinfo (), the following indicates success;
?
?
5 Testing with PHP
?
$redis = new Redis (), $redis->connect ("192.168.138.2", "6379"); The PHP client sets the IP and port//store a value of $redis->set ("Say", "Hello World"), Echo $redis->get ("say"); Should output Hello world//store multiple values $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));
?
?
Linux to be in practice after the right to serve