当前位置: 首页 > 后端技术 > PHP

php+redis的介绍与简单应用

时间:2023-03-29 18:14:44 PHP

1、实验环境:win10+redis3.2+php72、安装php-redis/redis/redis图形管理工具等,此步略过;3、redis常用的五种数据类型,没有详细参考:http://www.runoob.com/redis/r...四、php+mysql+redis简单应用数据库名:redis数据表:redis_user模拟php操作Mysql+redisCURD操作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入口文件,操作MysqlID时请使用主键table('user')->insert(['user'=>'张三','pass'=>md5('123456'),'create_time'=>date('Y-m-dH:i:s')]);//删除数据//echoMysql::getInstance()->table('user')->where(array('id'=>30))->delete();//查看单条数据//$data=Mysql::getInstance()->table('user')->where(array('id'=>'30'))->find();//print_r($data);//查找所有数据//$all=Mysql::得到Instance()->table('user')->field('id,user')->select();$all=Mysql::getInstance()->table('user')->select();print_r($all);//修改数据//echoMysql::getInstance()->table('user')->where(array('id'=>30))->update(['user'=>'张三adfadfasdf11111111','pass'=>md5('123456aaa')]);?>Mysql.php数据库以及redis操作文件options['prefix']=$config['mysql']['prefix'];$this->conn=$conn;$this->redis=newRedis();$this->redis->connect($config['redis']['host'],$config['redis']['port']);}//获取对象实例staticfunctiongetInstance(){if(self::$instance){returnself::$instance;}else{self::$instance=newself();返回自我::$实例;}}//设置表名publicfunctiontable(string$table){$this->options['table']='`'.$this->options['prefix'].$table.'`';返回$这个;}//设置redis关键字publicfunctionredis(string$key){$this->options['key']=$key;返回$这个;}//设置条件publicfunctionwhere(array$where){$condition='';$and=count($where)>1?'和':'';foreach($whereas$key=>$value){if($key=='id'){$this->options['user_id']=$value;}if(strpos($key,':')){$arr=explode(':',$键);$condition.='`'.$arr['0'].'`'.$arr['1'].'"'.$值。'"'.$和;}else{$condition.='`'.$key.'`='.'"'.$value.'"'.$and;$this->options['where']=rtrim($condition,'and');返回$这个;}//设置字符串publicfunctionfield(string$field){$this->options['field']=$field;返回$这个;}//增加数据publicfunctioninsert(array$data){$key='`'.implode('`,`',array_keys($data)).'`';$value='"'.implode('","',$data).'"';$sql="插入{$this->options['table']}(".$key.")values(".$value.");";如果(mysqli_query($this->conn,$sql)){$user_id=$this->conn->insert_id;$data['id']=$user_id;//以哈希类型存储$this->redis->hset($this->options['table'],$user_id,json_encode($data));返回$user_id;}否则{返回0;}}//删除数据publicfunctiondelete(){$where=$this->options['where']?$this->options['where']:'1';$sql="从{$this->options['table']}哪里{$where}删除;";如果(mysqli_query($this->conn,$sql)){$this->redis->hdel($this->options['table'],$this->options['user_id']);返回1;}否则{返回0;}}//修改数据publicfunctionupdate(array$data){$condition='';$where=$this->options['where']?$this->options['where']:'1';foreach($dataas$key=>$value){$condition.=",`".$key."`='".$value."'";}$condition=substr($condition,1);$sql="更新{$this->options['table']}设置{$condition}where{$where};";if(mysqli_query($this->conn,$sql)){$hashData=(array)json_decode($this->redis->hget($this->options['table'],$this->options['user_id']));foreach($dataas$key=>$value){$hashData[$key]=$value;$this->redis->hset($this->options['table'],$this->options['user_id'],json_encode($hashData));返回1;}否则{返回0;}}//查询单条数据publicfunctionfind(){$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']));如果($field!='*'){$field=explode(',',$field);foreach($fieldas$value){$arr[$value]=$data[$value];$arr['typ']='redis';}返回$arr;}返回$数据;}else{$sql="select{$field}from{$this->options['table']}where{$where};";如果($query=mysqli_query($this->conn,$sql)){returnmysqli_fetch_assoc($query);}else{返回数组();}}}//查询所有数据publicfunctionselect(){$data=array();$field=$this->options['field']?$this->options['field']:'*';$hashData=$this->redis->hgetall($this->options['table']);如果($hashData){如果($field!='*'){$field=explode(',',$field);}foreach($hashDataas$key=>$value){$data[$key]=array();$values=(array)json_decode($value);如果($字段!='*'){foreach($fieldas$k=>$v){$data[$key][$v]=$values[$v];}}else{$data[$key]=$values;}}echo'从redis获取数据
';}else{$sql="从{$this->options['table']}中选择{$field};";如果($query=mysqli_query($this->conn,$sql)){while($row=mysqli_fetch_assoc($query)){$data[]=$row;}}}返回$数据;}公共函数__destruct(){mysqli_close($this->conn);}}