Redis installation and data type

Data type classification

Storage data type classification:

Structured data: This data can be expressed in a two-digit table format.
Unstructured data: This type of data is not convenient to express in a two-dimensional table format.

According to different types of stored data, it is classified into two different types of databases:

sql structured database:

The type of data stored inside is structured data. (Alternatively known as relational database)
A structured database, created on the basis of a relational model,
generally oriented to records,
including Orcale, MySQL, SQL Server, Microsoft Access, DB2, etc.

nosql unstructured database:

The type of data stored in it is unstructured data. (
Alternatively known as non-relational databases) All databases other than mainstream relational databases are non-relational databases,
including Redis, MongBD, Hbase, CouhDB

The background of the new non-relational database

High performance High concurrent read and write requirements for the database
Huge Storage requirements for efficient storage and access of massive data
High Scalability requirements for high scalability and high availability of the database

Introduction to Redis

Redis runs on memory and supports persistence. It
adopts a key-value (key-value pair) storage form.
Advantages It
has extremely high data read and write speed.
Supports rich data types.
Supports data persistence.
Atomicity.
Supports data backup.

Redis supports five data types: string (string), hash (hash), list (list), set (collection) and zset (sorted set: ordered set).

Types of Introduction characteristic Scenes
String Binary security Can contain any data, such as jpg pictures or serialized objects, and a key can store up to 512M
Hash (dictionary) A collection of key-value pairs, that is, the Map type in programming languages It is suitable for storing objects, and can modify only a certain attribute value like updating an attribute in the database (Memcached needs to take out the entire string and deserialize it into an object after modification and then serialize and save it back) Store, read, modify user attributes
List Linked List (Doubly Linked List) Add and delete fast, provide an API to manipulate a certain section of elements 1, the latest news ranking and other functions (such as the timeline of the circle of friends) 2, message queue
Set Hash table implementation, elements are not repeated 1. Add, delete, and find the complexity of O(1) 2. Provide operations such as intersection, union, and difference for sets 1. Common friends 2. Use uniqueness to count all independent IPs that visit the website 3. When friends recommend, find the intersection according to the tag, and recommend it if it is greater than a certain threshold
Sorted Set Add a weight parameter score to the elements in Set, and the elements are arranged in order by score When the data is inserted into the collection, it has been naturally sorted 1. Leaderboard 2. Message queue with weight

Redis installation

Insert picture description here
1. Upload files and unzip

tar xf redis-5.0.4.tar.gz

2. Compile and install

cd redis-5.0.4/
make
make PREFIX=/usr/local/redis install

3. Establish a soft link

