Skip to content

Instantly share code, notes, and snippets.

@xiazhibin
xiazhibin / redis_protocol.sh
Last active January 27, 2019 13:52
将redis cmd转化为redis协议
#!/bin/bash
while read CMD; do
# each command begins with *{number arguments in command}\r\n
XS=($CMD); printf "*${#XS[@]}\r\n"
# for each argument, we append ${length}\r\n{argument}\r\n
for X in $CMD; do printf "\$${#X}\r\n$X\r\n"; done
done < redis_commands.txt
@xiazhibin
xiazhibin / nginx.md
Last active October 16, 2018 08:33
一些nginx的配置
location ~ \.(woff|woff2|ttf|dds|map|js|css|jpg|png|gif|lang|ico|mp4|json) {
        if ($request_uri ~* "^.*a\.js$ ") {
             add_header    Cache-Control  no-store;
        }
        root     /www-data/example1;
	    try_files $uri =404;
}
@xiazhibin
xiazhibin / common_bash.md
Last active October 14, 2018 14:46
常用bash命令

检测环境变量存不存在

if [ -z ${VAR+x} ]
then
    echo "VAR not exist"
else
    echo "$VAR"
fi
@xiazhibin
xiazhibin / t.md
Last active October 23, 2018 07:10
一些统计bash

cat /var/log/nginx/access.log | awk '{print $7}' | sort | uniq -c | sort -r -n |head -n 5

  • uniq 去取重复的行 -c, --count prefix lines by the number of occurrences
  • sort 排序 -r 以相反的顺序来排序 -n 依照数值的大小排序
  • head 显示开头 -n 显示的行数

wc -l 统计行数

du * -hs | sort -hr 查看文件夹大小

cat /dev/null > test.txt 清空文件

@xiazhibin
xiazhibin / UNION_or_OR.md
Last active May 18, 2018 02:13
Query with UNION or OR?
CREATE TABLE public.friend (
   uid1 INTEGER,
   uid2 INTEGER
);
CREATE INDEX index_uid1 ON friend USING BTREE (uid1);
REATE INDEX index_uid2 ON friend USING BTREE (uid2);

friend记录user1和user2是朋友关系,而且只会记录单方,(1,2)和(2,1)是一样的。

@xiazhibin
xiazhibin / fanin.go
Created May 10, 2018 02:53
go concurrency pattern
package main
import (
"fmt"
"time"
"math/rand"
)
func main() {
c := fanIn(boring("joe"),boring("Ann"))
@xiazhibin
xiazhibin / postgresql_test.md
Last active December 20, 2018 07:41
postgresql生成测试数据

核心方法

generate_series(start,stop);生成序列
(random()*(2*10^9))::integer;生成随机数
substr('abcdefghijklmnopqrstuvwxyz',1,(random()*26)::integer);生成随机字符串

例如有一下的Test表

 Table "public.test"
@xiazhibin
xiazhibin / thread.md
Last active April 12, 2018 08:25
线程一些常识

主线程退出对子线程的影响

进程是资源分配的基本单位,线程是CPU调度的基本单位

Main线程是个非守护线程,不能设置成守护线程

import threading
import time

threading.currentThread().setDaemon(True)
time.sleep(1)
@xiazhibin
xiazhibin / bit.md
Last active April 11, 2018 08:44
一些位运算的技巧

n个bit能表示的最大数值

-1 ^ (-1<<n) = 2^n-1

原理:将最右n位变成1

  • -1 表示 1111 1111 1111 1111
  • 移4位后 1111 1111 1111 1111 0000
  • 高位舍去,异或
     1111 1111 1111 0000
^ 1111 1111 1111 1111 
@xiazhibin
xiazhibin / concurrent.py
Created April 1, 2018 08:20
IO复用异步编程
def future_add_done_callback(future, callback):
if future.done():
callback(future)
else:
future.add_done_callback(callback)