Redis数据类型--string
Redis数据类型--string 五种数据类型:string, list, set, zset, hash 1). string类型 String是最简单的类型,一个key对应一个value,string类型是二进制安全的。Redis的string可以包含任何数据,比如jpg图片或者序列化的对象(PHP中对象序列化函数serialize)
Redis数据类型--string
五种数据类型:string, list, set, zset, hash
1). string类型
String是最简单的类型,一个key对应一个value,string类型是二进制安全的。Redis的string可以包含任何数据,比如jpg图片或者序列化的对象(PHP中对象序列化函数serialize)
内部实现,其本质是一个byte数组,字符串的大小被限制在512M以内
www.2cto.com
[plain]
struct sdshdr {
long len; //buf数组的长度
long free; //buf数组中剩余可用字节数
char buf[]; //存储实际字符串内容
}
操作方法:
a.set方法:
格式:set key value
设置key对应的值为string类型的value,OK=成功,0=失败, 若是该key已经存在,则覆盖其原有值。
[plain]
>set pwd 123456 //即添加一个pwd=123456的k-v
OK
b.get方法:
格式:get key
获取key对应的string值,如果key不存在返回nil
[plain]
>get pwd //即获取pwd对应的值
"123456"
>get name
(nil)
c.setnx方法:
格式:setnx key value
与set相同,不同的是:设置之前要检测key是否已经存在,如果key已经存在,则返回0,(nx = notexist)
www.2cto.com
[plain]
>setnx user zhangsan
(integer) 1
>setnx user lisi
(integer) 0
>get user
"zhangsan"
>set user chuangrain
OK
>get user
"chuangrain"
d. setex方法:
格式:setex key seconds value
设置key对应的值为string类型的value,并指定对此键值对应的有效期seconds
[plain]
>setex tea 10 food //即设置tea=food,指定10秒钟有效时间
OK
>get tea
"food"
>get tea //10s后
(nil)
e.setrange方法:
格式:setrange key offset value
www.2cto.com
用value参数调换指定key对应的字符串值 ,从偏移量offset开始,不存在的key当作空白字符串处理,如果offset大于key对应的字符串长度,那么原字符串和偏移量之间的空白将用零比特(zerobytes, "\x00")来填充,返回被setrange修改后的字符串长度。
[plain]
>set hw "hello word"
OK
>setrange hw 6 "Redis"
(integer)11
>get hw
"helloRedis"
>setrange hello 2 "chuang" //hello不存在
(integer) 12
>get hello
"\x00\x00chuangrain"
>setrange hello 15 rd
(integer) 17
>get hello
"\x00\x00chuangrain\x00\x00\x00rd"
f.mset方法
格式:mset key1 value1 key2 value2 …
一次设置多个key的值,成功返回OK,即全部设置成功,失败返回0,即没有任何设置,此操作具有原子性
g.msetnx方法:
格式:msetnx key1 value1 key2 value2 …
与mset类似,不同的是要检查设置的key是否已经存在,若存在返回0
h.getset方法:
格式:getset key value
设置key对应string类型的value,并返回key的旧值,如果key不存在,先设置key=value,再返回nil
www.2cto.com
[plain]
>get site
"taobao"
>getset site baidu
"taobao"
>get site
"baidu"
i.getrange方法:
格式:getrange key start end
获取key对应value的子字符串,从start开始到end结束,如果end(为正)小于 start(为正),或者key不存在,则返回空字符串"",如果end超出value的长度,则返 回start到value末尾之间的字符串,start和end可以为负,字符串左边的下标从0开始,右边的下标从-1开始。
0
1
2
3
4
b
a
i
d
u
-5
-4
-3
-2
-1
以"baidu"为例,即每个字符可能通过正、负来访问
[plain]
>get site
"baidu"
>getrange site 1 2
"ai"
>getrange site 3 -1
"du"
>getrange www 3 4 //www不存在
""
j.mget方法
格式:mget key1 key2 …
一次获取多个key对应的value值,如果对应的key不存在,则对应返回nil
www.2cto.com
[plain]
>mset user1 taobao user2 baidu
OK
>mget user1 user2 user3 //user3不存在
1) "taobao"
2) "baidu"
3) (nil)
k.incr方法:
格式:incr key
将指定key对应的value原子性的递增1,返回递增后的value值。如果key不存在,设其初始值为0,再递增1。如果value的值不是整型,则会返回失败信息。
www.2cto.com
[plain]
>mset key1 44 key2 1.1 key3 taobao
OK
>incr key1
(integer) 45
>get key1
"45"
>incr key2
(error)ERR value is not an integer or out of range
>incr key3
(error)ERR value is not an integer or out of range
>incr key4 //key4不存在
(integer) 1
l.incrby方法:
格式:incrby key decrement
与incr类似,将指定key对应的value原子性的递增decrement,返回递增后的value 值,如果key不存在, 设其初始值为0,再递增decrement。
m.decr方法:
格式:decr key
与incr作用相反,对key对应的value递减1操作,如果key不存在,则设置key 为初始值为0,再递减1。
[plain]
>decrdeepin //deepin不存在
(integer) -1
n.decrby方法:
www.2cto.com
格式:decrby key decrement
与incrby相反
o.strlen方法:
格式:strlen key
获取key对应的value的字符串长度,如果key不存在返回nil
p.append方法:
格式:append key value
如果key已经存在,将value的数据追加到已存在的value末尾,如果key不存在, 将创建一个新的key/value。返回新value的长度。
www.2cto.com
[plain]
>append site taobao
(integer) 11
>get site
"baidutaobao"
>append test taobao //test不存在
(integer) 6
>get test
"taobao"
q.setbit方法:
格式:setbit key offset value
设置在key对应指定offset上bit的值(value),该值只能为1或者0,返回该offset上原有的bit值。如果key不存在,将创建一个新值,并在指定的offset上设定bit值。如果offset大于value的字符串长度,redis将拉长value值并在指定offset上设置参数中 的bit值,之间添加的bit值为0。注:offset值必须大于0。
www.2cto.com
[plain]
>setbit userkey 7 1 //userkey不存在,从0开始到第2位bit值为1,返回原bit值0
(integer)0
>get userkey
"\x01"//即0000 0001转为16进制
r.getbit方法:
格式:getbit key offset
获取key对应offset上的bit值,如果offset大于value的长度,或者key不存在,或者非二进制型字符串,返回0。
www.2cto.com
[plain]
>getbit userkey 7
(integer) 1
>getbit userkey //user1key不存在
(integer) 0

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

Using Redis to lock operations requires obtaining the lock through the SETNX command, and then using the EXPIRE command to set the expiration time. The specific steps are: (1) Use the SETNX command to try to set a key-value pair; (2) Use the EXPIRE command to set the expiration time for the lock; (3) Use the DEL command to delete the lock when the lock is no longer needed.

Using the Redis directive requires the following steps: Open the Redis client. Enter the command (verb key value). Provides the required parameters (varies from instruction to instruction). Press Enter to execute the command. Redis returns a response indicating the result of the operation (usually OK or -ERR).

The best way to understand Redis source code is to go step by step: get familiar with the basics of Redis. Select a specific module or function as the starting point. Start with the entry point of the module or function and view the code line by line. View the code through the function call chain. Be familiar with the underlying data structures used by Redis. Identify the algorithm used by Redis.

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.
