redis常用操作类型

字符串用法

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
#设置键值 ex表示妙 nx:键不存在时创建
set a 1 ex 100 nx
#px 代表毫秒 xx:存在时创建
set a 1 px 1000 xx
#获取建
get a
#删除
del a
#获取所有键
keys *
#获取10条与a有关的从0位置开始
scan 0 match '*a*' 10
#自增
incr
#递减
decr
#指定键加3
incrby a 3
#指定键减3
decrby a 3
#字符串抓加456
append a 456
#批量赋值
mset a 1 b 2 c 3
#清空当前数据库
flushdb
#清空所有数据库
flushall

hash 用法

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
127.0.0.1:6379[2]> hset people age 18
(integer) 1
127.0.0.1:6379[2]> hset people name l
(integer) 1
127.0.0.1:6379[2]> hget people age
"18"
127.0.0.1:6379[2]> hmset people sex '男' phoen apple
OK
127.0.0.1:6379[2]> hget people sex
"\xe7\x94\xb7"
127.0.0.1:6379[2]> hget people phoen
"apple"
127.0.0.1:6379[2]> hmget people phoen sex name age
1) "apple"
2) "\xe7\x94\xb7"
3) "l"
4) "18"
127.0.0.1:6379[2]> hgetall people
1) "age"
2) "18"
3) "name"
4) "l"
5) "sex"
6) "\xe7\x94\xb7"
7) "phoen"
8) "apple"
127.0.0.1:6379[2]> HEXISTS people age
(integer) 1
127.0.0.1:6379[2]> HEXISTS people agea
(integer) 0
127.0.0.1:6379[2]> HINCRBY people age 1
(integer) 19
127.0.0.1:6379[2]> hget people age
"19"
127.0.0.1:6379[2]> hkeys people
1) "age"
2) "name"
3) "sex"
4) "phoen"
127.0.0.1:6379[2]> HVALS people
1) "19"
2) "l"
3) "\xe7\x94\xb7"
4) "apple"
127.0.0.1:6379[2]> hlen people
(integer) 4
127.0.0.1:6379[2]> hsetnx people color green
(integer) 1
127.0.0.1:6379[2]> hlen people
(integer) 5
127.0.0.1:6379[2]> hgetall people
1) "age"
2) "19"
3) "name"
4) "l"
5) "sex"
6) "\xe7\x94\xb7"
7) "phoen"
8) "apple"
9) "color"
10) "green"
127.0.0.1:6379[2]> hkeys people
1) "age"
2) "name"
3) "sex"
4) "phoen"
5) "color"

列表用法

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
127.0.0.1:6379[2]> lpush frined 1 2 3 4 5
(integer) 5
127.0.0.1:6379[2]> lpop frined
"5"
127.0.0.1:6379[2]> rpop frined
"1"
127.0.0.1:6379[2]> rpush drined 7
(integer) 1
127.0.0.1:6379[2]> rpop frined
"2"
127.0.0.1:6379[2]> llen frined
(integer) 2
127.0.0.1:6379[2]> lindex frined 1
"3"
127.0.0.1:6379[2]> lindex frined 2
(nil)
127.0.0.1:6379[2]> lindex frined 3
(nil)
127.0.0.1:6379[2]> lindex frined 0
"4"
127.0.0.1:6379[2]> lindex frined -1
"3"
127.0.0.1:6379[2]> lrange frined 0 2
1) "4"
2) "3"
127.0.0.1:6379[2]> lrange frined -2 2
1) "4"
2) "3"
127.0.0.1:6379[2]> lrange frined -1 2
1) "3"
127.0.0.1:6379[2]> lrange frined 0 3
1) "4"
2) "3"
127.0.0.1:6379[2]> llen frined
(integer) 2
127.0.0.1:6379[2]> lpush frined 1
(integer) 3
127.0.0.1:6379[2]> lrange frined 0 3
1) "1"
2) "4"
3) "3"
127.0.0.1:6379[2]> rpush frined 7
(integer) 4
127.0.0.1:6379[2]> lrange frined 0 4
1) "1"
2) "4"
3) "3"
4) "7"
127.0.0.1:6379[2]> rpushx frined 7
(integer) 5
127.0.0.1:6379[2]> lrange frined 0 5
1) "1"
2) "4"
3) "3"
4) "7"
5) "7"
127.0.0.1:6379[2]> lindex frined 1
"4"
127.0.0.1:6379[2]> lindex frined 0
"1"
127.0.0.1:6379[2]> blpop frined 1
1) "frined"
2) "1"
127.0.0.1:6379[2]> lpush friend 1 2 3
(integer) 3
127.0.0.1:6379[2]> linsert friend before 0 0
(integer) -1
127.0.0.1:6379[2]> llen friend
(integer) 3
127.0.0.1:6379[2]> linsert friend before 1 33
(integer) 4
127.0.0.1:6379[2]> llen friend
(integer) 4
127.0.0.1:6379[2]> lrange friend 0 4
1) "3"
2) "2"
3) "33"
4) "1"
127.0.0.1:6379[2]> lset friend 3 11
OK
127.0.0.1:6379[2]> lrange friend 0 4
1) "3"
2) "2"
3) "33"
4) "11"

