摘要:一實驗環境二圖形管理工個等安裝,此步驟略過三常用的五種數據類型,不做詳細說明參考四簡單應用數據庫名稱數據表模擬操作的操作配置文件入口文件,操作時請使用主鍵增加數據張三刪除數據查看單條數據查找所有數據修改數據
一、實驗環境: 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")]); ?>
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); } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/28590.html
摘要:熟悉編程,對于網絡模型有一定的認知,熟悉多路復用技術。對主從延遲能有效解決。能夠支持對于千萬級流量網站的正常開發維護工作。結束語技術無止境,未來有更多可能。 本文旨在給要學習 PHP 的新手一個大概的認知輪廓,在心里有個學習的結構,有的放矢,避免走太多彎路。大神請忽略。 入門階段 預備知識 1、掌握基本HTML、JS、CSS語法;熟悉 Bootstrap。 參考: https:/...
閱讀 3141·2023-04-26 02:33
閱讀 3102·2023-04-25 21:33
閱讀 907·2021-09-02 09:56
閱讀 2910·2019-08-30 15:44
閱讀 2460·2019-08-30 13:15
閱讀 1034·2019-08-30 13:04
閱讀 1634·2019-08-29 15:09
閱讀 3956·2019-08-26 18:26