ln -s /usr/local/redis/bin/* /usr/bin/

4. Execute the script

cd redis-5.0.4/utils/
./install_server.sh
 /usr/local/redis/bin/redis-server

Insert picture description here
Port: 6379
Config file: /etc/redis/6379.conf Configuration file path\color{red}{Configuration file path}With opposing Wen member passage diameter
Log file: /var/log/redis_6379.loglog file path \ color {red} {} log file pathDay Zhi Wen member passage diameter
Data dir: / var / lib /data file path \ color {red} {} data file pathThe number of data packets member passage diameter
Executable: / usr / local / redisexecutable file path \ color {red} {} executable pathMay be performed row text member passage diameter
Cli Executable: / usr / local /client command tools \ color {red} {} client command toolsCustomer households end command to make work tool

netstat -anput | grep redis

Insert picture description here

5. Redis command

[root@server6 ~]# /etc/init.d/redis_6379 stopClose \color{red}{close}Turned off
Stopping ...
Waiting for the shutdown to the Redis ...
the Redis stopped
[the root Server6 @ ~] # /etc/init.d/redis_6379 StartOpen \ color {red} {} openOpen start
Starting the Redis Server ...
[the root Server6 @ ~] # /etc/init.d/redis_6379 the restartrestarting \ color {red} {} rebootRe- start
Stopping ...
Waiting for the shutdown to the Redis ...
the Redis stopped
Starting the Redis Server ...
[the root Server6 @ ~] # /etc/init.d/redis_6379 Statuscheck the status of \ color {red} {} view statusTo check see -like state
Redis is running (62607)

vi /etc/redis/6379.conf

Insert picture description here

redis-cli -h 20.0.0.16 -p 6379

Insert picture description here

type of data

1.String (string)

String is the most basic type of redis. You can understand it as exactly the same type as Memcached. A key corresponds to a value.
The string type is binary safe. This means that the redis string can contain any data. Such as jpg images or serialized objects.
The string type is the most basic data type of Redis, and the value of the string type can store up to 512MB.

Example: set key value to set key value to get key to get the value of the key

20.0.0.16:6379> Set string1 6
ok
20.0.0.16:6379>Get string1
“6”
20.0.0.16:6379>Incr string1
(integer) 7
20.0.0.16:6379> Decr string1
(integer) 6
20.0.0.16:6379> Decrby string1 3
(integer) 3
20.0.0.16:6379>Incrby string1 5
(integer) 8
20.0.0.16:6379>

2.Hash (hash)

Redis hash is a collection of key-value (key=>value) pairs.
Redis hash is a mapping table between field and value of string type, and hash is particularly suitable for storing objects.

Example: Hset: add hash data hget: get hash data hmget: get multiple hash data

20.0.0.16:6379> Hset hash1 key1 a
(integer) 1
20.0.0.16:6379> Hset hash1 key2 b
(integer) 1
20.0.0.16:6379> Hset hash1 key3 c
(integer) 1
20.0.0.16:6379> Hmget hash1 key1 key2 key3
1) “a”
2) “b”
3) “c”

20.0.0.16:6379> Hset hash1 field1 a1 field2 b2
(integer) 2
20.0.0.16:6379> Hmget hash1 field1 field2
1) “a1”
2) “b2”
20.0.0.16:6379>

3. List (list)

The Redis list is a simple list of strings, sorted in the order of insertion. You can add an element to the head (left) or tail (right) of the list.

Example: lpush: push the value from the left lpop: pop the value from the left rpush: push the value from the right rpop: pop the value from the right llen: view the length of a certain list data type

20.0.0.16:6379> Lpush list1 4
(integer) 1
20.0.0.16:6379> Lpush list1 5
(integer) 2
20.0.0.16:6379>Lpush list1 6
(integer) 3
20.0.0.16:6379>Llen list1
(integer) 3
20.0.0.16:6379>Rpop list1
“4”
20.0.0.16:6379> Llen list1
(integer) 2

20.0.0.16:6379> Lpush list2 4
(integer) 1
20.0.0.16:6379> Lpush list2 5
(integer) 2
20.0.0.16:6379>Lpush list2 6
(integer) 3
20.0.0.16:6379>Lpop list2
“6”
20.0.0.16:6379>Lpop list2
“5”
20.0.0.16:6379> Llen list2
(integer) 1
20.0.0.16:6379>

4.Set(集合)

Redis 的 Set 是 string 类型的无序集合。
集合是通过哈希表实现的,所以添加,删除,查找的复杂度都是 O(1)。
sadd 命令
添加一个 string 元素到 key 对应的 set 集合中,成功返回 1,如果元素已经在集合中返回 0。

sadd key member
scard:查看set数据中存在的元素个数
sismember:判断set数据中是否存在某个元素
srem:删除某个set数据中的元素

实例:

20.0.0.16:6379> sadd set1 12
(integer) 1
20.0.0.16:6379> sadd set1 13
(integer) 1
20.0.0.16:6379> sadd set1 12
(integer) 0
20.0.0.16:6379> Scard set1
(integer) 2
20.0.0.16:6379> Sismember set1 11
(integer) 0
20.0.0.16:6379> Sismember set1 13
(integer) 1
20.0.0.16:6379>Srem set1 13
(integer) 1
20.0.0.16:6379>Sismember set1 13
(integer) 0
20.0.0.16:6379> Smembers set1
1) “12”
20.0.0.16:6379>

5.zset(sorted set:有序集合)

Redis zset 和 set 一样也是string类型元素的集合,且不允许重复的成员。
不同的是每个元素都会关联一个double类型的分数。redis正是通过分数来为集合中的成员进行从小到大的排序。
zset的成员是唯一的,但分数(score)却可以重复。

zadd 命令
添加元素到集合,元素在集合中存在则更新对应score
使用方法:zadd key score member
zcard:查询
zrang:数据排序

Example:
20.0.0.16:6379> Zadd zset1 1.1 val1
(integer) 1
20.0.0.16:6379> Zadd zset1 2.2 val2
(integer) 1
20.0.0.16:6379> Zadd zset1 3.3 val3
(integer) 1
20.0.0.16:6379> Zadd zset1 3.3 val10
(integer) 1
20.0.0.16:6379> Zcard zset1
(integer) ) 4
20.0.0.16:6379> Zrangebyscore zset1 0 10
1) “val1”
2) “val2”
3) “val10”
4) “val3”
20.0.0.16:6379> Zrange zset1 0 10 withscores
1) “val1”
2) “1.1000000000000001”
3) “val2”
4 ) “2.2000000000000002”
5) “val10”
6) “3.2999999999999998”
7) “val3”
8) “3.2999999999999998”
20.0.0.16:6379>

Guess you like

Origin blog.csdn.net/weixin_50345511/article/details/111270611