0

0

php 加 redis 入门及简单应用

不言

不言

发布时间:2018-04-13 16:05:52

|

1920人浏览过

|

来源于php中文网

原创

本篇文章给大家分享的内容是phpredis 入门及简单应用 ,有着一定的参考价值,有需要的朋友可以参考一下

一、实验环境: win10 + redis3.2 + php7
二、php-redis / redis /redis图形管理工个等安装,此步骤略过;
三、redis常用的五种数据类型,不做详细说明
参考:http://www.runoob.com/redis/r...
四、php + mysql + redis 简单应用
数据库名称:redis 数据表:redis_user
模拟 php 操作Mysql + redis 的 CURD 操作

1、config.php配置文件

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

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')]);
?>

php 加 redis 入门及简单应用
php 加 redis 入门及简单应用
php 加 redis 入门及简单应用

DM建站系统汽车保养维修HTML5网站模板1.5
DM建站系统汽车保养维修HTML5网站模板1.5

DM建站系统汽车保养维修HTML5网站模板,DM企业建站系统。是由php+mysql开发的一套专门用于中小企业网站建设的开源cms。DM系统的理念就是组装,把模板和区块组装起来,产生不同的网站效果。可以用来快速建设一个响应式的企业网站( PC,手机,微信都可以访问)。后台操作简单,维护方便。DM企业建站系统安装步骤:第一步,先用phpmyadmin导入sql文件。 第二步:把文件放到你的本地服务器

下载

立即学习PHP免费学习笔记(深入)”;

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

options['prefix'] = $config['mysql']['prefix'];
        $this->conn = $conn;
        $this->redis = new Redis();
        $this->redis->connect($config['redis']['host'],$config['redis']['port']);
    }

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

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

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

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

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

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

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


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

    

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

                                                  

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
php文件怎么打开
php文件怎么打开

打开php文件步骤:1、选择文本编辑器;2、在选择的文本编辑器中,创建一个新的文件,并将其保存为.php文件;3、在创建的PHP文件中,编写PHP代码;4、要在本地计算机上运行PHP文件,需要设置一个服务器环境;5、安装服务器环境后,需要将PHP文件放入服务器目录中;6、一旦将PHP文件放入服务器目录中,就可以通过浏览器来运行它。

1998

2023.09.01

php怎么取出数组的前几个元素
php怎么取出数组的前几个元素

取出php数组的前几个元素的方法有使用array_slice()函数、使用array_splice()函数、使用循环遍历、使用array_slice()函数和array_values()函数等。本专题为大家提供php数组相关的文章、下载、课程内容,供大家免费下载体验。

1324

2023.10.11

php反序列化失败怎么办
php反序列化失败怎么办

php反序列化失败的解决办法检查序列化数据。检查类定义、检查错误日志、更新PHP版本和应用安全措施等。本专题为大家提供php反序列化相关的文章、下载、课程内容,供大家免费下载体验。

1228

2023.10.11

php怎么连接mssql数据库
php怎么连接mssql数据库

连接方法:1、通过mssql_系列函数;2、通过sqlsrv_系列函数;3、通过odbc方式连接;4、通过PDO方式;5、通过COM方式连接。想了解php怎么连接mssql数据库的详细内容,可以访问下面的文章。

948

2023.10.23

php连接mssql数据库的方法
php连接mssql数据库的方法

php连接mssql数据库的方法有使用PHP的MSSQL扩展、使用PDO等。想了解更多php连接mssql数据库相关内容,可以阅读本专题下面的文章。

1402

2023.10.23

html怎么上传
html怎么上传

html通过使用HTML表单、JavaScript和PHP上传。更多关于html的问题详细请看本专题下面的文章。php中文网欢迎大家前来学习。

1229

2023.11.03

PHP出现乱码怎么解决
PHP出现乱码怎么解决

PHP出现乱码可以通过修改PHP文件头部的字符编码设置、检查PHP文件的编码格式、检查数据库连接设置和检查HTML页面的字符编码设置来解决。更多关于php乱码的问题详情请看本专题下面的文章。php中文网欢迎大家前来学习。

1440

2023.11.09

php文件怎么在手机上打开
php文件怎么在手机上打开

php文件在手机上打开需要在手机上搭建一个能够运行php的服务器环境,并将php文件上传到服务器上。再在手机上的浏览器中输入服务器的IP地址或域名,加上php文件的路径,即可打开php文件并查看其内容。更多关于php相关问题,详情请看本专题下面的文章。php中文网欢迎大家前来学习。

1303

2023.11.13

php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

65

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
进程与SOCKET
进程与SOCKET

共6课时 | 0.3万人学习

Redis+MySQL数据库面试教程
Redis+MySQL数据库面试教程

共72课时 | 6.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号