php redis使用,php + redis 入门及简单应用

本文介绍了一个在Windows环境下,使用PHP7、Redis3.2和MySQL结合进行数据操作的示例。通过Mysql.php类实现对MySQL数据库和Redis缓存的增删查改(CRUD)操作,利用Redis作为缓存提高数据读取效率。
摘要由CSDN通过智能技术生成

一、实验环境: 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配置文件

$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

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

?>

bV8ez1?w=438&h=583

d09719c7ddd678c9dc3f6ed0e5e706b2.png

2e7b2042eb5db5170a53d398f4d2b01b.png

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

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获取数据

';

$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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值