Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common commands for keys and strings in Redis

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail what are the common commands about keys and strings in Redis. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Redis related knowledge

The default port number for Redis is 6379

There are 16 databases by default, similar to array subscript starting from 0, and library 0 is used by default initially.

Use the command select to switch databases. Such as select 8.

Unified password management, all libraries have the same password.

Dbsize looks at the number of key in the current database.

Flushdb empties the current library.

Flushall kills the whole library.

Redis is a single thread + multiplex IO multiplexing technology.

Multiplexing refers to using a single thread to check the ready state of multiple file descriptors (Socket), such as calling select and poll functions, passing in multiple file descriptors, and returning if one file descriptor is ready, otherwise blocking until the timeout. After getting the ready state, the real operation can be performed in the same thread, or you can start thread execution (such as using a thread pool).

Serial VS multithreading + lock (memcached) VS single threading + multiplex IO multiplexing (Redis)

Redis differs from Memcache in three ways:

Support for multiple data types

Support for persistence

Single thread + multiplex IO multiplexing

Data types in Redis

Redis key (key)

Keys *: view all key of the current library (match: keys * 1)

Exists key: determines whether a key exists.

Type key: check what type of key you have.

Del key: deletes the specified key data

Unlink key: select non-blocking deletion based on value. Only the keys is removed from the keyspace metadata, and the real deletion is performed asynchronously later.

Expire key 10:10 seconds to set the expiration time for the given key

Ttl key: check how many seconds are left to expire.-1 means never expires, and-2 means expired.

Select command toggles database

Dbsize looks at the number of key in the current database.

Flushdb empties the current library.

Flushall kills all libraries

Redis string (String)

String is the most basic type of Redis, and one key corresponds to one value.

The String type is binary safe. It means that the string of Redis can contain any data. Such as jpg pictures.

Or serialized objects.

The String type is the most basic data type of Redis, and the string value in a Redis can be up to 512m.

Common command

Set, get, etc.

Set: add key-value pairs.

When the set value of key is set to a new value, the new value will overwrite the old one.

* NX: when key does not exist in the database, you can add key-value to the database.

* XX: when key exists in the database, you can add key-value to the database, which is mutually exclusive with the NX parameter.

* EX: the number of seconds that key timed out.

* PX: the number of milliseconds of the timeout of key, which is mutually exclusive with EX.

Get query the corresponding key value.

Append sets the given

< value >

Append to the end of the original value

Strlen gets the length of the value.

Setnx sets the value of key only if key does not exist.

Increase or decrease the built value

Incr

Increase the numeric value stored in key by 1.

Can only operate on numeric values. If empty, the new increment is 10.

Decr

Subtract the numeric value stored in key by 1.

You can only operate on numeric values. If empty, the new increment is-1.

Inrjy/ decrby increases or subtracts the numeric values stored in key. Custom step size.

Note:

Incr and decr are atomic operations

ITunes + in java is not an atomic operation.

Mset, mget and msetnx

Mset....

Set one or more key-value pairs simultaneously.

Mget

Get one or more value at the same time.

Msetnx

It's atomic.

Set one or more key-value pairs at the same time, if and only if none of the given key exists.

One that existed before was unsuccessful.

Getrange 、 setrange

Getrange

Get the range of values, similar to substring in java, front package, back package

Setrange

Overrides the stored string value from the

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report