Flyertutor


  • 首页

  • 归档

  • 分类

  • 标签

Redis 批量删除 key

发表于 2018-06-12 | 分类于 Redis

redis 的 del 不支持匹配符,只能通过 shell 命令来删除。

1
redis-cli keys "*select*" | awk -F ') "' '{print "\""$1"\""}' | xargs redis-cli del

如果有密码和端口修改的话,加上相应的参数。

©版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 & 作者信息。

Happy Coding

MySQL DDL&DCL

发表于 2018-05-31

MySQL 中 DML 和 DCL 在日常开发中用的不是很多,这里做个简单的记录,便于日后查看。

DDL

创建表

1
2
3
4
5
create table test(
id int not null primary key auto_increment,
name varchar(50) not null comment '名称',
age int not null default 0 comment '年龄'
);

删除表

1
drop table test;

修改表名

1
alter table test rename users;
阅读全文 »

Linux 常用命令

发表于 2018-05-25

查看磁盘使用情况

1
df -h

查看目录大小

1
du -sh *

查看某个进程

1
ps aux | grep php-fpm

监控某个日志文件

1
2
tailf error.log
tail -f error.log

监控服务器

1
top

统计 access.log 文件访问次数最多的 ip 地址,并取前 10 条记录

1
cat access.log | awk ‘{print $1}’ | sort | uniq -c | sort -k1nr | head -10

©版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 & 作者信息。

Happy Coding

PHP 发送 HTTP 请求方式

发表于 2018-05-15 | 分类于 PHP

在 PHP 中我们发送 http 请求常用的是 curl,今天接触到其他几种方式,这里做下简单的记录。

curl 方式

这种是最常用的方式,我这里就不做记录了,平时开发中用的最多了。

stream 流的方式

我们可以使用 file_get_contents 的第三个参数来传递一个包装的 http stream,下面来个简单的代码:

1
2
3
4
5
6
7
8
9
$opts = array(
'http' => array(
'method' => "GET",
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
)
);
$context = stream_context_create($opts);
$result = file_get_contents("http://www.baidu.com", false, $context);
var_dump($result);

详细信息请阅读官方文档

socket方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
参数说明:
hostname: 主机名
port: 端口
errNo: 错误码
errMsg: 错误信息
timeout: 设置连接的时限,单位为秒
*/
$fp = fsockopen("www.baidu.com", 80, $errNo, $errMsg, 10);
if (!$fp) {
echo $errNo, "<br />", $errMsg;
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.baidu.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}

上面只是一个简单的例子,具体用法详细参考官方文档

©版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 & 作者信息。

Happy Coding

Laravel5.5 使用队列

发表于 2018-05-10 | 分类于 Laravel

今天就对 Laravel 的队列功能做个简单的事例,我使用的驱动是 redis,详细可以查看文档。

生成任务类

1
php artisan make:queue SendMsg

就会在 app/Jobs 目录下面生成 SendMsg.php 文件,下面我们去编辑。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Log;

class SetTopicRecommend implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;


private $msg;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct($msg)
{
$this->msg = $msg;
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Log::info($this->msg); // 写日志,可以在日志文件中进行查看
}
}
阅读全文 »

Linux Top 命令详解

发表于 2018-05-07 | 分类于 Linux

最近服务器经常出问题,所以用命令 top 分析其性能,在这里对其做个记录,便于以后查看。

top 是常用的性能分析命令,基本和 windows 的任务管理器类似,下面详细介绍使用方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> top
top - 10:36:26 up 72 days, 22:21, 1 user, load average: 0.00, 0.01, 0.05
Tasks: 81 total, 1 running, 80 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 1883844 total, 99960 free, 375164 used, 1408720 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 1306676 avail Mem

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 43276 2556 1348 S 0.0 0.1 4:19.02 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.02 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 1:20.24 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
7 root rt 0 0 0 0 S 0.0 0.0 0:00.00 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 9:45.47 rcu_sched
10 root rt 0 0 0 0 S 0.0 0.0 0:18.19 watchdog/0

下面将其参数做下详细的说明:

1
top - 10:36:26 up 72 days, 22:21,  1 user,  load average: 0.00, 0.01, 0.05

10:36:26:当前系统时间
up 72 days, 22:21:表示系统开机到现在的运行时间
1 user:当前登录系统的用户个数
load average:分别是1分钟、5分钟、15分钟的系统负载情况,
load average 数据是每隔5秒钟检查一次活跃的进程数,然后按特定算法计算出的数值。如果这个数除以逻辑CPU的数量,结果高于5的时候就表明系统在超负荷运转了。

阅读全文 »

Go RPC 使用例子

发表于 2018-05-03 | 分类于 Go

RPC是 Remote Procedure Call 的缩写,从字面意思理解就是远程过程调用,详细解释 地址

Go 语言官方包里面实现了 RPC,我这里就来个小例子做下简单的演示。

Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main

import "fmt"
import "net/rpc"
import "net/http"
import "net"

type Args struct {
A, B int
}

type Quotient struct {
Quo, Rem int
}

type Arith int

func (this *Arith) Sum(args *Args, reply *int) error {
*reply = args.A + args.B
return nil
}

