Redis complete Linux environment under configuration and demo

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43489208/article/details/102738616

1.redis no memory space is used, the data read speed faster than the database, but expensive, so need to use the right place
2. redis set up under Linux

//安装redis步骤
//推荐进入到linux路径/usr/local/src
$ wget http://download.redis.io/releases/redis-4.0.10.tar.gz
$ tar xzf redis-4.0.10.tar.gz 
$ cd redis-4.0.10/ 
$ make

Installation, start-up test

//二进制文件是编译完成后在src目录下,通过下面的命令启动Redis服务
$ src/redis-server
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"

Here will complain when the configuration is less than 128 and then go to the configuration file which opened 1024

//移动redis的配置文件
//1、在etc里面建立一个文件夹
   mkdir  /etc/redis
   //注意:权限问题777
//2、移动配置文件到新建的文件夹下
   cp  redis.conf  /etc/redis/
//3、杀死redis并重新后台开启redis
	pkill -9 redis-server
	src/redis-server /etc/redis/redis.conf
//4、检测redis是否开启
	ps axu | grep redis-server
	//如果是最新时间开启的redis,则表明开启成功
//5、客户端远程通过ip连接redis
	src/redis-cli -h 192.168.1.81 -p 6379
//6、如果出现如下,则表明连接成功
	192.168.1.81:6379>

Then install php-redis

$ wget https://github.com/phpredis/phpredis/archive/3.1.5.tar.gz
$ tar -zxf 3.1.5.tar.gz
$ cd phpredis-3.1.5                      # 进入 phpredis 目录

$ /usr/local/php/bin/phpize              # php安装后的路径
$ ./configure --with-php-config=/usr/local/php/bin/php-config
$ make && make install

Here will complain no php-devel, and then install

yum install php-devel

Modify the php.ini file add the following:

extension=redis.so

Then open your phpinfo (), look at whether there is redis configuration, and remember to restart

Supplementary demo

<?php  
//实例化redis
$redis = new \Redis();
//连接redis
$redis->connect('127.0.0.1',6379);
$redis->auth('12345'); 
if($redis->get('inner')=='yes' || !$redis->get('inner')){
    //第一次进入,需要缓存
    //连接数据库进行查询
    $db = new mysqli('127.0.0.1','root','root','table');
    $sql = "select * from newsinfo";
    $res = $db->query($sql);
    while($new = mysqli_fetch_assoc($res)){
        $news[] = $new;
    }
     //将数据存入redis的list中
    $json=json_encode($news);
    $redis->del('news');//把键值删除,防止重复
    $redis->lPush('news', $json);
    $redis->set('inner', 'no',60); //设置键值有效期为60秒
}else{
    //从redis中取出数据
    $json=$redis->lRange('news', 0, -1);
    $news=json_decode($json[0],true);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>redis缓存实例</title>
</head>
<body>
    <?php foreach ($news as $k => $v) {  ?>
        <li><?php  echo $v['title'];  ?></li>
    <?php } ?>
</body>
</html>

Guess you like

Origin blog.csdn.net/qq_43489208/article/details/102738616