之所以要在工程中添加緩存,是因爲在互聯網項目中查詢功能是非常頻繁的,如果每次查詢都調用數據庫的話,會給數據庫造成很大的壓力,因此需要在用戶和數據庫之間加一層緩存,對於同樣的查詢,只查詢一遍數據庫,然後把數據保存到緩存當中,當其他用戶再訪問同樣的頁面時便可以直接從緩存中去讀取數據,這樣查詢效率將會提升非常多。同時也會大大減輕數據庫的壓力。
下面開始安裝Redis。
第一步:下載Redis安裝版本
這裏需要說明一點的是,新版的redis不夠穩定,建議大家還是使用舊版本的redis,比如3.0.0版本。大家可以到http://download.csdn.net/detail/u012453843/9820041這個地址下載。
第二步:將安裝包上傳到虛擬機
爲了統一管理安裝包,我一般會在虛擬機的/usr/local目錄下新建一個software目錄,如下所示。
- [root@redis ~]# mkdir /usr/local/software
- [root@redis ~]#
- [root@redis software]# ll
- 總用量 1512
- -rw-r--r--. 1 root root 1547237 4月 20 09:19 redis-3.0.0.tar.gz
- [root@redis software]#
第三步:解壓
- [root@redis software]# tar -zxvf redis-3.0.0.tar.gz -C /usr/local/
由於redis是由C語言編寫的,它的運行需要C環境,因此我們需要先安裝gcc。安裝命令是yum install gcc-c++
第五步:編譯安裝
我們到/usr/local/redis-3.0.0目錄下,要運行make和make install命令就要有Makefile這個文件,我們可以看到是有這個文件的。
- [root@redis software]# cd /usr/local/redis-3.0.0/
- [root@redis redis-3.0.0]# ll
- 總用量 204
- -rw-rw-r--. 1 root root 85775 2月 12 23:14 00-RELEASENOTES
- -rw-rw-r--. 1 root root 53 2月 12 23:14 BUGS
- -rw-rw-r--. 1 root root 1805 2月 12 23:14 CONTRIBUTING
- -rw-rw-r--. 1 root root 1487 2月 12 23:14 COPYING
- drwxrwxr-x. 7 root root 4096 2月 12 23:14 deps
- -rw-rw-r--. 1 root root 11 2月 12 23:14 INSTALL
- -rw-rw-r--. 1 root root 151 2月 12 23:14 Makefile
- -rw-rw-r--. 1 root root 4223 2月 12 23:14 MANIFESTO
- -rw-rw-r--. 1 root root 6834 2月 12 23:14 README.md
- -rw-rw-r--. 1 root root 46695 2月 12 23:14 redis.conf
- -rwxrwxr-x. 1 root root 271 2月 12 23:14 runtest
- -rwxrwxr-x. 1 root root 280 2月 12 23:14 runtest-cluster
- -rwxrwxr-x. 1 root root 281 2月 12 23:14 runtest-sentinel
- -rw-rw-r--. 1 root root 7606 2月 12 23:14 sentinel.conf
- drwxrwxr-x. 2 root root 4096 2月 12 23:14 src
- drwxrwxr-x. 10 root root 4096 2月 12 23:14 tests
- drwxrwxr-x. 7 root root 4096 2月 12 23:14 utils
- [root@redis redis-3.0.0]#
- [root@redis redis-3.0.0]# make
- [root@redis redis-3.0.0]# make install PREFIX=/usr/local/redis
- cd src && make install
- make[1]: Entering directory `/usr/local/redis-3.0.0/src'
- Hint: It's a good idea to run 'make test' ;)
- INSTALL install
- INSTALL install
- INSTALL install
- INSTALL install
- INSTALL install
- make[1]: Leaving directory `/usr/local/redis-3.0.0/src'
- [root@redis redis-3.0.0]#
- [root@redis local]# ll
- 總用量 52
- drwxr-xr-x. 2 root root 4096 9月 23 2011 bin
- drwxr-xr-x. 2 root root 4096 9月 23 2011 etc
- drwxr-xr-x. 2 root root 4096 9月 23 2011 games
- drwxr-xr-x. 2 root root 4096 9月 23 2011 include
- drwxr-xr-x. 2 root root 4096 9月 23 2011 lib
- drwxr-xr-x. 2 root root 4096 9月 23 2011 lib64
- drwxr-xr-x. 2 root root 4096 9月 23 2011 libexec
- drwxr-xr-x. 3 root root 4096 4月 20 18:20 redis
- drwxrwxr-x. 6 root root 4096 2月 12 23:14 redis-3.0.0
- drwxr-xr-x. 2 root root 4096 9月 23 2011 sbin
- drwxr-xr-x. 5 root root 4096 4月 7 01:13 share
- drwxr-xr-x. 2 root root 4096 4月 20 17:38 software
- drwxr-xr-x. 2 root root 4096 9月 23 2011 src
- [root@redis local]# cd redis
- [root@redis redis]# ll
- 總用量 4
- drwxr-xr-x. 2 root root 4096 4月 20 18:20 bin
- [root@redis redis]# cd bin
- [root@redis bin]# ll
- 總用量 26348
- -rwxr-xr-x. 1 root root 5580303 4月 20 18:20 redis-benchmark
- -rwxr-xr-x. 1 root root 22177 4月 20 18:20 redis-check-aof
- -rwxr-xr-x. 1 root root 7829962 4月 20 18:20 redis-check-rdb
- -rwxr-xr-x. 1 root root 5709171 4月 20 18:20 redis-cli
- lrwxrwxrwx. 1 root root 12 4月 20 18:20 redis-sentinel -> redis-server
- -rwxr-xr-x. 1 root root 7829962 4月 20 18:20 redis-server
- [root@redis bin]#
第一種啓動方式是直接使用./redis-server命令啓動,如下所示(可以看到默認端口是6379),只不過這種啓動會佔用終端,如果我們按Ctrl+C就會停止redis-server。
- [root@redis bin]# ./redis-server
- 15618:C 20 Apr 18:26:36.662 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
- 15618:M 20 Apr 18:26:36.664 * Increased maximum number of open files to 10032 (it was originally set to 1024).
- _._
- _.-``__ ''-._
- _.-`` `. `_. ''-._ Redis 3.2.8 (00000000/0) 64 bit
- .-`` .-```. ```\/ _.,_ ''-._
- ( ' , .-` | `, ) Running in standalone mode
- |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
- | `-._ `._ / _.-' | PID: 15618
- `-._ `-._ `-./ _.-' _.-'
- |`-._`-._ `-.__.-' _.-'_.-'|
- | `-._`-._ _.-'_.-' | http://redis.io
- `-._ `-._`-.__.-'_.-' _.-'
- |`-._`-._ `-.__.-' _.-'_.-'|
- | `-._`-._ _.-'_.-' |
- `-._ `-._`-.__.-'_.-' _.-'
- `-._ `-.__.-' _.-'
- `-._ _.-'
- `-.__.-'
- 15618:M 20 Apr 18:26:36.710 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
- 15618:M 20 Apr 18:26:36.710 # Server started, Redis version 3.2.8
- 15618:M 20 Apr 18:26:36.714 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
- 15618:M 20 Apr 18:26:36.728 * The server is now ready to accept connections on port 6379
- [root@redis redis-3.0.0]# ll
- 總用量 204
- -rw-rw-r--. 1 root root 85775 2月 12 23:14 00-RELEASENOTES
- -rw-rw-r--. 1 root root 53 2月 12 23:14 BUGS
- -rw-rw-r--. 1 root root 1805 2月 12 23:14 CONTRIBUTING
- -rw-rw-r--. 1 root root 1487 2月 12 23:14 COPYING
- drwxrwxr-x. 7 root root 4096 4月 20 18:15 deps
- -rw-rw-r--. 1 root root 11 2月 12 23:14 INSTALL
- -rw-rw-r--. 1 root root 151 2月 12 23:14 Makefile
- -rw-rw-r--. 1 root root 4223 2月 12 23:14 MANIFESTO
- -rw-rw-r--. 1 root root 6834 2月 12 23:14 README.md
- -rw-rw-r--. 1 root root 46695 2月 12 23:14 redis.conf
- -rwxrwxr-x. 1 root root 271 2月 12 23:14 runtest
- -rwxrwxr-x. 1 root root 280 2月 12 23:14 runtest-cluster
- -rwxrwxr-x. 1 root root 281 2月 12 23:14 runtest-sentinel
- -rw-rw-r--. 1 root root 7606 2月 12 23:14 sentinel.conf
- drwxrwxr-x. 2 root root 4096 4月 20 18:17 src
- drwxrwxr-x. 10 root root 4096 2月 12 23:14 tests
- drwxrwxr-x. 7 root root 4096 2月 12 23:14 utils
- [root@redis redis-3.0.0]# cp redis.conf /usr/local/redis/bin/
- [root@redis redis-3.0.0]# cd /usr/local/redis/bin
- [root@redis bin]# ll
- 總用量 26400
- -rw-r--r--. 1 root root 76 4月 20 18:30 dump.rdb
- -rwxr-xr-x. 1 root root 5580303 4月 20 18:20 redis-benchmark
- -rwxr-xr-x. 1 root root 22177 4月 20 18:20 redis-check-aof
- -rwxr-xr-x. 1 root root 7829962 4月 20 18:20 redis-check-rdb
- -rwxr-xr-x. 1 root root 5709171 4月 20 18:20 redis-cli
- -rw-r--r--. 1 root root 46695 4月 20 18:32 redis.conf
- lrwxrwxrwx. 1 root root 12 4月 20 18:20 redis-sentinel -> redis-server
- -rwxr-xr-x. 1 root root 7829962 4月 20 18:20 redis-server
- [root@redis bin]#
下面使用後臺啓動
- [root@redis bin]# ./redis-server redis.conf
- [root@redis bin]# ps -ef|grep redis
- root 17591 1 0 18:39 ? 00:00:00 ./redis-server 127.0.0.1:6379
- root 17849 1524 0 18:41 pts/0 00:00:00 grep redis
- [root@redis bin]#
使用./redis-cli連接上redis服務,然後使用ping命令,如果返回的是PONG,說明連接沒問題。
- [root@redis bin]# ./redis-cli
- 127.0.0.1:6379> ping
- PONG
- 127.0.0.1:6379>
- [root@redis bin]# ./redis-cli -h 192.168.156.11 -p 6379
- 192.168.156.11:6379> ping
- PONG
- 192.168.156.11:6379>