集合用法

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#集合追加元素
127.0.0.1:6379[2]> sadd address home schoole company
(integer) 3
#返回集合数
127.0.0.1:6379[2]> scard address
(integer) 3
127.0.0.1:6379[2]> smembers address
1) "schoole"
2) "company"
3) "home"
127.0.0.1:6379[2]> srem address company
(integer) 1
127.0.0.1:6379[2]> smembers address
1) "schoole"
2) "home"
#随即取多个元素
127.0.0.1:6379[2]> srandmember address 1
1) "schoole"
127.0.0.1:6379[2]> srandmember address 2
1) "schoole"
2) "home"
127.0.0.1:6379[2]> srandmember address 1
1) "home"
127.0.0.1:6379[2]> srandmember address 1
1) "home"
127.0.0.1:6379[2]> sadd address2 hospital
(integer) 1
#移动元素到另一个集合
127.0.0.1:6379[2]> smove address address2 schoole
(integer) 1
127.0.0.1:6379[2]> smove address address2 school
(integer) 0
127.0.0.1:6379[2]> smembers address2
1) "schoole"
2) "hospital"
127.0.0.1:6379[2]> smembers address
1) "home"
#判断元素是否存在
127.0.0.1:6379[2]> sismember address home
(integer) 1
127.0.0.1:6379[2]> sismember address schoole
(integer) 0
127.0.0.1:6379[2]> sunion address address2
1) "hospital"
2) "schoole"
3) "home"
127.0.0.1:6379[2]> sadd address park
(integer) 1
127.0.0.1:6379[2]> sadd address2 park
(integer) 1
#并集
127.0.0.1:6379[2]> sunion address address2
1) "hospital"
2) "schoole"
3) "park"
4) "home"
#并集保存在addres3
127.0.0.1:6379[2]> SUNIONSTORE address3 address address2
(integer) 4
127.0.0.1:6379[2]> smembers address3
1) "hospital"
2) "schoole"
3) "park"
4) "home"
#获取交集
127.0.0.1:6379[2]> sinter address address2
1) "park"
127.0.0.1:6379[2]> sdiff address address2
1) "home"
127.0.0.1:6379[2]> smembers address
1) "park"
2) "home"
#获取集合元素
127.0.0.1:6379[2]> smembers address2
1) "park"
2) "schoole"
3) "hospital"
##获取差集
127.0.0.1:6379[2]> sdiff address2 address
1) "hospital"
2) "schoole"

