search
HomeDatabaseMysql TutorialRedis数据类型--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 

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AM

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL: Database Management System vs. Programming LanguageMySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AM

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL: Managing Data with SQL CommandsMySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AM

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL's Purpose: Storing and Managing Data EffectivelyMySQL's Purpose: Storing and Managing Data EffectivelyApr 16, 2025 am 12:16 AM

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

SQL and MySQL: Understanding the RelationshipSQL and MySQL: Understanding the RelationshipApr 16, 2025 am 12:14 AM

The relationship between SQL and MySQL is the relationship between standard languages ​​and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment