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

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.> set hello world
OK> get hello
"world"> del hello
(integer) 1> get hello
(nil)> set counter 2
OK> get counter
"2"> incr counter
(integer) 3> get counter
"3"> incrby counter 100
(integer) 103> get counter
"103"> decr counter
(integer) 102> get counter

2.List list

The List in Redis is actually a linked list (Redis uses a double-ended linked list to implement List).> lpush mylist 1 2 ll ls mem
(integer) 5> lrange mylist 0 -1
1) "mem"
2) "ls"
3) "ll"
4) "2"
5) "1"> lindex mylist -1
"1"> lindex mylist 10        # index is not within the range of mylist

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.> sadd myset hao hao1 xiaohao hao
(integer) 3> smembers myset
1) "xiaohao"
2) "hao1"
3) "hao"> sismember myset hao
(integer) 1


Redis hash is a mapping table of field (field) and value (value) of string type. Hash is especially suitable for storing objects.> hset user name1 hao
(integer) 1> hset user email1
(integer) 1> hgetall user
1) "name1"
2) "hao"
3) "email1"
4) ""> hget user user
(nil)> hget user name1
"hao"> hset user name2 xiaohao
(integer) 1> hset user email2 2776837181@qq.con
(integer) 1> hgetall user
1) "name1"
2) "hao"
3) "email1"
4) ""
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> zadd myscoreset 100 hao 90 xiaohao
(integer) 2> zrange myscoreset 0 -1
1) "xiaohao"
2) "hao"

Tags: Database Redis Cache

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