有序集合

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#增加集合元素
127.0.0.1:6379> zadd orderedset 1 home 2 school
(integer) 2
#返回元素个数
127.0.0.1:6379> zcard orderedset
(integer) 2
#返回分数在0-30之间的元素个数
127.0.0.1:6379> zcount orderedset 0 30
(integer) 2
#对home元素累加2
127.0.0.1:6379> zincrby orderedset 2 home
"3"
#获取home的值
127.0.0.1:6379> zscore orderedset home
"3"
127.0.0.1:6379> zscore orderedset school
"2"
#通过索引(0-2)获取元素并带上分数
127.0.0.1:6379> zrange orderedset 0 2 withscores
1) "school"
2) "2"
3) "home"
4) "3"
127.0.0.1:6379> zrangebyscore orderedset 0 2 withscores limit 0 2
1) "school"
2) "2"
#获取分数在1-3之间的元素,从索引0到索引2
127.0.0.1:6379> zrangebyscore orderedset 1 3 withscores limit 0 2
1) "school"
2) "2"
3) "home"
4) "3"
127.0.0.1:6379> zrangebyscore orderedset 2 3 withscores limit 0 2
1) "school"
2) "2"
3) "home"
4) "3"
127.0.0.1:6379> zincrby orderedset 2 home
"5"
127.0.0.1:6379> zrangebyscore orderedset 2 3 withscores limit 0 2
1) "school"
2) "2"
127.0.0.1:6379> zrangebyscore orderedset 2 4 withscores limit 0 2
1) "school"
2) "2"
127.0.0.1:6379> zrangebyscore orderedset 2 5 withscores limit 0 2
1) "school"
2) "2"
3) "home"
4) "5"
#移除元素
127.0.0.1:6379> zrem orderedset home
(integer) 1
#移除分数在1-3之间的元素
127.0.0.1:6379> zremrangebyscore orderedset 1 3
(integer) 1
127.0.0.1:6379> zrank orderedset home
(nil)
127.0.0.1:6379> zadd orderedset 1 home 2 school
(integer) 2
#获取home顺序索引
127.0.0.1:6379> zrank orderedset home
(integer) 0
127.0.0.1:6379> zrank orderedset school
(integer) 1
127.0.0.1:6379> zrevrank orderedset school
(integer) 0
#获取home逆序索引
127.0.0.1:6379> zrevrank orderedset home
(integer) 1

nginx location uri 调用示例

nginx 反斜杠使用示例

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
42
43
44
45
46
47
48
49
50
51
52
server {
listen 80;
server_name localhost;
location /api/ {
proxy_pass http://localhost:8080;
}
#请求地址:http://localhost/api/demo
#代理后的地址:http://localhost:8080/api/demo

location /api/ {
proxy_pass http://localhost:8080/;
}
#请求地址:http://localhost/api/demo
#代理后的地址:http://localhost:8080/demo

location /api {
proxy_pass http://localhost:8080;
}
#请求地址:http://localhost/api/demo
#代理后的地址:http://localhost:8080/api/demo

location /api {
proxy_pass http://localhost:8080/;
}
#请求地址:http://localhost/api/demo
#代理后的地址:http://localhost:8080//demo 双斜线

location /api/ {
proxy_pass http://localhost:8080/test;
}
#这样配置没有斜线
#请求地址:http://localhost/api/demo
#代理后的地址:http://localhost:8080/testdemo

location /api/ {
proxy_pass http://localhost:8080/test/;
}
#请求地址:http://localhost/api/demo
#代理后的地址:http://localhost:8080/test/demo

location /api {
proxy_pass http://localhost:8080/test;
}
#请求地址:http://localhost/api/demo
#代理后的地址:http://localhost:8080/test/demo

location /api {
proxy_pass http://localhost:8080/test/;
}
#请求地址:http://localhost/api/demo
#代理后的地址:http://localhost:8080/test//demo
}

nginx常用配置项

