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( '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 entry file, please use the primary key ID when operating Mysql
<?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')]); ?>
<?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']); } //获取对象实例 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获取数据<pre class="brush:php;toolbar:false">'; $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获取数据<pre class="brush:php;toolbar:false">'; } 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); } }
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!

Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

redis error就是redis数据库和其组合使用的部件出现错误,这个出现的错误有很多种,例如Redis被配置为保存数据库快照,但它不能持久化到硬盘,用来修改集合数据的命令不能用。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.