“This is my 39th day of participating in the First Challenge 2022. For more details: First Challenge 2022.”

The data structure

The Redis set object is also implemented in two ways: intSet and Hashtable. The underlying Hashtable is implemented through dict.

Intset coding

Intset-encoded collection objects use as the underlying implementation a collection of integers (INTSets) in which all the elements contained in the collection are stored.

127.0.0.1:6379> sadd numbers 12 3 4 5 (integer) 5 127.0.0.1:6379> Object Encoding numbers "intset"Copy the code

Hashtable coding

In addition, the underlying implementation of hashTable encoded collection objects uses dict objects, where each key is a string object, each string object contains a collection element, and the dictionary values are all set to NULL. (This is similar to the IMPLEMENTATION of HashSet in the JDK, which uses the HashMap implementation val to store an empty object.)

127.0.0.1:6379> sadd numbers "seven"
(integer) 1
127.0.0.1:6379> object encoding numbers
"hashtable"
Copy the code

Code conversion

Use intSet encoding if the following two conditions are met:

  1. The collection object holds that all elements are integer values
  2. The collection object holds a maximum of 512 elements

An example (for the second condition) :

127.0.0.1:6379> eval "for I =1, 512 do redis. Call ('sadd', KEYS[1], 1) integer (nil) 127.0.0.1:6379> scard integers (INTEGER) 512 127.0.0.1:6379> object encoding integers "intset"Copy the code

More than 512 values of int type are converted to a Hashtable store, and the underlying data set is dict

127.0.0.1:6379> sadd integers 8080 (INTEGER) 1 127.0.0.1:6379> Scard integers (INTEGER) 513 127.0.0.1:6379> object 6379 > encoding integers "hashtable" 127.0.0.1.Copy the code

Pay attention to the point

  • Conversion from intset to dict is irreversible
  • Set cannot be repeated
  • Support interchange and complement
  • To support theset-max-intset-entriesThe value can be modified in the configuration file
  • Dict is the underlying store and value is null
  • When intSet is the underlying object, the complexity of the object it looks for is O(logN).

Usage scenarios

  • Tags: mainly used for social memory of interest (be careful to ensure that the same transaction is completed)

    • User Adding a Label
    sadd user:1:tags tag1 tag2 tag4
    sadd user:2:tags tag1 tag2 tag5
    Copy the code
    • Record the user for the tag
    sadd tag1:users user:1 user:3
    sadd tag2:users user:1 user:2
    Copy the code
  • Compute tags of common interest to users

    sinter user:1:tags user:2:tags
    Copy the code
  • Random number in the lottery

    # spop/srandmember
    
    127.0.0.1:6379> sadd userids 1
    (integer) 1
    127.0.0.1:6379> sadd userids 2
    (integer) 1
    127.0.0.1:6379> sadd userids 3
    (integer) 1
    127.0.0.1:6379> srandmember userids
    "2"
    Copy the code

Common operation

The resources

  1. Redis Design and Implementation, Huang Jianhong
  2. www.runoob.com/redis/redis…