Redis is an open source, support network, memory based, key value pair storage database, written using ANSI C. Its development is hosted by VMware. According to db-engines.com data from the monthly ranking website, Redis is the most popular key value pair storage database.
Download and installation of Redis:
@ubuntu:/$ Mkdir/usr/local/redis
@ubuntu:/$ Cd/usr/local/redis
@ubuntu:/$ wget http://redis.googlecode.com/files/redis-2.4.2.tar.gz
@ubuntu:/$ tar xzf redis-2.4.2.tar.gz
@ubuntu:/$ CD redis-2.4.2
@ubuntu:/$ Make
@ubuntu:/$ src/redis-server
Redis Test command:
@ubuntu:/$ src/redis-cli
@ubuntu:/$ redis> set foo bar
@ubuntu:/$ OK
@ubuntu:/$ redis> get foo
@ubuntu:/$ "Bar";
Installed Redis server-side program, now to use Redis in the project, because our background is written in PHP, so we need to use the Redis PHP client. There are several types of PHP clients:
Predis★repository Jol1hahn
Mature and supported
Phpredis★repository YOWGI
This is a client written in C as a PHP module.
Rediska Repository Homepage Shumkov
Redisent Repository Justinpoliey
Iredis Repository
Dhorrigan
Here I choose Phpredis (c developed Expansion pack) this PHP extension.
@ubuntu:/$ wget https://github.com/owlient/phpredis/archive/master.zip;
@ubuntu:/$ unzip Master.zip;
@ubuntu:/$ CD phpredis-master/
@ubuntu:/$/usr/local/php/bin/phpize
@ubuntu:/$/configure--with-php-config=/usr/local/php/bin/php-config
@ubuntu:/$ Make
@ubuntu:/$ make Install
Note: When using C to develop PHP extensions, if you use dynamic link library to compile the extension module, you need to use phpize, the tool in the use of Apt-get install PHP5 default is not installed, installation phpize:apt-get install Php5-dev.
To modify a configuration file:
modifying php.ini files
Add the following line to the php.ini:
Extension=redis.so
After restarting the nginx, it takes effect.
PHP Test:
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379);
$redis->set (' Test ', ' Hello world! ');
echo $redis->get (' Test ');
Finally open Redis Service: Redis-server or redis-server/etc/redis.conf (background run),
If you do not open a service, you may receive the uncaught exception ' redisexception ' with message ' Redis server went away '? "Error
Redis Chinese Configuration Document:
1, whether the next process run, the default is no, the production environment must be set to Yes and set to start automatically
Daemonize No
2, such as the next stage of the process to run, you need to specify a PID, the default is/var/run/redis.pid
Pidfile/var/run/redis.pid
3, listening port, default is 6379
Port 6379
4, binding host IP, the default value is 127.0.0.1 (note), the production environment is generally an intranet IP
Bind 127.0.0.1
5, timeout, default is 300 (SEC)
Timeout 300
6, logging level, there are 4 optional values, Debug,verbose (default value), notice,warning
LogLevel verbose
7, logging mode, the default value is stdout
LogFile stdout
8, the number of available databases, the default value is 16, the default database is 0
Databases 16
9, to indicate how many times the update operation will be synchronized to the data file for how long. This can be a combination of conditions, such as the default configuration file settings, set three conditions.
At least 1 keys have been changed in 900 seconds (15 minutes)
Save 900 1
At least 10 keys have been changed in 300 seconds (5 minutes)
Save 300 10
10, whether to compress data when storing to the local database, the default is Yes
Rdbcompression Yes
11, local database filename, default value is Dump.rdb
Dbfilename/root/redis_db/dump.rdb
12, the local database storage path, the default value is./
dir/root/redis_db/
13, when this machine is from the service, set the main service IP and port (note)
Slaveof
14, when this machine is from the service, set the main service connection password (note)
Masterauth
15, Connection password (note)
Requirepass foobared
16, maximum number of client connections, default unrestricted (note)
MaxClients 128
17, set the maximum memory, the maximum memory settings, Redis will first attempt to clear the expired or expiring key, when this method is processed, the maximum memory settings, will no longer write operations. Comments
MaxMemory
18, whether logging after each update operation, if not turned on, may cause a loss of data over time during a power outage. Because the Redis itself synchronizes data files according to the above save condition, some data will only exist in memory for a period of time. The default value is no
AppendOnly Yes
19, update the log file name, the default value is appendonly.aof (note)
Appendfilename/root/redis_db/appendonly.aof
20, update the log condition, there are 3 optional values. No means that the operating system synchronizes the data cache to disk, and always indicates that Fsync () is manually invoked after each update operation to write the data to disk, Everysec is synchronized every second (the default).
Appendfsync everysec
21, whether to use virtual memory, the default value is no
vm-enabled Yes
22, virtual memory file path, default value is/tmp/redis.swap, not multiple Redis instance sharing
Vm-swap-file/tmp/redis.swap
23, all the data greater than vm-max-memory into virtual memory, no matter how small the vm-max-memory settings, all the index data is memory storage (Redis index data is the keys), that is, when the vm-max-memory set to 0, In fact, all of the value exists on the disk. The default value is 0.
Vm-max-memory 0
24, the virtual memory file in block storage, 32bytes per block
Vm-page-size 32
25, the maximum number of virtual internal files
Vm-pages 134217728
26, you can set the number of threads to access the swap file, set the best not to exceed the machine's core, if set to 0, then all the operation of the swap file is serial. It may cause a long delay, but there is a good guarantee of data integrity.
Vm-max-threads 4
27, the small output cache together, so that can be in a TCP packet for the client to send multiple responses, the specific principle and the real effect I am not very clear. So according to the notes, you're not sure when you set it to Yes.
Glueoutputbuf Yes
28, the hash data structure was introduced in Redis 2.0. When the hash contains more than the specified number of elements and the largest element does not exceed the critical value, the hash will be stored in a special encoding (greatly reducing memory usage), where the two thresholds can be set
Hash-max-zipmap-entries 64
The maximum value of an element in a 29,hash
Hash-max-zipmap-value 512
30, after opening, Redis will use 1 milliseconds of CPU time every 100 milliseconds to hash the Redis hash table, can reduce the use of memory. When you have a very stringent real-time requirement in your usage scenario, you cannot accept Redis's 2 millisecond delay to the request, and configure this to No. If you do not have such stringent real-time requirements, you can set to Yes so that you can free up memory as quickly as possible