redis的简单使用实例

一、实验环境: win10 + redis3.2 + php7
二、php-redis / redis /redis;
三、redis常用的五种数据类型,不做详细说明
四、php + mysql + redis 简单应用

数据库名称:redis 数据表:redis_user
模拟 php 操作Mysql + redis 的 CURD 操作

1、config.php配置文件

<?php
$config = array(
    'mysql'=>array(
        'host'=>'127.0.0.1',
        'user'=>'root',
        'pass'=>'root',
        'dbname'=>'redis',
        'prefix'=>'redis_'
    ),
    'redis'=>array(
        'host'=>'127.0.0.1',
        'port'=>6379
    )
);

index.php 入口文件,操作Mysql时请使用主键ID

<?php
reqiure_once('Mysql.php');

//增加数据
// echo Mysql::getInstance()->table('user')->insert(['user'=>'张三','pass'=>md5('123456'),'create_time'=>date('Y-m-d H:i:s')]);

//删除数据
// echo Mysql::getInstance()->table('user')->where(array('id'=>30))->delete();

//查看单条数据
// $data = Mysql::getInstance()->table('user')->where(array('id'=>'30'))->find();
// print_r($data);

//查找所有数据
// $all = Mysql::getInstance()->table('user')->field('id,user')->select();
$all = Mysql::getInstance()->table('user')->select();
print_r($all);

//修改数据
// echo Mysql::getInstance()->table('user')->where(array('id'=>30))->update(['user'=>'张三adfadfasdf11111111','pass'=>md5('123456aaa')]);
?>

图片描述
图片描述
图片描述

Mysql.php 数据库以及redis操作文件

<?php
class Mysql
{
    static  $instance;
    private $conn;
    private $redis;
    private $options = array();


    //单例模式
    private function __construct()
    {
        error_reporting(E_ALL^E_NOTICE^E_WARNING);
        require_once(__DIR__.'/Config.php');
        $conn = mysqli_connect($config['mysql']['host'],$config['mysql']['user'],$config['mysql']['pass'],$config['mysql']['dbname']);
        if(mysqli_connect_errno($conn)) {
            echo "连接Mysql失败:".mysqli_connect_error();
            exit;
        }
        mysqli_query($conn,'set names utf8');
        $this->options['prefix'] = $config['mysql']['prefix'];
        $this->conn = $conn;
        $this->redis = new Redis();
        $this->redis->connect($config['redis']['host'],$config['redis']['port']);

猜你喜欢

转载自blog.csdn.net/gp_666/article/details/80150841