Redis five big data structures commonly used commands

linux Download the redis database.

apt install redis

If the right is not enough, the right is directly raised.

sudo apt install redis-server

 

linuxEnable and stop service

service redis start
service redis stop
service redis restart

xshellEnter Linux

cd ..  # Return to root directory
ls # see file
cd etc/

 

Well, it’s time to get to the point.

First, record.Connect, exit, switch databaseA command

Connect:redis:redis-cli
Sign out:exit
Switch database:select n

The database has no name, 16 by default, identified by 0-15, and the connection redis selects the first database by default (switched by select)

 

It has been said that redis has five big data structures. What do they have?

redisyeskey-valueData structure, each data is aKey value pair

The type of the key isCharacter string

Note: keys cannot be repeated.

There are five types of values:

  String ——> Character string

  Hash ——> Hash

  List ——> list

  set ——> aggregate

  Zset ——> Ordered set

 

Now let’s start with a detailed look at what commands are commonly used for each data type.

 

stringtype

stringIt is the most basic type of redis, and a key corresponds to a value.

Sets the value of a given key. If key has stored other values, SET overwrites the old value and ignores the type.

Setting data:set  key  value
Set multiple sets of data:mset key value  [key value..]

Setting values for multiple sets of keys is an atomic operation, either one set successfully or one set fails.

 

Returns the value of one or more key. If key does not exist, return nil, if key exists, but not string return nil

View data:get  key

 

Appends the specified value to the end of the key, creates and assigns if the key does not exist, and returns the length of the appended string

Additional data:append  key  value

 

Returns the remaining survival time of key, -1 indicates permanent existence, and -2 does not exist.

ttl key

 

At the same time setting the key, setting the expiration time (in seconds) key will no longer be available after the expiration, will be automatically deleted by the system.

set key value ex seconds 

set age 18 ex 20
Or setex key seconds value case: (setex sex 20 ‘male’)

 

Removes the lifetime of the specified key, returns 1 successfully, and 0 if the key does not exist or does not exist.

persist key

 

An additional order is added here.

Access to database:user db_name;

    If you do not know if the database exists, please add if exists.

 

Global key operation

Commands applicable to five data types of redis

rename key newkey    Rename

When key is the same as newkey or key does not exist, an error is returned. When newkey exists, it will be overwritten.

     keys *                    # View all key
     
     del key                    # Delete the number of successful returns
     
     exists key                # See if key exists or returns.
     
     type key                # View key type
     
     expire key seconds        #Set expiration date
        
     persist key                #Remove expiration date

 

listtype

ListType is a list of strings that can be added or deleted at the head or tail of the list.
When inserting data, the elements are added to the list’s head or tail according to the insertion order.
If the key does not exist, Redis will create one for the key.

Add data:rpush key value [value…]   Add data at the end
     lpush key value [value…] Add data to the header

 

Returns the value of the element in the list. Index starts from 0 and returns null when index exceeds the index.

lindex key index

  #View second

 

Look at the values of elements in index ranges.

View data:lrange key start stop

#View all values

 

Length of return list

llen key

 

Modify the data:lset key index value

AppointQuotation markMake a modification

 

Delete data:lpop key    Delete the first on the left.rpop key    Delete the first on the right.

 

Hashtype

It is a set of key values (key=> value) pairs. It is the mapping table of string type field and value.
     user                 { name:juhao,             age:18 }
user -> key(Key) name, age -> field (domain) juhao, 18 -> value (value)

 

Add data:

Set the field-value to the hash table. If the key does not exist, a new hash table will be created and reassigned, and the existing hash table will be overwritten.

hset key field value

 

View field values:

hget key field

 

Batch add:

hmset key field value field2 value2

 

View all value:

hvals key

 

View all field:

hkeys key

 

# Get multiple field

hmget key field[field...]

 

# Get all `field` and `value

hgetall key

 

 # See a couple of key values.

hlen key

 

# Determines whether the specified field in the hash table exists, returns 1, and returns 0 if key or field does not exist.

hexists key field

 

# delete

hdel key field

 

Settype

The element is string type.
unordered set
The elements are unique and not duplicated.

sadd key member [member...]     Added element

Add one or more member elements to the collection key. If member already exists, the element will be ignored.

 

# Returns the number of elements in set key.

scard key

 

 # Get all the elements in the collection

smembers key

 

 # There is a certain value in the judgement set.

Judge whether member exists in key and return to 0 or 1.

sismember key member

 

 # delete

 Remove one or more elements. Member that does not exist will be ignored, and the number of elements removed will be returned.

srem key member [member...]

 

# Random deletion

spop key

Removes and returns a random element in the collection, and returns to NULL when key does not exist.

 

 

zsettype

Similar to Set, the difference is that each member in Sorted has allocated one.Score (Score)Used to sort (ascending) members.
zsetThe members areOnlyBut the score (score) can be repeated.

 

# Add / modify

zadd key score member[ [score member] ..]

Set, exist, update

 

 

# See

zscore key member

View score value

 

# A member returned by index to key and withscores to display score.

zrange key start stop[withscores]

# Display all

  

 

# Returns the element of score in a given interval.

zrangebyscore key min max

 

# delete

zrem key member [member...]

Removes one or more elements in an ordered set and ignores if member does not exist.

 

# Delete elements whose index is in a given interval.

zremrangebyrank min max

 

# Delete elements of score in a given interval.

zremrangebyscore  min max

 

 

Write here, redis finishing, order not to memorize, with more natural to remember.

It’s like writing sql, creating a new table, inserting insert into, and querying the select * from table.

 

Author: half smile and half smile

Blog link: https://www.cnblogs.com/lixy-88428977

Statement: This is a summary of bloggers’ learning experience, and the level is limited. If inappropriate, please correct me. If you think it’s not bad, you are welcome to reprint it. Please quote the author and provenance.

Leave a Reply

Your email address will not be published. Required fields are marked *