# redis常见操作
# shell操作redis
echo "get hamweather_weather.52867" |redis-cli > ham_cond.txt
1
# python操作redis
import redis
self.redis = redis.Redis(host=os.getenv('REDIS_HOST'), port=os.getenv('REDIS_PORT'), db=0)
# 特别的 zadd 和正常顺序不一样,据说严格模式是一致的。
self.redis.zadd(key, member, score)
self.redis.setex(key, v, 900)
1
2
3
4
5
2
3
4
5
# php操作redis
date_default_timezone_set("Asia/Shanghai");
ini_set('memory_limit','256M');
echo('auto send notice start time :'.date('Y-m-d H:i:s',time())."\n");
$redis=new Redis();
$redis_host=getenv('REDIS_HOST');
$redis_port=getenv('REDIS_PORT');
$redis->connect($redis_host,$redis_port);
$redis->select(0);
function redis_init($db=0){
$host = getenv("GARDEN_REDIS_HOST");
$port = getenv("GARDEN_REDIS_PORT");
$redis = new Redis();
$redis->connect($host, $port);
$redis->select($db);
return $redis;
}
// scan操作
$iter = null;
while (true) {
$keys = $redis->scan($iter, "mo:rtypecount:reqkey:hour:*:set");
if ($keys === false) {
return;
}
foreach ($keys as $index_key) {
$hour = explode(":", $index_key)[4];
if($hour <= $hour_del){
$ret_keys = $redis->smembers($index_key);
foreach($ret_keys as $key){
$redis->del($key);
}
$redis->del($index_key);
}
}
}
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
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
# go操作redis
import "github.com/gomodule/redigo/redis"
count_redis_host := os.Getenv("REDIS_ONLINE_HOST")
count_redis_port := os.Getenv("REDIS_ONLINE_PORT")
count_redis,r_err := redis.Dial("tcp",count_redis_host+":"+count_redis_port)
count_redis.Do("SELECT", 1)
// 进程退出时,需要释放的资源
defer count_redis.Close()
// 单个key操作
r_res, _ := redis.String(con.Do("get", key1))
if r_res != "" {
con.Do("EXPIRE", "key1", "1296000")
}
// 批量pipeline方式
redis.Send("GET",key1)
redis.Send("GET",key2)
redis.Flush()
for i := 0; i < 2; i++ {
res,r_err := redis.String(adx_cluster_redis.Receive())
if r_err != nil {
continue
}
fmt.Println(res)
}
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
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
# openresty-lua操作redis
local redis = require "resty.redis"
function _M.connect()
local red = redis:new()
red:set_timeout(5000) -- 5 sec timeout
--red:set_keepalive(0, 500)
local ok, err = red:connect(host, port)
if not ok then
if err then print("connect redis error "..err) end
return false,err
else
return red,nil
end
end
-- 放入连接池,不可以真的关闭
function _M.closeDB(conn)
if conn then
--conn:close()
conn:set_keepalive(5000,200)
end
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23