Getting started with php plus redis and simple applications

不言
Release: 2023-03-23 18:24:01
Original
1649 people have browsed it

The content of this article is to share with you the introduction and simple application of PHP plus redis. It has a certain reference value. Friends in need can refer to it

1. Experimental environment: win10 redis3. 2 php7
2. Installation of php-redis / redis /redis graphic management tools, etc., skip this step;
3. Five commonly used data types in redis , no detailed explanation
Reference: http://www.runoob.com/redis/r...
4. PHP mysql redis simple application
Database name :redis data table: redis_user
Simulate php operation Mysql redis CURD operation

1, config.php configuration file

<?php
$config = array(
    &#39;mysql&#39;=>array(
        &#39;host&#39;=>&#39;127.0.0.1&#39;,
        &#39;user&#39;=>&#39;root&#39;,
        &#39;pass&#39;=>&#39;root&#39;,
        &#39;dbname&#39;=>&#39;redis&#39;,
        &#39;prefix&#39;=>&#39;redis_&#39;
    ),
    &#39;redis&#39;=>array(
        &#39;host&#39;=>&#39;127.0.0.1&#39;,
        &#39;port&#39;=>6379
    )
);
Copy after login

index.php entry file, please use the primary key ID when operating Mysql

<?php
reqiure_once(&#39;Mysql.php&#39;);

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

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

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

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

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

Getting started with php plus redis and simple applications
Getting started with php plus redis and simple applications
Getting started with php plus redis and simple applications

##Mysql.php database And redis operation file

<?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__.&#39;/Config.php&#39;);
        $conn = mysqli_connect($config[&#39;mysql&#39;][&#39;host&#39;],$config[&#39;mysql&#39;][&#39;user&#39;],$config[&#39;mysql&#39;][&#39;pass&#39;],$config[&#39;mysql&#39;][&#39;dbname&#39;]);
        if(mysqli_connect_errno($conn)) {
            echo "连接Mysql失败:".mysqli_connect_error();
            exit;
        }
        mysqli_query($conn,&#39;set names utf8&#39;);
        $this->options[&#39;prefix&#39;] = $config[&#39;mysql&#39;][&#39;prefix&#39;];
        $this->conn = $conn;
        $this->redis = new Redis();
        $this->redis->connect($config[&#39;redis&#39;][&#39;host&#39;],$config[&#39;redis&#39;][&#39;port&#39;]);
    }

    //获取对象实例
    static function getInstance()
    {
        if(self::$instance) {
            return self::$instance;
        } else {
            self::$instance = new self();
            return self::$instance;
        }
    }

    //设置表名
    public function table(string $table)
    {
        $this->options[&#39;table&#39;] = &#39;`&#39;.$this->options[&#39;prefix&#39;].$table.&#39;`&#39;;
        return $this;
    }

    //设置redis键名
    public function redis(string $key)
    {
        $this->options[&#39;key&#39;] = $key;
        return $this;
    }

    //设置条件
    public function where(array $where)
    {
        $condition = &#39;&#39;;
        $and = count($where) > 1 ? &#39; and &#39; : &#39;&#39;;
        foreach ($where as $key => $value) {
            if($key == &#39;id&#39;) {$this->options[&#39;user_id&#39;] = $value;}
            if(strpos($key,&#39;:&#39;)) {
                $arr = explode(&#39;:&#39;, $key);
                $condition .= &#39;`&#39;.$arr[&#39;0&#39;].&#39;` &#39;.$arr[&#39;1&#39;]. &#39; "&#39;.$value.&#39;" &#39; . $and ; 
            } else {
                $condition .= &#39;`&#39;.$key.&#39;` = &#39; .&#39;"&#39;.$value.&#39;"&#39; .$and  ; 
            }
        }
        $this->options[&#39;where&#39;] = rtrim($condition,&#39; and &#39;);
        return $this;
    }

    //设置字段
    public function field(string $field)
    {
        $this->options[&#39;field&#39;] = $field;
        return $this;
    }
    
    //增加数据
    public function insert(array $data)
    {
        $key = &#39;`&#39;.implode(&#39;`,`&#39;, array_keys($data)).&#39;`&#39;;
        $value = &#39;"&#39;.implode(&#39;","&#39;, $data).&#39;"&#39;;
        $sql = "insert into {$this->options[&#39;table&#39;]} (".$key.") values (".$value.");";
        if( mysqli_query($this->conn,$sql) ) {
            $user_id = $this->conn->insert_id;
            $data[&#39;id&#39;] = $user_id;
            //以hash类型存储
            $this->redis->hset($this->options[&#39;table&#39;],$user_id,json_encode($data));
            return $user_id;
        } else {
            return 0;
        }
    }

    //删除数据
    public function delete()
    {
        $where = $this->options[&#39;where&#39;] ? $this->options[&#39;where&#39;] : &#39;1&#39;;
        $sql = "delete from {$this->options[&#39;table&#39;]} where {$where};";
        if(mysqli_query($this->conn,$sql)) {
            $this->redis->hdel($this->options[&#39;table&#39;],$this->options[&#39;user_id&#39;]);
            return 1;
        } else {
            return 0;
        }
    }

    //修改数据
    public function update(array $data)
    {
        $condition = &#39;&#39;;
        $where = $this->options[&#39;where&#39;] ? $this->options[&#39;where&#39;] : &#39;1&#39;;
        foreach ($data as $key => $value) {
            $condition .= ", `".$key."` = &#39;".$value."&#39;";
        }
        $condition = substr($condition, 1);
        $sql = " update {$this->options[&#39;table&#39;]} set {$condition} where {$where} ; ";
        if(mysqli_query($this->conn,$sql)) {
            $hashData = (array)json_decode($this->redis->hget($this->options[&#39;table&#39;],$this->options[&#39;user_id&#39;]));
            foreach ($data as $key => $value) {
                $hashData[$key] = $value;
            }
            $this->redis->hset($this->options[&#39;table&#39;],$this->options[&#39;user_id&#39;],json_encode($hashData));
            return 1;
        } else {
            return 0;
        }
    }


    //查找单条数据
    public function find()
    {
        $field  = $this->options[&#39;field&#39;] ? $this->options[&#39;field&#39;] : &#39;*&#39;;
        $where = $this->options[&#39;where&#39;] ? $this->options[&#39;where&#39;] : &#39;1&#39;;
        if($this->options[&#39;user_id&#39;]) {
            echo &#39;从redis获取数据<pre class="brush:php;toolbar:false">&#39;;
            $data = (array)json_decode($this->redis->hget($this->options[&#39;table&#39;],$this->options[&#39;user_id&#39;]));
            if($field != &#39;*&#39;) {
                $field = explode(&#39;,&#39;, $field);
                foreach ($field as $value) {
                    $arr[$value] = $data[$value];
                    $arr[&#39;typ&#39;] = &#39;redis&#39;;
                }
                return $arr;
                
            }
            return $data;
            
        } else {
            
            $sql = " select {$field} from {$this->options[&#39;table&#39;]} where {$where}; ";
            if($query = mysqli_query($this->conn,$sql)) {
                return mysqli_fetch_assoc($query);
            } else {
                return array();
            }
        }
    }
    //查找所有数据
    public function select()
    {
        $data = array();
        $field  = $this->options[&#39;field&#39;] ? $this->options[&#39;field&#39;] : &#39;*&#39;;
        $hashData = $this->redis->hgetall($this->options[&#39;table&#39;]);
        if($hashData) {
            if($field != &#39;*&#39;) { 
                $field = explode(&#39;,&#39;, $field);
            }
            foreach ($hashData as $key => $value) {
                $data[$key] = array();
                $values = (array)json_decode($value);
                if($field != &#39;*&#39;) {
                    foreach ($field as $k => $v) {
                        $data[$key][$v] = $values[$v];
                    }
                }else{
                    $data[$key] = $values;
                }
            }
            echo &#39;从redis获取数据<pre class="brush:php;toolbar:false">&#39;;
        } else {
            $sql = " select {$field} from {$this->options[&#39;table&#39;]} ; ";
            if($query = mysqli_query($this->conn,$sql)) {
                while ($row = mysqli_fetch_assoc($query)) {
                    $data[] = $row;
                }
            }
        }
        return $data;
    }

    

    public function __destruct()
    {
        mysqli_close($this->conn);
    }
}
Copy after login
                                                                                                                                                             

The above is the detailed content of Getting started with php plus redis and simple applications. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!