Getting Started with Redis Basics

1. Concept

Redis is an in-memory cache database. The full name of Redis is: Remote Dictionary Server (remote data service), written in C language. Redis is a key-value storage system (key-value storage system) that supports rich data types, such as: String, list, set, zset, hash . Redis is a storage system that supports key-value and other data structures.
It can be used in scenarios such as caching, event publishing or subscription, high-speed queues, etc. Support the network, provide direct access to strings, hashes, lists, queues, and collection structures, memory-based, and persistent.

Second, the characteristics of Redis

1. Excellent read and write performance: Redis can read 110,000 times/s and write 81,000 times/s
2. Rich data types: Redis supports Strings, Lists, Hashes, Sets and Ordered Sets data type operations for binary cases.
3. Atomicity: All operations of redis are atomic
4. Rich features
5. Persistence: redis supports persistence: allows data to be persisted to hard disk
6. Publish Subscribe
7. Distributed
...

3. Redis 5 basic data types

For redis, all key s are strings.
redis mainly includes five common data types, namely: String, List, Set, Zset, and Hash.

1.string string

Features:
1.String is the most basic data type in redis, a key corresponds to a value.
2. The string of redis can contain any data. Such as numbers, strings, jpg images or serialized objects.

127.0.0.1:6379> set hello world
OK
127.0.0.1:6379> get hello
"world"
127.0.0.1:6379> del hello
(integer) 1
127.0.0.1:6379> get hello
(nil)
127.0.0.1:6379> set counter 2
OK
127.0.0.1:6379> get counter
"2"
127.0.0.1:6379> incr counter
(integer) 3
127.0.0.1:6379> get counter
"3"
127.0.0.1:6379> incrby counter 100
(integer) 103
127.0.0.1:6379> get counter
"103"
127.0.0.1:6379> decr counter
(integer) 102
127.0.0.1:6379> get counter
"102"

2.List list

The List in Redis is actually a linked list (Redis uses a double-ended linked list to implement List).

127.0.0.1:6379> lpush mylist 1 2 ll ls mem
(integer) 5
127.0.0.1:6379> lrange mylist 0 -1
1) "mem"
2) "ls"
3) "ll"
4) "2"
5) "1"
127.0.0.1:6379> lindex mylist -1
"1"
127.0.0.1:6379> lindex mylist 10        # index is not within the range of mylist
(nil)

3.Set collection

A Redis Set is an unordered collection of type String. Collection members are unique, which means that duplicate data cannot appear in the collection.

127.0.0.1:6379> sadd myset hao hao1 xiaohao hao
(integer) 3
127.0.0.1:6379> smembers myset
1) "xiaohao"
2) "hao1"
3) "hao"
127.0.0.1:6379> sismember myset hao
(integer) 1

4.Hash

Redis hash is a mapping table of field (field) and value (value) of string type. Hash is especially suitable for storing objects.

127.0.0.1:6379> hset user name1 hao
(integer) 1
127.0.0.1:6379> hset user email1 2776837181@qq.com
(integer) 1
127.0.0.1:6379> hgetall user
1) "name1"
2) "hao"
3) "email1"
4) "2776837181@qq.com"
127.0.0.1:6379> hget user user
(nil)
127.0.0.1:6379> hget user name1
"hao"
127.0.0.1:6379> hset user name2 xiaohao
(integer) 1
127.0.0.1:6379> hset user email2 2776837181@qq.con
(integer) 1
127.0.0.1:6379> hgetall user
1) "name1"
2) "hao"
3) "email1"
4) "2776837181@qq.com"
5) "name2"
6) "xiaohao"
7) "email2"
8) "2776837181@qq.con"

5.Zset ordered collection

Redis ordered collections, like collections, are also collections of elements of type string, and duplicate members are not allowed

127.0.0.1:6379> zadd myscoreset 100 hao 90 xiaohao
(integer) 2
127.0.0.1:6379> zrange myscoreset 0 -1
1) "xiaohao"
2) "hao"

Tags: Database Redis Cache

Posted by James138 on Fri, 03 Jun 2022 23:50:41 +0530