How to set and get a simple string value using SET and GET?
Sep 22, 2025 am 05:29 AMUse the SET command to set string values ??in Redis, such as SET mykey "Hello Redis", if the key already exists, it will be overwritten; use GET mykey to get string values, if it does not exist, it will be returned (nil). 1. The SET command can add EX parameters to set the expiration time, such as SET mykey "temporary" EX 10. 2. GET is only suitable for string types, and other types require special command operations. 3. It is recommended to have a clear name such as user:1001:name to avoid overwriting available SETNX and use TTL mykey to check the expiration time. Mastering SET and GET can achieve basic data access.
Redis is a commonly used in-memory database that supports multiple data types. The most basic operation is to set and get string values, which we use SET
and GET
commands to complete.
Use SET to set string values
In Redis, a key (key) can be associated with a string value (value) using the SET
command.
For example:
SET mykey "Hello Redis"
This command will set the value of the key mykey
to "Hello Redis"
. If this key already exists, its original value will be overwritten.
Pay attention to a few details:
- The value can be a number, text, or any string.
- If you want to set the expiration time, you can add
EX
parameters afterwards, such as:SET mykey "temporary" EX 10
Indicates that this key-value pair exists for only 10 seconds.
Use GET to get string values
After setting, you can use the GET
command to read the value of the corresponding key.
For example:
GET mykey
If mykey
exists, it will return its value; if it does not exist, it will return (nil)
.
Frequently Asked Questions:
- You can only use
GET
to get the value of the string type. If it is another type (such as list or hash), although the result can be returned, it is more appropriate to use it with dedicated command operations. - If you misuse a non-existent key, don't worry, Redis will not report an error, it just won't return the content.
Some suggestions for actual use
- Clear naming: For example, using
user:1001:name
to store username is more clear than usingname
. - Avoid overwriting important data: When using
SET
, remember to check whether the key has been occupied. You can also use theSETNX
command to avoid overwriting existing values. - Use TTL in combination to view expiration time: If the expiration time is set, you can use
TTL mykey
to see how many seconds there are left.
Basically that's it. Master SET
and GET
, and you can already use Redis to store and read the most basic data.
The above is the detailed content of How to set and get a simple string value using SET and GET?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

ArtGPT
AI image generator for creative art from text prompts.

Stock Market GPT
AI powered investment research for smarter decisions

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Running Redis with Docker without installing it on the host, it can be quickly started through the dockerrun command; it can customize configuration files and mount them to implement memory policies and other settings; it can persist data by naming volume redis-data; it is recommended to use DockerCompose to facilitate the deployment and maintenance of the development environment.

First add SpringDataRedis dependencies, then set Redis connection information in the configuration file, then enable cache through @EnableCaching and use cache annotations, and finally operate data through RedisTemplate or StringRedisTemplate to realize cache, session storage or high-speed data access.

UseFLUSHDBtoclearthecurrentdatabaseorFLUSHALLforalldatabases;bothsupportASYNC(background)orSYNC(blocking)modes,withASYNCpreferredinproductiontoavoidlatency.

Installing Redis can be done through APT or source code, and APT is simpler; 2. Update the package index and install redis-server; 3. Start and enable the power-on self-start; 4. Use redis-cliping to test PONG; 5. Optional configuration files to adjust bindings, passwords, etc.; 6. Restart the service and complete the installation.

HyperLogLog provides a memory efficient and fast unique count estimation method in Redis via PFADD and PFCOUNT commands. 1. HyperLogLog is a probability algorithm used to estimate the number of different elements in the dataset. It only requires a small amount of fixed memory to process large-scale datasets. It is suitable for tracking independent visitors or high-frequency search queries and other scenarios; 2. PFADD is used to add elements to HyperLogLog, and PFCOUNT returns the unique element estimate value in one or more structures; 3. Using meaningful key names, directly adding string values, and merging multiple HLLs to avoid repeated calculations are the best practices for using PFADD and PFCOUNT; 4. HyperLo

Redis transactions are implemented through commands such as MULTI, EXEC, WATCH, etc., providing command sequential execution and isolation. 1. Use MULTI to start the transaction, and EXEC commit, and all commands will be executed atomically; 2. If you use the WATCH monitoring key before EXEC, once the key is modified by other clients, the transaction will be aborted and retry; 3. The transaction does not support rollback, syntax errors cause EXEC to fail, and runtime errors will only affect a single command; 4. It is recommended to use Lua scripts for complex atomic operations. For example, when updating the balance, use WATCH to ensure the data is consistent. If EXEC returns nil, try again.

Serialized storage of JSON using strings is suitable for simple reading and writing. The RedisJSON module supports field-level operations to improve efficiency. The hash type is suitable for frequent update scenarios of flat structures. The selection is based on data access mode.

SETstoresakey-valuepair,optionallywithexpiryorexistenceconditions;2.GETretrievesthevaluebykey,returningnilifnonexistent;3.EXISTScheckskeypresence,returning1or0;4.DELremovesthekey.
