git 常用操作

  1. 全局配置
    git config --global user.name "hitortoise"

git config --global user.email "yzhsh89@126.com"

  1. 生成密钥
    ssh-keygen -t rsa -C"yzhsh89@126.com"
  2. 服务端添加公钥
    vim ~/.ssh/authorized_keys
  3. 创建裸仓库
    git init --bare sample.git

chown -R git:git sample.git

  1. 克隆
    git clone git@server:/srv/sample.git
  2. git不能显示中文
    git config --global core.quotepath false

MySql 百千万级别数据中随机获取一条或多条记录之RAND()优化

最近所做读书小程序中需从千万条数据中随机抽取一条名言语录展示,常规的select * from famous order by RAND() LIMIT 1;,缺少索引及全表扫描,速度异常慢。
SELECT * FROM famous AS t1 JOIN (SELECT ROUND(RAND() * ((SELECT MAX(id) FROM famous)-(SELECT MIN(id) FROM famous))+(SELECT MIN(id) FROM famous)) AS id) AS t2 WHERE t1.id >= t2.id ORDER BY t1.id LIMIT 1;

date("Y-m-d",strtotime("-1 month")) 真的靠谱吗?

时常用date("Y-m-d",strtotime("-1 month"))获取上个月的日期,其实它并不靠谱。
如当前日期未3月3,则:var_dump(date("Y-m-d", strtotime("-1 month", strtotime("2018-03-31"))));
得到的结果并非2018-02-28而是2018-03-03
正确的方式之一是添加last day of first day of
var_dump(date("Y-m-d", strtotime("last day of -1 month", strtotime("2017-03-31"))));

php将base64保存为图片

<?php 
$base64_url = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADkAAAA5CAYAAACMGIOFAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyNpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDE0IDc5LjE1MTQ4MSwgMjAxMy8wMy8xMy0xMjowOToxNSAgICAgICAgIj4gPHJkZ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/^(data:\s*image\/(\w+);base64,)/', $base64_url, $result);
$type = $result[2];

$base64_body = substr(strstr($base64_url,','),1);
$img = base64_decode($base64_body);

$file_name_ok = md5(time()).".".$type;
file_put_contents($file_name_ok, $img);

git直接push提交代码到线上生产环境

为方便开发过程中随时预览线上效果,需考虑git提交数据时直接将代码同时push到线上生产环境中。

  1. project.git/hooks新建post-receive

    !/bin/sh

    git --work-tree=/www/project --git-dir=/data/git/project.git checkout -f

  2. chown +x post-receive
  3. 注意赋予project项目目录git写入权限

ThinkPHP5 后台不使用API路由

THINKPHP5开发API接口路由容易影响后台管理系统访问,需在route.php中使用路由变量规则定义API路由
Route::pattern('ver','^v\d+');
此时定义API路由
Route::get(':ver/version', "api/common/get_version");