PHP and Redis application experience sharing

Source: Internet
Author: User
Tags redis download
PHP and Redis use experience sharing
First, installation
1. Redis Download and Installation:
Reference


Mkdir/usr/local/redis
Cd/usr/local/redis
wget http://redis.googlecode.com/files/redis-2.4.2.tar.gz
Tar xzf redis-2.4.2.tar.gz
CD redis-2.4.2
Make
./src/redis-server


2. Redis Test command:
src/redis-cliredis> set foo barokredis> get foo "bar";

3. Next, Redis configuration is required:
Reference


Daemonize Yes
Pidfile/usr/local/redis/var/redis.pid
Port 6379
Timeout 300
LogLevel verbose
Logfile/usr/local/redis/var/redis.log
Databases 16
Save 900 1
Save 300 10
Save 60 10000
Rdbcompression Yes
Dbfilename Dump.rdb
Dir/usr/local/redis/var
Slave-serve-stale-data Yes
AppendOnly No
Appendfsync everysec
No-appendfsync-on-rewrite No
Vm-enabled No
Vm-swap-file/tmp/redis.swap
Vm-max-memory 0
Vm-page-size 32
Vm-pages 134217728
Vm-max-threads 4
Hash-max-zipmap-entries 512
Hash-max-zipmap-value 64
List-max-ziplist-entries 512
List-max-ziplist-value 64
Set-max-intset-entries 512
activerehashing Yes


4. Redis Chinese Configuration document:
Reference


1, whether the next process runs, the default is no, the production environment must be set to Yes, and set to start automatically
Daemonize No
2, such as the next 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, the binding host IP, the default value is 127.0.0.1 (note), the production environment is generally the internal and external network IP
Bind 127.0.0.1
5, timeout time, default is 300 (seconds)
Timeout 300
6, logging level, 4 selectable values, Debug,verbose (default), notice,warning
LogLevel verbose
7, logging mode, 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, indicates how many times the update operation, the data is synchronized to the data file. This can be a combination of multiple conditions, such as the default configuration file settings, set three conditions.
At least 1 keys are changed in 900 seconds (15 minutes)
Save 900 1
At least 10 keys are changed in 300 seconds (5 minutes)
Save 300 10
10, whether to compress data when storing to local database, default is yes
Rdbcompression Yes
11, local database file name, default value is Dump.rdb
Dbfilename/root/redis_db/dump.rdb
12, the local database holds the path, the default value is.
dir/root/redis_db/
13. Set the IP and port (note) of the main service when the machine is from service
Slaveof
14, when this machine is from the service, set the connection password (note) of the master service
Masterauth
15, Connection password (note)
Requirepass foobared
16, maximum number of client connections, no Limit by default (note)
MaxClients 128
17, set the maximum memory, after the maximum memory settings, Redis will first try to clear the expired or expiring key, when this method processing, any maximum memory settings, will no longer write operations. Comments
MaxMemory
18, whether logging after each update operation, if not turned on, may result in loss of data over a period of time when power is lost. Because the Redis itself synchronizes data files in sync with the save conditions above, some data will only exist in memory for a period of time. Default value is no
AppendOnly Yes
19, update log file name, default value is appendonly.aof (note)
Appendfilename/root/redis_db/appendonly.aof
20, update the log conditions, a total of 3 optional values. No indicates that the data cache of the operating system is synchronized to disk, always means that the data is written to disk manually after each update operation, and EVERYSEC is synchronized once per second (the default value).
Appendfsync everysec
21, whether to use virtual memory, the default value is no
vm-enabled Yes
22, virtual memory file path, default value of/tmp/redis.swap, cannot be shared by multiple Redis instances
Vm-swap-file/tmp/redis.swap
23, all data greater than vm-max-memory into virtual memory, regardless of the vm-max-memory settings, all index data is memory storage (REDIS index data is keys), that is, when the vm-max-memory is set to 0, In fact, all value is present on disk. The default value is 0.
Vm-max-memory 0
24, virtual memory files in block storage, 32bytes per block
Vm-page-size 32
25, the maximum number of virtual intrinsic 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 number, if set to 0, then all the operation of the swap file is serial. This can result in a long delay, but with good assurance of data integrity.
Vm-max-threads 4
27, the small output cache is put together, in order to be able to send multiple responses to the client in a TCP packet, the specific principle and the actual effect I am not very clear. So according to the note, you're not sure when you set it to Yes.
Glueoutputbuf Yes
28, a hash data structure was introduced in Redis 2.0. When a hash contains more than the specified number of elements and the largest element does not exceed the critical value, the hash is stored in a special encoding method (greatly reducing memory usage), where these two thresholds can be set
Hash-max-zipmap-entries 64
The maximum value of an element in the 29,hash
Hash-max-zipmap-value 512
30, when turned on, Redis will use 1 milliseconds of CPU time every 100 milliseconds to hash the Redis hash table, which can reduce the use of memory. When you have a very strict real-time requirement in your usage scenario, you cannot accept Redis's occasional 2 millisecond delay on requests, and configure this as No. If you don't have such strict real-time requirements, you can set it to Yes so that you can free up memory as quickly as possible
activerehashing Yes


5, Php-redis module installation:
If you do not have phpize on your machine, you will need to install the Php-devel package first.
sudo apt-get install Php-devel

Reference


Cd/usr/local/src
wget--no-check-certificate Http://github.com/owlient/phpredis/tarball/master-O phpredis.tar.gz
Tar zxvf phpredis.tar.gz
CD phpredis*
Phpize
./configure--with-php-config=/usr/local/php/bin/php-config
Make
sudo make install


Restart the Web service after installation is complete
6. Redis Interface Management Tools
A PHP version of the Redis Web visualization management software
Here you can download https://github.com/ErikDubbelboer/phpRedisAdmin/downloads
In the Web directory, you can access both
7, the use of PHP code
$redis = new Redis (), $redis->connect (' 127.0.0.1 ', 6379), $redis->set (' key ', ' Val '), Echo $redis->get (' key ');
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.