Redis实战(3)数据类型一Strings
Redis的作者antirez(SalvatoreSanfilippo)曾经发表了一篇名为Redis宣言(RedisManifesto)的文章,文中列举了Redis的七个原则,以向大家阐明Redis的思..
Redis 的作者 antirez(Salvatore Sanfilippo)曾经发表了一篇名为 Redis 宣言(Redis Manifesto)
的文章,文中列举了 Redis 的七个原则,以向大家阐明 Redis 的思想。
1、Redis 是一个操作数据结构的语言工具,它提供基于 TCP 的协议以操作丰富的数据结构。
在 Redis 中,数据结构这个词的意义不仅表示在某种数据结构上的操作,更包括了结构本身
及这些操作的时间空间复杂度。
2、Redis 定位于一个内存数据库,正是由于内存的快速访问特性,才使得 Redis 能够有如此
高的性能,才使得 Redis 能够轻松处理大量复杂的数据结构,Redis 会尝试其它的存储方面
的选择,但是永远不会改变它是一个内存数据库的角色。
3、Redis 使用基础的 API 操作基础的数据结构,Redis 的 API 与数据结构一样,都是一些最基
础的元素,你几乎可以将任何信息交互使用此 API 格式表示。作者调侃说,如果有其它非人
类的智能生物存在,他们也能理解 Redis 的 API。因为它是如此的基础。
4、Redis 有着诗一般优美的代码,经常有一些不太了解 Redis 有的人会建议 Redis 采用一些
其它人的代码,以实现一些 Redis 未实现的功能,但这对我们来说就像是非要给《红楼梦》
接上后四十回一样。
5、Redis 始终避免复杂化,我们认为设计一个系统的本质,就是与复杂化作战。我们不会为了一个小功能而往源码里添加上千行代码,解决复杂问题的方法就是让复杂问题永远不要提复杂的问题。
6、Redis 支持两个层成的 API,第一个层面包含部分操作 API,但它支持用于分布式环境下
的 Redis。第二个层面的 API 支持更复杂的 multi-key 操作。它们各有所长,但是我们不会推
出两者都支持的 API,但我们希望能够提供实例间数据迁移的命令,并执行 multi-key 操作。
7、我们以优化代码为乐,我们相信编码是一件辛苦的工作,唯一对得起这辛苦的就是去享
受它。如果我们在编码中失去了乐趣,那最好的解决办法就是停下来。我们决不会选择让Redis 不好玩的开发模式。
Redis 的作者 antirez 曾笑称 Redis 为一个数据结构服务器(data structures server)
,这是一个非常准确的表述,Redis 的所有功能就是将数据以其固有的几种结构来保存,并提供
给用户操作这几种结构的接口。本文将介绍 Redis 支持的各种数据类型及其操作接口。
strings 类型及操作
string 是最简单的类型,你可以理解成与 Memcached 是一模一样的类型,一个 key 对应一个
value,其上支持的操作与 Memcached 的操作类似。但它的功能更丰富。
string 类型是二进制安全的。意思是 redis 的 string 可以包含任何数据,比如 jpg 图片或者序
列化的对象。从内部实现来看其实 string 可以看作 byte 数组,最大上限是 1G 字节,下面是
string 类型的定义:
len 是 buf 数组的长度。
free 是数组中剩余可用字节数,由此可以理解为什么 string 类型是二进制安全的了,因为它
本质上就是个 byte 数组,当然可以包含任何数据了
buf 是个 char 数组用于存贮实际的字符串内容,其实 char 和 c#中的 byte 是等价的,都是一
个字节。
另外 string 类型可以被部分命令按 int 处理.比如 incr 等命令,如果只用 string 类型,redis 就
可以被看作加上持久化特性的 memcached。
当然 redis 对 string 类型的操作比 memcached 还是多很多的,具体操作方法如下:
set:
设置 key 对应的值为 string 类型的 value。
例如我们添加一个 name= HongWan 的键值对,可以这样做:
setnx:
设置 key 对应的值为 string 类型的 value。
如果 key 已经存在,返回 0, nx是 not exist 的意思。
例如我们添加一个 name= HongWan_new 的键值对,可以这样做:
setex:
设置 key 对应的值为 string 类型的 value,并指定此键值对应的有效期。
例如我们添加一个 haircolor= red 的键值对,并指定它的有效期是 10 秒,可以这样做:
可见由于最后一次的调用是 10 秒以后了,所以取不到 haicolor 这个键对应的值。
setrange:
设置指定 key 的 value 值的子字符串。
例如我们希望将 HongWan 的 126 邮箱替换为 gmail 邮箱,那么我们可以这样做:
其中的 5 是指从下标为 5(包含 5)的字符开始替换
mset:
一次设置多个 key 的值,成功返回 ok 表示所有的值都设置了,失败返回 0 表示没有任何值被设置。
msetnx:

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