nginx 一些常规配置项

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
http{
upstream upServer{
server 127.0.0.1:8001 weight=3;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
server 127.0.0.1:8004;
server 127.0.0.1:8005;
}

server{
listen 80;
server_name www.test.com;
access_log /var/log/web/test_access.log
error_log /var/log/web/test-error.log
location / {
root /data/wwwroot/testProject;
index index.html;
}
#反向代理设置
location /{
proxy_pass http://localhost:9000;
proxy_direct off;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_send_lowat 12000;
client_max_body_size 10m;
client_body_buffer_size 128k;
gzip on;
gzip_types text/plain;
gzip_min_length 1100;
gzip_buffers 4 8k;
charset utf-8;
}
#错误页面自定义
error 404 /404.html;

location /404.html{
root /spool/www
}
#负载均衡
location / {
proxy_pass http://upServer
}
#支持php
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#防盗链
location ~* \.(gif|jpg|png|swf|flv)$ {
valid_referers none blocked *.nginxcn.com;
if ($invalid_referer) {
rewrite ^/ [www.nginx.cn](http://www.nginx.cn/)
#return 404;
}
}
}
}

swoole-nginx

config info

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
42
43
44
45
46
47
48
49
50
51
#gzip on;
#gzip_min_length 1024;
#gzip_comp_level 2;
#gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml application/x-httpd-php image/jpeg image/gif image/png font/ttf font/otf image/svg+xml;
#gzip_vary on;
gzip_disable "msie6";
upstream swooles {
# 通过 IP:Port 连接
server 127.0.0.1:5300 weight=5 max_fails=3 fail_timeout=30s;
# 通过 UnixSocket Stream 连接,小诀窍:将socket文件放在/dev/shm目录下,可获得更好的性能
#server unix:/xxxpath/laravel-s-test/storage/laravels.sock weight=5 max_fails=3 fail_timeout=30s;
#server 192.168.1.1:5200 weight=3 max_fails=3 fail_timeout=30s;
#server 192.168.1.2:5200 backup;
keepalive 16;
}
server {
listen 80;
charset utf-8;
index index.html index.php;
server_name totok-cms.uwinltd.com;
access_log logs/totok-cms.uwinltd.com_swoole_access.log main;
error_log logs/totok-cms.uwinltd.com_swoole_error.log;
root /opt/onemena/wwwroot/uwinltd.com/totok-cms.uwinltd.com/public;
client_max_body_size 100m;
location / {
try_files $uri $uri/ @laravels;
}
# 当请求PHP文件时直接响应404,防止暴露public/*.php
#location ~* \.php$ {
# return 404;
#}
location @laravels {
# proxy_connect_timeout 60s;
# proxy_send_timeout 60s;
# proxy_read_timeout 120s;
# proxy_http_version 1.1;
# proxy_set_header Connection "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-PORT $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header Server-Protocol $server_protocol;
proxy_set_header Server-Name $server_name;
proxy_set_header Server-Addr $server_addr;
proxy_set_header Server-Port $server_port;
#proxy_pass http://127.0.0.1:5300$suffix;
proxy_pass http://swooles;
}

}

docker-compose

#docker-compose文件示例

#compose 版本 有 1,2,3 三个版本

1
version: "3"

##服务器信息

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
services:

#web服务器

web:
image: dockercloud/hello-world #镜像信息 本地有用本地,没有的话从远程获取

#容器名称
container_name: hello-world

#主机名称
hostname: hello-world.com

#本机端口映射到容器端口,YAML将会解析xx:yy这种数字格式为60进制,建议采用字符串格式
ports:
-"8080:8080"
-"127.0.0.1:9000:8080"

#向hosts文件中添加域名解析记录
extra_hosts:
-"www.test.com:162.185.32.65"

#挂载一个目录或已存在的数据卷容器,格式: [HOST:CONTAINER] or [HOST:CONTAINER:ro](只读)
volumes:
-./web/data_dir:/demo/data_dir
-./web/logs:/demo/logs

#不使用宿主目录而使用自定义驱动
volume_driver:yourdriver

#从另外服务或容器挂载数据卷
volumes_from:
-server_name
-container_name

#定义接入点,会覆盖dockerfile文件中的entrypoint
entrypoint:./
restart:

#服务器连接
links:
-database:database

#容器启动后执行的命令,例:composer install or ['composer','install']
command:composer install

#控制服务容器启动顺序,在web服务器启动前先启动databse服务及redis
depends_on:
-database
-redis

#基于其他模板进行扩展
extends:
-file:common.yaml
-service:common

#连接到docker-compose.yaml外部容器
external_links:
-project_db_1:mysql

#为容器添加Docker元数据(metadata)信息
lables:
decription:"web app service"

#日志驱动类型,有json-file|syslog|none三种类型
log_driver:
"josn-file"

#日志驱动参数
log_opt:
address:""

#增加指定容器内核能力
cap_add:
-ALL

#去掉指定容器内核能力
cap_drop:
-NET_ADMIN

#配置DNS服务器
dns:
-8.8.8.8
-4.4.4.4

#从文件中获取环境变量,优先级低于environment设置的环境变量
env_file:
-./env

#redis服务容器
redis:
image: redis
links:
-web
networks:
-back-tier

#数据库服务容器
database:
image: beginor/postgis:9.3
container_name: postgis
hostname: postgis
ports:
-5432:5432
volumes:
-./database/data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: 1q2w3e4R
restart:
networks:
front-tier:
driver: bridge
back-tier:
driver: bridge

你是本站第位访客 本站访客数人次
Fork me on GitHub