#!/bin/sh
#
# Simple Redis init.d script conceived to work on linux systems
# as it does use of the /proc filesystem.
REDISPORT=6379
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/var/run/redis_${REDISPORT}.pid
CONF=/etc/redis/${REDISPORT}.conf
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
::
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exists, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/$PID ]
do
echo "Waiting for Redis to shutdown..."
sleep 1
done
echo "Redis stopped"
fi
::
*)
echo "Please use start or stop as first argument"
::
esac
del可以删除多个键值,返回值为删除的个数。del命令的参数不支持通配符,但可以通过linux的实现批量删除redis-cli DEL $(redis-cli KEYS "user:*")(有长度限制)来达到效果,效果比xargs效果更好。
获取keyvalue值的类型
12345678910
127.0.0.1:6379> set foo 1
OK
127.0.0.1:6379> lpush foo 1
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> lpush foa 1
(integer) 1
127.0.0.1:6379> type foo
string
127.0.0.1:6379> type foa
list
set key value
get key
incr key # 对应的值需为数值
set foo 1
incr foo
set foo b
incr foo
# (error) ERR value is not an integer or out of range
# 增加指定的整数
incrby key increment
decr key
decr key decrement
increbyfloat key increment
append key value
strlen key # 字节数,和java字符串的length不同
mget key [key ...]
mset key value [key value ...]
getbit key offset
setbit key offset value
bitcount key [start] [end]
bitop operation destkey key [key ...] # AND OR XOR NOT
set foo1 bar
set foo2 aar
BITOP OR res foo1 foo2 # 位操作命令可以非常紧凑地存储布尔值
GET res
散列值
12345678910111213141516
hset key field value
hget key field
hmset key field value [field value ...]
hmget key field [field ...]
hgetall key
hexists key field
hsetnx key field value # 当字段不存在时赋值 if not exists
hincrby key field increment
hdel key field [field ...]
hkeys key # 仅key
hvals key # 仅value
hlen key # 字段数量
列表
双端队列型列表
123456789101112131415161718192021
lpush key value [value ...]
rpush key value [value ...]
lpop key
rpop key
llen key
lrange key start stop # 可以使用负索引,从0开始,包括最右边的元素
lrem key count value
# 删除列表中前count个值为value的元素,返回的是实际删除的元素个数。
# count为负数是从右边开始删除
# count为0时删除所有值为value的元素
# 获得/设置指定索引的元素值
lindex key index # index为负数是从右边开始
lset key index value
ltrim key start end # 只保留列表指定的片段
linsert key BEFORE/AFTER pivotvalue value
poplpush source destination # 将元素从给一个列表转到另一个列表
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set key 1
QUEUED
127.0.0.1:6379> sadd key 2
QUEUED
127.0.0.1:6379> set key 3
QUEUED
127.0.0.1:6379> exec
1) OK
2) (error) WRONGTYPE Operation against a key holding the wrong kind of value
3) OK
127.0.0.1:6379> get key
"3"
127.0.0.1:6379> watch key
OK
127.0.0.1:6379> set key 2
OK
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set key 3
QUEUED
127.0.0.1:6379> exec
(nil)
127.0.0.1:6379> get key
"2"
127.0.0.1:6379> lpush tag:ruby:posts 1 2 3
(integer) 3
127.0.0.1:6379> hmset post:1 time 140801 name HelloWorld
OK
127.0.0.1:6379> hmset post:2 time 140802 name HelloWorld2
OK
127.0.0.1:6379> hmset post:3 time 140803 name HelloWorld3
OK
127.0.0.1:6379> sort tag:ruby:posts BY post:*->time desc
1) "3"
2) "2"
3) "1"
127.0.0.1:6379> sort tag:ruby:posts BY post:*->time DESC GET post:*->name
1) "HelloWorld3"
2) "HelloWorld2"
3) "HelloWorld"
一个sort命令中可以有多个GET参数(而BY参数只能有一个),所以还可以这样用:
1234567
127.0.0.1:6379> sort tag:ruby:posts BY post:*->time desc GET post:*->name GET post:*->time
1) "HelloWorld3"
2) "140803"
3) "HelloWorld2"
4) "140802"
5) "HelloWorld"
6) "140801"
如果还需要返回文章ID,可以使用GET #获得,也就是返回元素本身的值。
12345678910
127.0.0.1:6379> sort tag:ruby:posts BY post:*->time desc GET post:*->name GET post:*->time GET #
1) "HelloWorld3"
2) "140803"
3) "3"
4) "HelloWorld2"
5) "140802"
6) "2"
7) "HelloWorld"
8) "140801"
9) "1"
localtimes=redis.call('incr', KEYS[1])
if times==1 then
redis.call('expire', KEYS[1], ARGV[1])
end
if times>tonumber(ARGV[2]) then
return 0
end
return 1
# redis-cli --eval ratelimiting.lua rate.limiting:127.0.0.1 , 10 3 逗号前的是键,后面的是参数
lua语法(和shell脚本有点像,更简洁)
123456789101112
本地变量 local x=10
注释 --xxx
多行注释 --[[xxxx]]
赋值 local a,b=1,2 # a=1, b=2
local a={1,2,3}
a[1]=5
数字操作符的操作数如果是字符串会自动转成数字
tonumber
tostring
只要操作数不是nil或者false,逻辑操作符就认为操作数为真,否则为假!
用..来实现字符串连接
取长度 print(#"hello") -- 5
local result={}
for i,v in ipairs(KEYS) do
result[i]=redis.call("HGETALL", v)
end
return result
获取并删除有序集合中分数最小的元素
12345
local element=redis.call("ZRANGE", KEY[1], 0, 0)[1]
if element the
redis.call('ZREM', KEYS[1], element)
end
return element
处理JSON
123456789
local sum=0
local users=redis.call('mget', unpack(KEYS))
for _,user in ipairs(users) do
local courses=cjson.decode(user).course
for _,score in pairs(courses) do
sum=sum+score
end
end
return sum