Redis basis

Redis common data structures

String,Hash,List,Set,SortedSet.

1. String (String type)

  • Is the most basic data type in Redis, onekeyCorresponds to avalue.
  • String is binary safe, which means redisstringCan contain any data. Such as numbers, strings, JPG images or serialized objects.

Actual combat scene:

  1. Cache: The classic usage scenario, the common information, strings, pictures or videos and other information into Redis, Redis as the cache layer, mysql as the persistence layer, reduce mysql read and write pressure.
  2. Counters: Redis is a single-threaded model, one command is executed before the next, and data can be dropped to other data sources.
  3. Session: Common solution Spring Session + Redis realizes session sharing

2. Hash

Is aMapmapThe value itself is a key-value pair structure, such asvalue={{field1,value1},...... fieldN,valueN}}

Actual combat scene:

  1. Cache: intuitive, more space saving than String, the maintenance of cache information, such as user information, video information, etc.

3. ()

List is simply a linked List (Redis uses the List of double-ended linked lists), which is ordered. Values can be repeated, and corresponding values can be extracted by subscript. Data can be inserted and deleted on the left and right sides.

The technique of using lists

  • Lpush + Lpop = Stack
  • Lpush + rpop = Queue
  • Lpush + LTRIM = Capped Collection
  • Lpush + brPOP = Message Queue

Actual combat scene:

  1. timeline: For example, the timeline of micro-bloglpushAdd a timeline to display new list information.

4. Set

A collection type is also an element used to hold multiple strings, but unlike a list, a collection type is:

  1. No duplicate elements are allowed;
  2. Elements in the collection are unordered and cannot be retrieved by index subscript;
  3. Support the operation between sets, you can take the intersection of multiple sets, union set, difference set.

Actual combat scene;

  1. Tag, you can tag the user, or the user can tag the message, so that the same tag or similar tag can be used to recommend things or people to follow.
  2. Like, or click on, favorites, etc., can be putsetIn the implementation.

5. Zset

The ordered set is necessarily related to the set and retains the feature that the set cannot have repeated members. The difference is that the elements in the ordered set can be sorted. It sets a score for each element as the basis of sorting.

(Elements in an ordered set cannot be repeated, but scores can be repeated, just as student numbers in a class cannot be repeated, but test scores can be the same).

Actual combat scene:

  1. Leaderboards: An ordered collection of classic usage scenarios. For example, websites such as novel videos need to make a ranking of novel videos uploaded by users. The ranking can be scored according to the number of users’ attention, update time, word count and so on.

Common relational databases and non-relational data and their differences🔗