·在你的计算机上安装/配置了MySQL,若是没有,请转到此处:如何在Ubuntu 16.04上安装MySQLphp
sudo apt update java
sudo apt install redis-servermysql
sudo vi /etc/redis/redis.conf复制代码
################################# GENERAL ##################################### redis
# By default Redis does not run as a daemon. Use 'yes' if you need it. sql
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized. 数据库
daemonize yes 缓存
# If you run Redis from upstart or systemd, Redis can interact with your 安全
# supervision tree. Options: bash
# supervised no - no supervision interaction 服务器
# supervised upstart - signal upstart by putting Redis into SIGSTOP mode
# supervised systemd - signal systemd by writing READY=1 to $NOTIFY_SOCKET
# supervised auto - detect upstart or systemd method based on
# UPSTART_JOB or NOTIFY_SOCKET environment variables
# Note: these supervision methods only signal "process is ready."
# They do not enable continuous liveness pings back to your supervisor.
supervised systemd
# If a pid file is specified, Redis writes it where specified at startup
# and removes it at exit.
#
# When the server runs non daemonized, no pid file is created if none is
# specified in the configuration. When the server is daemonized, the pid file
# is used even if not specified, defaulting to "/var/run/redis.pid".
#
# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
pidfile /var/run/redis/redis-server.pid
# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# 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). # # Warning: since Redis is pretty fast an outside user can try up to # 150k passwords per second against a good box. This means that you should # use a very strong password otherwise it will be very easy to break. requirepass yourpasswordhere # Command renaming. # # It is possible to change the name of dangerous commands in a shared # environment. For instance the CONFIG command may be renamed into something # hard to guess so that it will still be available for internal-use tools # but not available for general clients. # # Example: # # rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52复制代码
sudo systemctl restart redis.service复制代码
·运行如下命令以安装PHPRedis扩展名:
sudo apt-get install php-redis复制代码
·将如下行添加到你的php.ini文件中:
extension=redis.so复制代码
·仅使用MySQL:
·使用MySQL和Redis:
o请记下这样作所花费的时间应使用时间样原本处理
n个此类请求,同时将n逐渐增长为一、十、100、1000、10000、100000、1000000、1000000
<?php $con = mysqli_connect("localhost","root","admin","blog_db"); for($i = 1; $i <= 10000000; $i = $i *10) { $startTime = microtime(true); for($j = 1; $j <= $i; $j++) { $rand = rand(1, 100000); $sql = "SELECT VALUE from data WHERE `key` = $rand"; if (!mysqli_query($con, $sql)) { echo "Error: " . $sql . "" . mysqli_error($con); } } $endTime = microtime(true); file_put_contents('/home/ayush/Desktop/temp/blog/mysqlonly.log', $i . ',' . ($endTime - $startTime) . "\n" , FILE_APPEND); }复制代码
<?php $con = mysqli_connect("localhost","root","admin","blog_db"); $client = new Redis(); $client->connect('localhost'); for($i = 1; $i <= 10000000; $i = $i *10) { $startTime = microtime(true); for($j = 1; $j <= $i; $j++) { $rand = rand(1, 100000); if(!$client->exists($rand)) { $client->set($rand, $rand); $sql = "SELECT VALUE from data WHERE `key` = $rand"; if (!mysqli_query($con, $sql)) { echo "Error: " . $sql . "" . mysqli_error($con); } } } $endTime = microtime(true); file_put_contents('/home/ayush/Desktop/temp/blog/redis.log', $i . ',' . ($endTime - $startTime) . "\n" , FILE_APPEND); $client->flushAll(); }复制代码
Redis的性能开始显着提升。 所以,若是处理的请求数量很大,则将Redis这样的缓存引擎与数据库一块儿使用是一个好主意。
抽丝剥茧。细说架构那些事 优锐课