[] Redis Redis based learning (II) Basic Redis command of the operation, persistence
1. Redis basic commands
1.1 Redis data structure
- redis is stored: data key, value format, wherein the key is a string, value five different data structures
- value data structure:
- String type string
- Hash type hash: map format
- List type list: linkedlist format. Support repeating elements
- Collection types set: do not allow duplicate elements
- Type ordered set sortedset: not allow duplicate elements, and sequential elements
- value data structure:
String String type 1.2
- Storage: set key value
127.0.0.1:6379> set username zhangsan
OK
- Get: get key
127.0.0.1:6379> get username
"zhangsan"
- Delete: del key
127.0.0.1:6379> del username
(integer) 1
1.3 Types hash hash
- Storage: hset key field value
127.0.0.1:6379> hset myhash username lisi
(integer) 1
127.0.0.1:6379> hset myhash password 123
(integer) 1
- Obtain:
- hget key field: obtaining a value corresponding to the specified field
127.0.0.1:6379> hget myhash username
"lisi"
- hgetall key: Get all the field and value
127.0.0.1:6379> hgetall myhash
1) "username"
2) "lisi"
3) "password"
4) "123"
- Delete: hdel key field
127.0.0.1:6379> hdel myhash username
(integer) 1
1.4 List Type list
You can add an element to the head of the list (on the left) or tails (to the right)
- Add to:
- lpush key value: the list of elements into the left table
- rpush key value: the elements are added to the right of the list
127.0.0.1:6379> lpush myList a
(integer) 1
127.0.0.1:6379> lpush myList b
(integer) 2
127.0.0.1:6379> rpush myList c
(integer) 3
- Gets: lrange key start end: acquiring range
127.0.0.1:6379> lrange myList 0 -1
1) "b"
2) "a"
3) "c"
- delete:
* lpop key: 删除列表最左边的元素,并将元素返回
* rpop key: 删除列表最右边的元素,并将元素返回
1.5 collection type set
Do not allow duplicate elements
- Storage: sadd key value
127.0.0.1:6379> sadd myset a
(integer) 1
127.0.0.1:6379> sadd myset a
(integer) 0
- Gets: smembers key: get all the elements in the collection set
127.0.0.1:6379> smembers myset
1) "a"
- Delete: srem key value: deleting an element set collection
127.0.0.1:6379> srem myset a
(integer) 1
1.6 indexed collections sortedset
We do not allow duplicate elements and sequential elements. Each element will be associated with a type of score double.
It is to redis from small to large order of collection of member passing score.
- Storage: zadd key score value
127.0.0.1:6379> zadd mysort 60 zhangsan
(integer) 1
127.0.0.1:6379> zadd mysort 50 lisi
(integer) 1
127.0.0.1:6379> zadd mysort 80 wangwu
(integer) 1
- 获取:zrange key start end [withscores]
127.0.0.1:6379> zrange mysort 0 -1
1) "lisi"
2) "zhangsan"
3) "wangwu"
127.0.0.1:6379> zrange mysort 0 -1 withscores
1) "zhangsan"
2) "60"
3) "wangwu"
4) "80"
5) "lisi"
6) "500"
- Delete: zrem key value
127.0.0.1:6379> zrem mysort lisi
(integer) 1
1.7-class instruction
1. keys * : 查询所有的键
2. type key : 获取键对应的value的类型
3. del key:删除指定的key value
2. endurance of
- redis is an in-memory database, when redis server restart, restart the computer acquisition, data will be lost, we can redis data memory persistence saved to the hard disk file.
- redis persistence mechanism:
- RDB
- AOF
2.1 RDB
The default mode, no configuration, default on the use of this mechanism
- A certain time interval, the detection of the change key, and persistent data
- Edit redis.windwos.conf file
# after 900 sec (15 min) if at least 1 key changed
save 900 1
# after 300 sec (5 min) if at least 10 keys changed
save 300 10
# after 60 sec if at least 10000 keys changed
save 60 10000
- Redis restart the server, and specify the configuration file name
D:\redis-2.8.9>redis-server.exe redis.windows.conf
2.2 AOF
Logging way, the recording operation of each command. After each command can operate, persistent data
- Edit redis.windwos.conf file
appendonly no(关闭aof) --> appendonly yes (开启aof)
appendfsync always : 每一次操作都进行持久化
appendfsync everysec : 每隔一秒进行一次持久化
appendfsync no : 不进行持久化