redisが佳境に入る(02)Stringとhashタイプ
6256 ワード
前回はredisの簡単な紹介を行い、インストール、起動、停止、ログインなどの操作を行いました.次にredisをさらに学びます
サポートされるデータ型のstringとhash.
stringはあまり説明されず、redisに格納されているキー値ペアです.
次のコードを直接入力します.
サポートされるデータ型のstringとhash.
stringはあまり説明されず、redisに格納されているキー値ペアです.
次のコードを直接入力します.
[redies@localhost redis-2.4]$ redis-cli // redis
redis 127.0.0.1:6379> set name xiaobaiyang //set set
OK // OK
redis 127.0.0.1:6379> get name //get get
"xiaobaiyang" //
redis 127.0.0.1:6379> setnx name xiaobaiyang1 //setnx
(integer) 0 // name 0
redis 127.0.0.1:6379> get name
"xiaobaiyang" // name
redis 127.0.0.1:6379> setex age 10 red //setex 10s OK
redis 127.0.0.1:6379> get age
"red"
redis 127.0.0.1:6379> get age //10s age
(nil)
redis 127.0.0.1:6379> setnx age 20 // , age
(integer) 1
redis 127.0.0.1:6379> get age
"20"
redis 127.0.0.1:6379> set mail [email protected]
OK
redis 127.0.0.1:6379> setrange mail 6 bbbb//setrange
(integer) 21
redis 127.0.0.1:6379> get mail
"[email protected]"
redis 127.0.0.1:6379> mset key1 1 key2 2 key3 3 //mset
OK
redis 127.0.0.1:6379> get key1
"1"
redis 127.0.0.1:6379> get ke2
(nil)
redis 127.0.0.1:6379> get key2
"2"
redis 127.0.0.1:6379> get key3
"3"
redis 127.0.0.1:6379> msetnx key4 4 // setnx
(integer) 1
redis 127.0.0.1:6379> msetnx key4 key5
(integer) 0
redis 127.0.0.1:6379> msetnx key5 5 ket 6
(integer) 1
redis 127.0.0.1:6379> get ket
"6"
redis 127.0.0.1:6379> getset key5
55 // set "5"
redis 127.0.0.1:6379> getrange key5
01 // (error) ERR wrong number of arguments for 'getrange' command
redis 127.0.0.1:6379> getrange name 0 6
"xiaobai"
redis 127.0.0.1:6379> mget key1 key2 key3 //
1) "1"
2) "2"
3) "3"
redis 127.0.0.1:6379> incr age //
(integer) 21
redis 127.0.0.1:6379> incrby age 10 //
(integer) 31
redis 127.0.0.1:6379> incrby age -10
(integer) 21
redis 127.0.0.1:6379> decr age 10
(error) ERR wrong number of arguments for 'decr' command
redis 127.0.0.1:6379> decr age // (integer)
20
redis 127.0.0.1:6379> decrby age
(error) ERR wrong number of arguments for 'decrby' command
redis 127.0.0.1:6379> decrby age
1 // (integer) 19
redis 127.0.0.1:6379> append name ... // name ....
(integer) 14
redis 127.0.0.1:6379> get name
"xiaobaiyang..."
redis 127.0.0.1:6379> strlen name //
(integer) 14
redis 127.0.0.1:6379> del name //
(integer) 1
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> get name
(nil)
redis 127.0.0.1:6379> ============================ hash================= ##hash , , Map<map>
redis 127.0.0.1:6379> hset myhash1 name xiaobaiyang //hset
(integer) 1 //
redis 127.0.0.1:6379> hset myhash1 mai [email protected]
(integer) 1
redis 127.0.0.1:6379> hget myhash1 name // hget
"xiaobaiyang"
redis 127.0.0.1:6379> hmset myhash1 key1 1 key2 2 //
OK
redis 127.0.0.1:6379> hmget myhash1 name mai key1 key2 //
1) "xiaobaiyang"
2) "[email protected]"
3) "1"
4) "2"
redis 127.0.0.1:6379> hsetnx myhash1 name xxxx // setnx
(integer) 0
redis 127.0.0.1:6379> hget myhash1 name
"xiaobaiyang"
redis 127.0.0.1:6379> hsetnx myhash1 key3 3
(integer) 1
redis 127.0.0.1:6379> hget myhash1 key3
"3"
redis 127.0.0.1:6379> hset myhash1 age 11
(integer) 1
redis 127.0.0.1:6379> hincr myhash1 age //
(error) ERR unknown command 'hincr'
redis 127.0.0.1:6379> hincrby myhash1 age
(error) ERR wrong number of arguments for 'hincrby' command
redis 127.0.0.1:6379> hincrby myhash1 age 2 //
(integer) 13
redis 127.0.0.1:6379> hexists myhash1 age //
(integer) 1
redis 127.0.0.1:6379> hexists myhash1 age1
(integer) 0
redis 127.0.0.1:6379> hlen myhash1 //
(integer) 6
redis 127.0.0.1:6379> hdel myhsh1 //
(error) ERR wrong number of arguments for 'hdel' command
redis 127.0.0.1:6379> hdel myhash1 age1 //
(integer) 0
redis 127.0.0.1:6379> hexists myhash1 age
(integer) 1
redis 127.0.0.1:6379> hkeys myhash1// keys
1) "name"
2) "mai"
3) "key1"
4) "key2"
5) "key3"
6) "age"
redis 127.0.0.1:6379> hvals myhs //
(empty list or set)
redis 127.0.0.1:6379> hvals myhash1
1) "xiaobaiyang"
2) "[email protected]"
3) "1"
4) "2"
5) "3"
6) "13"
redis 127.0.0.1:6379> hgetall myhash1 // key values
1) "name"
2) "xiaobaiyang"
3) "mai"
4) "[email protected]"
5) "key1"
6) "1"
7) "key2"
8) "2"
9) "key3"
10) "3"
11) "age"
12) "13"
redis 127.0.0.1:6379> quit //