-
<?php
// 数据库连接类
class DB{
//私有的属性
private static $dbcon=false;
private $host;
private $port;
private $user;
private $pass;
private $db;
private $charset;
private $link;
//私有的构造方法
private function __construct(){
$this->host = 'localhost';
$this->port = '3306';
$this->user = 'root';
$this->pass = 'root';
$this->db = 'imooc';
$this->charset= 'utf8';
//连接数据库
$this->db_connect();
//选择数据库
$this->db_usedb();
//设置字符集
$this->db_charset();
}
//连接数据库
private function db_connect(){
$this->link=mysqli_connect($this->host.':'.$this->port,$this->user,$this->pass);
if(!$this->link){
echo "数据库连接失败<br>";
echo "错误编码".mysqli_errno($this->link)."<br>";
echo "错误信息".mysqli_error($this->link)."<br>";
exit;
}
}
//设置字符集
private function db_charset(){
mysqli_query($this->link,"set names {$this->charset}");
}
//选择数据库
private function db_usedb(){
mysqli_query($this->link,"use {$this->db}");
}
//私有的克隆
private function __clone(){
die('clone is not allowed');
}
//公用的静态方法
public static function getIntance(){
if(self::$dbcon==false){
self::$dbcon=new self;
}
return self::$dbcon;
}
//执行sql语句的方法
public function query($sql){
$res=mysqli_query($this->link,$sql);
if(!$res){
echo "sql语句执行失败<br>";
echo "错误编码是".mysqli_errno($this->link)."<br>";
echo "错误信息是".mysqli_error($this->link)."<br>";
}
return $res;
}
//获得最后一条记录id
public function getInsertid(){
return mysqli_insert_id($this->link);
}
/**
* 查询某个字段
* @param
* @return string or int
*/
public function getOne($sql){
$query=$this->query($sql);
return mysqli_free_result($query);
}
//获取一行记录,return array 一维数组
public function getRow($sql,$type="assoc"){
$query=$this->query($sql);
if(!in_array($type,array("assoc",'array',"row"))){
die("mysqli_query error");
}
$funcname="mysqli_fetch_".$type;
return $funcname($query);
}
//获取一条记录,前置条件通过资源获取一条记录
public function getFormSource($query,$type="assoc"){
if(!in_array($type,array("assoc","array","row")))
{
die("mysqli_query error");
}
$funcname="mysqli_fetch_".$type;
return $funcname($query);
}
//获取多条数据,二维数组
public function getAll($sql){
$query=$this->query($sql);
$list=array();
while ($r=$this->getFormSource($query)) {
$list[]=$r;
}
return $list;
}
public function selectAll($table,$where,$fields='*',$order='',$skip=0,$limit=1000)
{
if(is_array($where)){
foreach ($where as $key => $val) {
if (is_numeric($val)) {
$condition = $key.'='.$val;
}else{
$condition = $key.'=\"'.$val.'\"';
}
}
} else {
$condition = $where;
}
if (!empty($order)) {
$order = " order by ".$order;
}
$sql = "select $fields from $table where $condition $order limit $skip,$limit";
$query = $this->query($sql);
$list = array();
while ($r= $this->getFormSource($query)) {
$list[] = $r;
}
return $list;
}
/**
* 定义添加数据的方法
* @param string $table 表名
* @param string orarray $data [数据]
* @return int 最新添加的id
*/
public function insert($table,$data){
//遍历数组,得到每一个字段和字段的值
$key_str='';
$v_str='';
foreach($data as $key=>$v){
// if(empty($v)){
// die("error");
// }
//$key的值是每一个字段s一个字段所对应的值
$key_str.=$key.',';
$v_str.="'$v',";
}
$key_str=trim($key_str,',');
$v_str=trim($v_str,',');
//判断数据是否为空
$sql="insert into $table ($key_str) values ($v_str)";
$this->query($sql);
//返回上一次增加操做产生ID值
return $this->getInsertid();
}
/*
* 删除一条数据方法
* @param1 $table, $where=array('id'=>'1') 表名 条件
* @return 受影响的行数
*/
public function deleteOne($table, $where){
if(is_array($where)){
foreach ($where as $key => $val) {
$condition = $key.'='.$val;
}
} else {
$condition = $where;
}
$sql = "delete from $table where $condition";
$this->query($sql);
//返回受影响的行数
return mysqli_affected_rows($this->link);
}
/*
* 删除多条数据方法
* @param1 $table, $where 表名 条件
* @return 受影响的行数
*/
public function deleteAll($table, $where){
if(is_array($where)){
foreach ($where as $key => $val) {
if(is_array($val)){
$condition = $key.' in ('.implode(',', $val) .')';
} else {
$condition = $key. '=' .$val;
}
}
} else {
$condition = $where;
}
$sql = "delete from $table where $condition";
$this->query($sql);
//返回受影响的行数
return mysqli_affected_rows($this->link);
}
/**
* [修改操作description]
* @param [type] $table [表名]
* @param [type] $data [数据]
* @param [type] $where [条件]
* @return [type]
*/
public function update($table,$data,$where,$limit=0){
//遍历数组,得到每一个字段和字段的值
$str='';
foreach($data as $key=>$v){
$str.="$key='$v',";
}
$str=rtrim($str,',');
if(is_array($where)){
foreach ($where as $key => $val) {
if(is_array($val)){
$condition = $key.' in ('.implode(',', $val) .')';
} else {
$condition = $key. '=' .$val;
}
}
} else {
$condition = $where;
}
if (!empty($limit)) {
$limit = " limit ".$limit;
}else{
$limit='';
}
//修改SQL语句
$sql="update $table set $str where $condition $limit";
$this->query($sql);
//返回受影响的行数
return mysqli_affected_rows($this->link);
}
}
?>
使用方法
对DB类中__construct()中的配置信息,进行符合自己数据库的修改
include 引入DB类
使用DB类中的方法需要先进行实例化,以插入数据为例:
$db = DB::getIntance();
$insert_data = ['order_id'=>'10010','order_amount' = '200.00','status'=> 1];
$res = $db->insert('order_info',$insert_data);
查看全部 -
>和>>都是重定向输出 1> 指标准信息输出路径(也就是默认的输出方式) 2> 指错误信息输出路径 2>&1 指将标准信息输出路径指定为错误信息输出路径(也就是都输出在一起)
1是标准输出
2是错误输出
> 和 >> 都是输出重定向符号。标准输出默认是打印到控制台,如果要导入到文件,就需要使用>或>>。> 会覆盖已有的文件内容,而>>会附加到已有内容之后。
< 和 << 是输入重定向符号。从文件中读取内容。
2>&1 是把错误输出导入(合并)到标准输出流中查看全部 -
消息队列 1、概念:高并发时,需要一个中间件来进行处理,消息放入队列后就可以立刻返回了。 2、核心结构:业务系统-》消息队列-》队列处理系统 3、应用场景:冗余,解耦,流量削峰(缓存+消息队列),异步通信,场景扩展,排序保证 4、优点:速度快, 5、缺点:单条大消息包时效率低查看全部
-
秒杀程序实现代码查看全部
-
消息队列总结
消息队列的概念、原理和场景
解耦案例:队列处理订单系统和配送系统(利用mysql中间表实现)
流量削峰案例:Redis的List类型实现秒杀;(用内存处理工具在大数据高并发挤爆服务器之前,进行中间件的处理,从而起到优化的作用)
RabbitMQ:更专业的消息系统实现方法,适用于架构逻辑复杂,分布式的程序中
守护进程处理队列:思路就是做一个监听,监听到消息队列中有消息时就进行处理
查看全部 -
<?php include_once './include/db.php'; if (!empty($_GET['mobile'])) { //这里首先应该是订单中心的处理流程 //....... //把用户get过来的数据进行过滤 $order_id = rand(10000, 99999); //订单信息 $insert_data = array( 'order_id ' => $order_id, 'mobile' => $_GET['mobile'], 'created_at' => date('Y-m-d H:i:s'), 'status' => 0, ); //存储数据 $db = DB::getInstance(); $res = $db->insert('order_queue', $insert_data); if ($res) { echo $insert_data['id'] . '保存成功'; } else { echo $insert_data['id'] . '保存失败'; } }
查看全部 -
#!/bin/bash
date
"+%G-%m-%d %H:%M:S"
cd (good.sh文件地址,如:) /home/pi/Sites/Imooc/queue_mysql/
php goods.php
定时任务部署
crontab -e
*/1 * * * * /home/pi/Sites/Imooc/queue_mysql/good.sh >> home/pi/Sites/Imooc/queue_mysql/log.log 2>&1
查看全部 -
<?php include './../include/db.php'; $db = DB::getInstance(); //1先把要处理的记录更新为等待处理 $wating = array('status' => 0); $lock = array('status' => 2); $res_lock= $db->update('order_queue', $lock, $waiting, 2); //2我们要选择出刚刚咱们更新的这些数据结构,然后进行配送系统的处理 if ($res_lock) { $res = $db->selectAll('order_queue', $lock); //然后由配货系统进行配货处理 //... //3 把这些处理过的程序更新为已完成 $success = array( 'status' => 1, 'updated' => date('Y-m-d H:i:s') ); $res_last = $db->update('order_queue', $success, $lock); if ($res_last) { echo 'Success' . $res_last; } else { echo 'Fail' . $res_last; } }else{ echo 'ALL FINISHED'; }
查看全部 -
Redis list
LPUSH/LPUSHX
RPUSH/RPUSHX
LPOP
RPOP
LTRIM
LLEN
LSET
LINDEX
LRANGE
查看全部 -
程序流程查看全部
-
undefined查看全部
-
redis_queue数据库表的SQL语句: create table `t_queue`( `id` int(11) unsigned not null AUTO_INCREMENT, `uid` int(11) not null default '0', `stamp` varchar(24) not null, primary key(`id`) )ENGINE=MyISAM default charset=utf8查看全部
-
sudo apt install rabbitmq-server查看全部
-
tail -f 监控日志输出查看全部
-
crontab -e中写定时任务; 2>&1 把错误输出转化成标准输出查看全部
举报