AI Hentai Generator
Generate AI Hentai for free.

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

The methods of the php8 data type include converting strings to integers, converting integers to strings, converting strings to floating point numbers, converting floating point numbers to strings, converting arrays to strings, converting strings to arrays, and converting Boolean values to integers. Integer conversion to Boolean value and variable type determination and conversion. Detailed introduction: 1. Converting a string to an integer includes the intval() function and (int) forced type conversion; 2. Converting an integer to a string includes the strval() function and (string) forced type conversion; 3. Converting a string to a float Points and so on.

In a MySQL database, gender fields can usually be stored using the ENUM type. ENUM is an enumeration type that allows us to select one as the value of a field from a set of predefined values. ENUM is a good choice when representing a fixed and limited option like gender. Let's look at a specific code example: Suppose we have a table called "users" that contains user information, including gender. Now we want to create a field for gender, we can design the table structure like this: CRE

In MySQL, the most suitable data type for gender fields is the ENUM enumeration type. The ENUM enumeration type is a data type that allows the definition of a set of possible values. The gender field is suitable for using the ENUM type because gender usually only has two values, namely male and female. Next, I will use specific code examples to show how to create a gender field in MySQL and use the ENUM enumeration type to store gender information. The following are the steps: First, create a table named users in MySQL, including

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

Python data types are: 1. Integer type; 2. Floating point type; 3. Complex number; 4. Boolean type; 5. String; 6. List; 7. Tuple; 8. Set; 9. Dictionary. Detailed introduction: 1. Integer type, used to represent integers, which can be positive, negative, or zero. In Python, the range of values that integers can represent is platform-specific; 2. Floating point type, used to represent numbers with decimal parts. Numbers, floating point type can represent positive numbers, negative numbers and zero; 3. Complex numbers, used to represent complex numbers, including real and imaginary parts; 4. Boolean type, used to represent Boolean values, etc.

When designing database tables, choosing the appropriate data type is very important for performance optimization and data storage efficiency. In the MySQL database, there is really no so-called best choice for the data type to store the gender field, because the gender field generally only has two values: male or female. But for efficiency and space saving, we can choose a suitable data type to store the gender field. In MySQL, the most commonly used data type to store gender fields is the enumeration type. An enumeration type is a data type that can limit the value of a field to a limited set.

Detailed explanation of how to use Boolean types in MySQL MySQL is a commonly used relational database management system. In practical applications, it is often necessary to use Boolean types to represent logical true and false values. There are two representation methods of Boolean type in MySQL: TINYINT(1) and BOOL. This article will introduce in detail the use of Boolean types in MySQL, including the definition, assignment, query and modification of Boolean types, and explain it with specific code examples. 1. The Boolean type is defined in MySQL and can be

C language is a widely used computer programming language that is efficient, flexible and powerful. To be proficient in programming in C language, you first need to understand its basic syntax and data types. This article will introduce the basic syntax and data types of C language and give examples. 1. Basic syntax 1.1 Comments In C language, comments can be used to explain the code to facilitate understanding and maintenance. Comments can be divided into single-line comments and multi-line comments. //This is a single-line comment/*This is a multi-line comment*/1.2 Keyword C language
