redisデータベースベースの基本操作
redisデータベースベースの基本操作
1、概要:
2、インストールと簡単なlinuxでの操作
redisサービス簡易操作(デフォルトポート6379)
3、データ型
1,string
2,hashesはフィールドと値のマッピングテーブルです.追加削除はすべて0,1です.ストレージオブジェクトを適用し、
3,list//チェーンテーブル、共通語スタックとキュー
4,set//無秩序集合
5,zset//秩序集合
1、概要:
redis,k-v , , , , , 。
push/pop,add/remove, , ,
redis ,
, ( )
2、インストールと簡単なlinuxでの操作
:https://redis.io/download
redisサービス簡易操作(デフォルトポート6379)
1, redis
./usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
//
2, :
./usr/local/redis/bin/redis-cli
3, redis
pkill /usr/local/redis/bin/redis-server
shutdown /usr/local/redis/bin/redis-server
4, :ps -ef | grep redis
5, :netstat -tunpl | grep 6379
3、データ型
1,string
, , , ,
( memcache )
set //
setnx // ,key 0,
setex //
setrange//
mset //
msetnx // , 0,
get
getset // ,
getrange//
mget //
incr //
incrby //
decr
decrby
append //
strlen //
:
127.0.0.1:6379> set name niushaogang
OK
127.0.0.1:6379> get name
"niushaogang"
127.0.0.1:6379> add name
(error) ERR unknown command 'add'
127.0.0.1:6379> add name niushaojing
(error) ERR unknown command 'add'
127.0.0.1:6379> get name
"niushaogang"
127.0.0.1:6379> append name niushaojing
(integer) 22
127.0.0.1:6379> get name
"niushaogangniushaojing"
127.0.0.1:6379> prepend name niuyongding
(error) ERR unknown command 'prepend'
127.0.0.1:6379> prepend 1
(error) ERR unknown command 'prepend'
127.0.0.1:6379> prepend name 1
(error) ERR unknown command 'prepend'
127.0.0.1:6379> get name
"niushaogangniushaojing"
127.0.0.1:6379> get id
(nil)
127.0.0.1:6379> get ni
(nil)
127.0.0.1:6379> get name
"niushaogangniushaojing"
127.0.0.1:6379> setns name sun
(error) ERR unknown command 'setns'
127.0.0.1:6379> setnx name sun
(integer) 0
127.0.0.1:6379> setex haircolor 10 red
OK
127.0.0.1:6379> get haircolor
"red"
127.0.0.1:6379> get haircolor
(nil)
127.0.0.1:6379> get name
"niushaogangniushaojing"
127.0.0.1:6379> set name
(error) ERR wrong number of arguments for 'set' command
127.0.0.1:6379> set name niushao
OK
127.0.0.1:6379> get name
"niushao"
127.0.0.1:6379> setrange name 3 gang
(integer) 7
127.0.0.1:6379> get name
"niugang"
127.0.0.1:6379> mset name1 niushao name2 gangshao
OK
127.0.0.1:6379> get name1
"niushao"
127.0.0.1:6379> get name2
"gangshao"
127.0.0.1:6379> mset name 3 ganggge
(error) ERR wrong number of arguments for MSET
127.0.0.1:6379> mset name3 gangge
OK
127.0.0.1:6379> get name3
"gangge"
127.0.0.1:6379> get name
"niugang"
127.0.0.1:6379> getset name niu
"niugang"
127.0.0.1:6379> get name
"niu"
127.0.0.1:6379> getrange name 2
(error) ERR wrong number of arguments for 'getrange' command
127.0.0.1:6379> get range 0 2
(error) ERR wrong number of arguments for 'get' command
127.0.0.1:6379> getrange name 0 2
"niu"
127.0.0.1:6379> getrange name 0 1
"ni"
127.0.0.1:6379> mget name name1 name2
1) "niu"
2) "niushao"
3) "gangshao"
127.0.0.1:6379> mset id1 3 id2 6 id3 9
OK
127.0.0.1:6379> get id1
"3"
127.0.0.1:6379> mget id1 id2 id3
1) "3"
2) "6"
3) "9"
127.0.0.1:6379> incr id1
(integer) 4
127.0.0.1:6379> incr id2
(integer) 7
127.0.0.1:6379> incr id2
(integer) 8
127.0.0.1:6379> incr id2
(integer) 9
127.0.0.1:6379> incr id2
(integer) 10
127.0.0.1:6379> incr id5
(integer) 1
127.0.0.1:6379> incr id5
(integer) 2
127.0.0.1:6379> incrby id5 10
(integer) 12
127.0.0.1:6379> incrby id5 10
(integer) 22
127.0.0.1:6379> decr id5
(integer) 21
127.0.0.1:6379> decr id5
(integer) 20
127.0.0.1:6379> decrby id5 5
(integer) 15
127.0.0.1:6379> append id5 niushao
(integer) 9
127.0.0.1:6379> get id5
"15niushao"
127.0.0.1:6379> strlen id5
(integer) 9
127.0.0.1:6379>
2,hashesはフィールドと値のマッピングテーブルです.追加削除はすべて0,1です.ストレージオブジェクトを適用し、
:
hset myhash key value//
hmset myhash key1 value1 key2 value2 ……
hget myhash key
hmget //hmget myhash key1 key2 key3
hsetnx
hincrby myhash key num //
hexists //
hlen //
hdel //
hkeys //
hvals //
hgetall //
:
127.0.0.1:6379> hset names:001 niushaogang
(error) ERR wrong number of arguments for 'hset' command
127.0.0.1:6379> hset names:001 name niushaogang
(integer) 1
127.0.0.1:6379> get names
(nil)
127.0.0.1:6379> hget name
(error) ERR wrong number of arguments for 'hget' command
127.0.0.1:6379> hget names::001
(error) ERR wrong number of arguments for 'hget' command
127.0.0.1:6379> hget names::001 name
(nil)
127.0.0.1:6379> hget names:001 name
"niushaogang"
127.0.0.1:6379> hsetnx names:002 name niushaojing
(integer) 1
127.0.0.1:6379> hset names:002 name
(error) ERR wrong number of arguments for 'hset' command
127.0.0.1:6379> hget names:002 name
"niushaojing"
127.0.0.1:6379> hmset names:003 name niushao sex nan age 20
OK
127.0.0.1:6379> hget names:003 sex
"nan"
127.0.0.1:6379> hmget names:003 name sex age
1) "niushao"
2) "nan"
3) "20"
127.0.0.1:6379> hincrby names:003 age 5
(integer) 25
127.0.0.1:6379> hget names:003 age
"25"
127.0.0.1:6379> hexists names age
(integer) 0
127.0.0.1:6379> hexists names:003 age
(integer) 1
127.0.0.1:6379> hlen names:003
(integer) 3
127.0.0.1:6379> hlen names:001
(integer) 1
127.0.0.1:6379> hlen names
(integer) 0
127.0.0.1:6379> hdel names:003 sex
(integer) 1
127.0.0.1:6379> hkeys names:003
1) "name"
2) "age"
127.0.0.1:6379> hvals names:003
1) "niushao"
2) "25"
127.0.0.1:6379> hgetall names:003
1) "name"
2) "niushao"
3) "age"
4) "25"
127.0.0.1:6379>
3,list//チェーンテーブル、共通語スタックとキュー
lpush //
lrange //lrange list 0 -1 list
rpush //
linsert // linsert list before list-value list-value
Lset //
lrem // lrem list key list-value-del
ltrim // ltrim list key1 key2
lpop // ,
rpop // ,
rpoplpush // list, ,
llen // list
:
[root@VM_173_6_centos bin]# ./redis-cli
127.0.0.1:6379> lpush list1 world
(integer) 1
127.0.0.1:6379> lpush list1 hello
(integer) 2
127.0.0.1:6379> lrange list1 0 -1
1) "hello"
2) "world"
127.0.0.1:6379> lrange list1 -1 0
(empty list or set)
127.0.0.1:6379> lpush list1 niushaogang
(integer) 3
127.0.0.1:6379> lrange list1 0 -1
1) "niushaogang"
2) "hello"
3) "world"
127.0.0.1:6379> rpush list1 niushao
(integer) 4
127.0.0.1:6379> lrange list1 -1 0
(empty list or set)
127.0.0.1:6379> lrange list1 0 -1
1) "niushaogang"
2) "hello"
3) "world"
4) "niushao"
127.0.0.1:6379> rpush list gang
(integer) 1
127.0.0.1:6379> rpush list1 gang
(integer) 5
127.0.0.1:6379> lrange list1 0 -1
1) "niushaogang"
2) "hello"
3) "world"
4) "niushao"
5) "gang"
127.0.0.1:6379> linset list1 before niushao gangshao
(error) ERR unknown command 'linset'
127.0.0.1:6379> linsert list1 before niushao gangshao
(integer) 6
127.0.0.1:6379> lrange list 0 -1
1) "gang"
127.0.0.1:6379> lrange list1 0 -1
1) "niushaogang"
2) "hello"
3) "world"
4) "gangshao"
5) "niushao"
6) "gang"
127.0.0.1:6379> Lset list1 0 gangge
OK
127.0.0.1:6379> lrange list1 0 -1
1) "gangge"
2) "hello"
3) "world"
4) "gangshao"
5) "niushao"
6) "gang"
127.0.0.1:6379> lrem list1 1 world
(integer) 1
127.0.0.1:6379> lrange list1 0 -1
1) "gangge"
2) "hello"
3) "gangshao"
4) "niushao"
5) "gang"
127.0.0.1:6379> ltrim list1 2 4
OK
127.0.0.1:6379> lrange list1 0 -1
1) "gangshao"
2) "niushao"
3) "gang"
127.0.0.1:6379> lpop list1
"gangshao"
127.0.0.1:6379> rpop list1
"gang"
127.0.0.1:6379> rpopllpush list1 ganggedingtianlidi
(error) ERR unknown command 'rpopllpush'
127.0.0.1:6379> rpoplpush list1 ganggenulichenggong
"niushao"
127.0.0.1:6379> lrange list1 0 -1
(empty list or set)
127.0.0.1:6379> lrange list1 0 -1
(empty list or set)
127.0.0.1:6379> lrange list1 0 -1
(empty list or set)
127.0.0.1:6379> lrange list1 0 -1
(empty list or set)
127.0.0.1:6379> llen list
(integer) 1
127.0.0.1:6379> llen list1
(integer) 0
127.0.0.1:6379> lpush list1 niu
(integer) 1
127.0.0.1:6379> lpush list1 gang
(integer) 2
127.0.0.1:6379> lpush list1 shao
(integer) 3
127.0.0.1:6379> lrange list1 0 -1
1) "shao"
2) "gang"
3) "niu"
127.0.0.1:6379> lpoplpush list list1
(error) ERR unknown command 'lpoplpush'
127.0.0.1:6379> rpoplpush list list1
"gang"
127.0.0.1:6379> lrange list1 0 -1
1) "gang"
2) "shao"
3) "gang"
4) "niu"
127.0.0.1:6379> llen list1
(integer) 4
4,set//無秩序集合
:
sadd
smember
srem
spop
sdiff
sdiffstore
sinter
sinterstore
sunion
sunionstore
smove
scard
sismember
srandmember
:
127.0.0.1:6379> sadd sets1 niu
(integer) 1
127.0.0.1:6379> asdd sets1 shao
(error) ERR unknown command 'asdd'
127.0.0.1:6379> sadd sets1 shao
(integer) 1
127.0.0.1:6379> sadd sets1 gang
(integer) 1
127.0.0.1:6379> sadd sets1 shao
(integer) 0
127.0.0.1:6379> smembers sets1
1) "gang"
2) "shao"
3) "niu"
127.0.0.1:6379> srem gang
(error) ERR wrong number of arguments for 'srem' command
127.0.0.1:6379> srem sets1 gang
(integer) 1
127.0.0.1:6379> spop sets1
"shao"
127.0.0.1:6379> smembers sets1
1) "niu"
127.0.0.1:6379> sadd sets1 one
(integer) 1
127.0.0.1:6379> sadd sets1 two
(integer) 1
127.0.0.1:6379> sadd sets1 three
(integer) 1
127.0.0.1:6379> sadd sets1 four
(integer) 1
127.0.0.1:6379> sadd sets2 four
(integer) 1
127.0.0.1:6379> sadd sets2 five
(integer) 1
127.0.0.1:6379> sdiff sets1 sets2
1) "one"
2) "three"
3) "niu"
4) "two"
127.0.0.1:6379> sdiff sets2 sets1
1) "five"
127.0.0.1:6379> sdiffstore sets3 sets1 sets2
(integer) 4
127.0.0.1:6379> smember sets3
(error) ERR unknown command 'smember'
127.0.0.1:6379> smembers sets3
1) "one"
2) "three"
3) "niu"
4) "two"
127.0.0.1:6379> sinter sets1 sets2
1) "four"
127.0.0.1:6379> sunion sets1 sets2
1) "two"
2) "niu"
3) "four"
4) "one"
5) "five"
6) "three"
127.0.0.1:6379> smove sets1 sets2 niu
(integer) 1
127.0.0.1:6379> smembers sets2
1) "four"
2) "five"
3) "niu"
127.0.0.1:6379> scard sets2
(integer) 3
127.0.0.1:6379> sinmember sets2 shao
(error) ERR unknown command 'sinmember'
127.0.0.1:6379> sismember sets2 shao
(integer) 0
127.0.0.1:6379> sismember sets2 niu
(integer) 1
127.0.0.1:6379> srandmember sets2
"niu"
127.0.0.1:6379> srandmember sets2
"niu"
127.0.0.1:6379> srandmember sets2
"five"
127.0.0.1:6379> srandmember sets2
5,zset//秩序集合
:
zadd // zadd set1 1 niu
zrange // withscores( )
zrem //
zincrby // zincrby set1 5 shao
zrank //
zrevrank //
zrevrang //
zrangebyscore// zrangebyscore set1 2 5 withscores
zcount // zcount set1 2 5 withscores
zcard //
zremrangebyrank//
zremrangebyscore// ( ) 。
:
127.0.0.1:6379> zaddz set1 1 niu
(error) ERR unknown command 'zaddz'
127.0.0.1:6379> zadd set1 1 niu
(integer) 1
127.0.0.1:6379> zadd set1 2 shao
(integer) 1
127.0.0.1:6379> zadd set1 3 gang
(integer) 1
127.0.0.1:6379> zrange sset1 0 -1
(empty list or set)
127.0.0.1:6379> zrange set1 0 -1
1) "niu"
2) "shao"
3) "gang"
127.0.0.1:6379> zrange set1 0 -1 withscore
(error) ERR syntax error
127.0.0.1:6379> zrange set1 0 -1 withscores
1) "niu"
2) "1"
3) "shao"
4) "2"
5) "gang"
6) "3"
127.0.0.1:6379> zrem set1 niu
(integer) 1
127.0.0.1:6379> zincrby set1 2 one
"2"
127.0.0.1:6379> zincrby set1 5 shao
"7"
127.0.0.1:6379> zrank set1 shao
(integer) 2
127.0.0.1:6379> zrange set1 0 -1
1) "one"
2) "gang"
3) "shao"
127.0.0.1:6379> zrevrank set1
(error) ERR wrong number of arguments for 'zrevrank' command
127.0.0.1:6379> zrevrank set1 0 -1 withscores
(error) ERR wrong number of arguments for 'zrevrank' command
127.0.0.1:6379> zervrank set1 shao
(error) ERR unknown command 'zervrank'
127.0.0.1:6379> zrevrank set2 four
(nil)
127.0.0.1:6379> zrevrank set2 one
(nil)
127.0.0.1:6379> zrevrank set1 one
(integer) 2
127.0.0.1:6379> zrevrange set1 0 -1 withscores
1) "shao"
2) "7"
3) "gang"
4) "3"
5) "one"
6) "2"
127.0.0.1:6379> zrangebyscore set1 0 1 withscores
(empty list or set)
127.0.0.1:6379> zrangebyscore set1 0 -1 withscores
(empty list or set)
127.0.0.1:6379> zrangebyscore set1 2 5 withscores
1) "one"
2) "2"
3) "gang"
4) "3"
127.0.0.1:6379> zcount set1 2 5 withscores
(error) ERR wrong number of arguments for 'zcount' command
127.0.0.1:6379> zcount set1 2 5 withscores
(error) ERR wrong number of arguments for 'zcount' command
127.0.0.1:6379> zcount set1 2 5 withscores
(error) ERR wrong number of arguments for 'zcount' command
127.0.0.1:6379> zcard set1
(integer) 3
127.0.0.1:6379> zremrangebyrank set1 2 2
(integer) 1
127.0.0.1:6379> zremrangebyscore set1 1 5
(integer) 2
127.0.0.1:6379>