func (this *Arith) Sub(args *Args, reply *int) error {
*reply = args.A - args.B
return nil
}

func main() {
arith := new(Arith)
rpc.Register(arith)

rpc.HandleHTTP()
l, err := net.Listen("tcp", ":8006")
if err != nil {
fmt.Println("err")
return
}
go http.Serve(l, nil)

select {}
}
阅读全文 »

Go Http服务器

发表于 2018-04-23 | 分类于 Go

http 协议用于 web 开发,Go 语言中也实现了他,下面我们就来看下如何使用他。

hello world

1
2
3
4
5
6
7
8
9
10
11
12
13
package main

import (
"net/http"
"log"
)

func main() {
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writer.Write([]byte("hello world"))
})
log.Fatal(http.ListenAndServe(":8000", nil))
}

编译运行

1
go run http.go

打开浏览器 http://localhost:8000 就可以看到效果了,是不是很简单。

阅读全文 »

Redis 实现附近的人

发表于 2018-04-18 | 分类于 PHP

利用 redis 实现附近的人功能,主要使用的是 redis 的 geo 数据类型,需要注意的是 redis 的版本。

软件和环境
centOS7, Redis4, phpredis3.1.4, PHP7

PHP的扩展我使用的是 phpredis ,如果你使用的是 predis 的话,请自行查找 api 文档,基本操作方法相似。

连接redis

1
2
3
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
// 如果有密码的话,使用 auth

记录用户定位信息

我这里主要用 user:{id} 来做标记记录坐标信息

1
2
3
$redis->geoAdd("users", 116.365, 39.976, "user:1");
$redis->geoAdd("users", 116.366, 39.975, "user:2");
$redis->geoAdd("users", 116.364, 39.977, "user:3");

搜索附近的人

使用自己的坐标来搜索周边固定位置的用户

1
2
3
// 搜索附近 5km 的用户,并且返回距离
$options = ['WITHDIST'];
$lists = $redis->geoRadius('users', 116.36, 39.96, 5, 'km', $options);

获取用户列表

根据上述获取的列表,可以将其进行分页展示。具体可以找 phpredis 文档 阅读详情。

我这里只是讲解了一个大概的思路,在具体的业务逻辑中,做具体的处理。

©版权声明:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 & 作者信息。

Happy Coding

使用 Gin 开发博客教程二

发表于 2018-04-14 | 分类于 Go

我们这节就来实现创建文章,涉及的要点创建数据表,ORM,创建文章。

建表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 分类表
CREATE TABLE `category` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20),
`status` tinyint(3) unsigned NOT NULL DEFAULT 1,
PRIMARY KEY (`id`),
KEY `idx_name` (`name`)
) ENGINE=InnoDB;

# 文章表
CREATE TABLE `posts` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(200) NOT NULL,
`category_id` int(10) unsigned NOT NULL DEFAULT '0',
`content` text NOT NULL,
`is_top` int(10) unsigned NOT NULL DEFAULT '0',
`hits` int(10) unsigned NOT NULL DEFAULT '0',
`sorts` int(10) unsigned NOT NULL DEFAULT '0',
`status` tinyint(3) unsigned NOT NULL DEFAULT '0',
`created_at` timestamp,
`updated_at` timestamp,
PRIMARY KEY (`id`),
KEY `idx_title` (`title`),
KEY `idx_category_id` (`category_id`),
KEY `idx_sorts` (`sorts`)
) ENGINE=InnoDB;

选择ORM

我这里选择的 ORM 是 gorm,安装方法为 go get -u github.com/jinzhu/gorm

1
2
3
4
5
6
7
func GetDB() *gorm.DB {
db, err := gorm.Open("mysql", "go:123456@/video?charset=utf8&parseTime=True&loc=Local")
if err != nil {
panic("failed to connect database")
}
return db
}

创建模板

1
router.LoadHTMLGlob("views/**/*")

建立目录 views,创建 create.html 表单模板,这里就写一个简单的模板文件就行。

1
2
3
4
5
6
7
8
9
10
11
12
13
<form action="/posts/store" method="post">
<div>
标题:
<input type="text" name="title">
</div>
<div>
内容:
<input type="text" name="title">
</div>
<div>
<input type="submit" value="提交" >
</div>
</form>

注册路由

1
2
router.GET("/posts/create", PostCreate)
router.POST("/posts/store", PostStore)

创建方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
func PostCreate(c *gin.Context) {
c.HTML(http.StatusOK, "create.html", nil)
}

func PostStore(c *gin.Context) {
title := c.PostForm("title")
content := c.PostForm("content")
ret := SavePost(title, content)
if ret {
c.String(http.StatusOk, "success")
} else {
c.String(http.StatusOk, "error")
}
}

创建入库

1
2
3
4
5
6
7
8
type Posts struct {
Title string
Content string
}
func SavePost(title, content string) bool {
posts := Posts{Title: title, Content: content}
return db.Create(&posts)
}
12…8
Flyertutor

Flyertutor

一个PHP爱好者

78 日志
9 分类
16 标签
GitHub
© 2018 Flyertutor
由 Hexo 强力驱动
主题 - NexT.Gemini