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 is the difference between setex command and set command in redis

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces what are the differences between the setex command and the set command in redis, which can be used for reference by interested friends. I hope you can learn a lot after reading this article.

Introduction:

The Redis Setex command sets the value and its expiration time for the specified key. If key already exists, the SETEX command replaces the old value.

The basic syntax for redis Setex commands is as follows:

Redis 127.0.0.1 6379 > SETEX KEY_NAME TIMEOUT VALUE

SETEX command Redis string (String) command

SETEX key seconds value

Associate the value to the key and set the lifetime of the key to seconds in seconds.

If key already exists, SETEX will overwrite the old value

This command is similar to two commands:

$redis- > SET ('key',' value'); $redis- > EXPIRE (' key','seconds'); # set time to live

The difference is that SETEX is an atomic operation, and the two actions of associating values and setting time to live are done at the same time, which is very useful when Redis is used as a cache.

Return value:

Returns OK if the setting is successful.

An error is returned when the seconds parameter is illegal.

# case 1:key does not exist $redis- > SETEX ('cache_user_id', 60JO100001); / / bool (true) echo $redis- > GET (' cache_user_id'); # value / / "100001" echo $redis- > TTL ('cache_user_id'); # remaining time to live / / int (56) # case 2:key already exists, key is overwritten $redis- > SET (' cd', "timeless"); / bool (true) $redis- > SETEX ('cd', 3000, "goodbye my love"); / / bool (true); echo $redis- > GET (' cd'); / / "goodbye my love" Thank you for reading this article carefully. I hope the article "what is the difference between the setex command and the set command in redis" shared by the editor will be helpful to you. At the same time, I hope you will support us, pay attention to the industry information channel, and more related knowledge is waiting for you to learn!

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

Database

Wechat

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

12
Report