国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Database Mysql Tutorial redis學(xué)習(xí)筆記5(set)

redis學(xué)習(xí)筆記5(set)

Jun 07, 2016 pm 03:21 PM
redis set study notes

redis學(xué)習(xí)筆記5(set) 一、概述: 在Redis中,我們可以將Set類(lèi)型看作為沒(méi)有排序的字符集合,和List類(lèi)型一樣,我們也可以在該類(lèi)型的數(shù)據(jù)值上執(zhí)行添加、刪除或判斷某一元素是否存在等操作。需要說(shuō)明的是,這些操作的時(shí)間復(fù)雜度為O(1),即常量時(shí)間內(nèi)完成次操作

redis學(xué)習(xí)筆記5(set)

?

一、概述:

?

? ? ? 在Redis中,我們可以將Set類(lèi)型看作為沒(méi)有排序的字符集合,和List類(lèi)型一樣,我們也可以在該類(lèi)型的數(shù)據(jù)值上執(zhí)行添加、刪除或判斷某一元素是否存在等操作。需要說(shuō)明的是,這些操作的時(shí)間復(fù)雜度為O(1),即常量時(shí)間內(nèi)完成次操作。Set可包含的最大元素?cái)?shù)量是4294967295。

? ? ? 和List類(lèi)型不同的是,Set集合中不允許出現(xiàn)重復(fù)的元素,這一點(diǎn)和C++標(biāo)準(zhǔn)庫(kù)中的set容器是完全相同的。換句話說(shuō),如果多次添加相同元素,Set中將僅保留該元素的一份拷貝。和List類(lèi)型相比,Set類(lèi)型在功能上還存在著一個(gè)非常重要的特性,即在服務(wù)器端完成多個(gè)Sets之間的聚合計(jì)算操作,如unions、intersections和differences。由于這些操作均在服務(wù)端完成,因此效率極高,而且也節(jié)省了大量的網(wǎng)絡(luò)IO開(kāi)銷(xiāo)。

?

二、相關(guān)命令列表:

命令原型 時(shí)間復(fù)雜度 命令描述 返回值

SADD key member [member ...] O(N) 時(shí)間復(fù)雜度中的N表示操作的成員數(shù)量。如果在插入的過(guò)程用,參數(shù)中有的成員在Set中已經(jīng)存在,該成員將被忽略,而其它成員仍將會(huì)被正常插入。如果執(zhí)行該命令之前,該Key并不存在,該命令將會(huì)創(chuàng)建一個(gè)新的Set,此后再將參數(shù)中的成員陸續(xù)插入。如果該Key的Value不是Set類(lèi)型,該命令將返回相關(guān)的錯(cuò)誤信息。 本次操作實(shí)際插入的成員數(shù)量。

SCARD key O(1) 獲取Set中成員的數(shù)量。 返回Set中成員的數(shù)量,如果該Key并不存在,返回0。

SISMEMBER key member O(1) 判斷參數(shù)中指定成員是否已經(jīng)存在于與Key相關(guān)聯(lián)的Set集合中。 1表示已經(jīng)存在,0表示不存在,或該Key本身并不存在。

SMEMBERS key O(N) 時(shí)間復(fù)雜度中的N表示Set中已經(jīng)存在的成員數(shù)量。獲取與該Key關(guān)聯(lián)的Set中所有的成員。

返回Set中所有的成員。

SPOP key O(1) 隨機(jī)的移除并返回Set中的某一成員。 由于Set中元素的布局不受外部控制,因此無(wú)法像List那樣確定哪個(gè)元素位于Set的頭部或者尾部。 返回移除的成員,如果該Key并不存在,則返回nil。

SREM key member [member ...] O(N) 時(shí)間復(fù)雜度中的N表示被刪除的成員數(shù)量。從與Key關(guān)聯(lián)的Set中刪除參數(shù)中指定的成員,不存在的參數(shù)成員將被忽略,如果該Key并不存在,將視為空Set處理。 從Set中實(shí)際移除的成員數(shù)量,如果沒(méi)有則返回0。

SRANDMEMBER key O(1) 和SPOP一樣,隨機(jī)的返回Set中的一個(gè)成員,不同的是該命令并不會(huì)刪除返回的成員。 返回隨機(jī)位置的成員,如果Key不存在則返回nil。

SMOVE source destination member O(1) 原子性的將參數(shù)中的成員從source鍵移入到destination鍵所關(guān)聯(lián)的Set中。因此在某一時(shí)刻,該成員或者出現(xiàn)在source中,或者出現(xiàn)在destination中。如果該成員在source中并不存在,該命令將不會(huì)再執(zhí)行任何操作并返回0,否則,該成員將從source移入到destination。如果此時(shí)該成員已經(jīng)在destination中存在,那么該命令僅是將該成員從source中移出。如果和Key關(guān)聯(lián)的Value不是Set,將返回相關(guān)的錯(cuò)誤信息。 1表示正常移動(dòng),0表示source中并不包含參數(shù)成員。

SDIFF key [key ...] O(N) 時(shí)間復(fù)雜度中的N表示所有Sets中成員的總數(shù)量。返回參數(shù)中第一個(gè)Key所關(guān)聯(lián)的Set和其后所有Keys所關(guān)聯(lián)的Sets中成員的差異。如果Key不存在,則視為空Set。 差異結(jié)果成員的集合。

SDIFFSTORE destination key [key ...] O(N) 該命令和SDIFF命令在功能上完全相同,兩者之間唯一的差別是SDIFF返回差異的結(jié)果成員,而該命令將差異成員存儲(chǔ)在destination關(guān)聯(lián)的Set中。如果destination鍵已經(jīng)存在,該操作將覆蓋它的成員。 返回差異成員的數(shù)量。

SINTER key [key ...] O(N*M) 時(shí)間復(fù)雜度中的N表示最小Set中元素的數(shù)量,M則表示參數(shù)中Sets的數(shù)量。該命令將返回參數(shù)中所有Keys關(guān)聯(lián)的Sets中成員的交集。因此如果參數(shù)中任何一個(gè)Key關(guān)聯(lián)的Set為空,或某一Key不存在,那么該命令的結(jié)果將為空集。 交集結(jié)果成員的集合。

SINTERSTORE destination key [key ...] O(N*M) 該命令和SINTER命令在功能上完全相同,兩者之間唯一的差別是SINTER返回交集的結(jié)果成員,而該命令將交集成員存儲(chǔ)在destination關(guān)聯(lián)的Set中。如果destination鍵已經(jīng)存在,該操作將覆蓋它的成員。 返回交集成員的數(shù)量。?

SUNION key [key ...] O(N) 時(shí)間復(fù)雜度中的N表示所有Sets中成員的總數(shù)量。該命令將返回參數(shù)中所有Keys關(guān)聯(lián)的Sets中成員的并集。 并集結(jié)果成員的集合。

SUNIONSTORE destination key [key ...] O(N) 該命令和SUNION命令在功能上完全相同,兩者之間唯一的差別是SUNION返回并集的結(jié)果成員,而該命令將并集成員存儲(chǔ)在destination關(guān)聯(lián)的Set中。如果destination鍵已經(jīng)存在,該操作將覆蓋它的成員。 返回并集成員的數(shù)量。

三、命令示例:

?

? ?1. SADD/SMEMBERS/SCARD/SISMEMBER:

? ? #在Shell命令行下啟動(dòng)Redis的客戶端程序。

? ? /> redis-cli

? ? #插入測(cè)試數(shù)據(jù),由于該鍵myset之前并不存在,因此參數(shù)中的三個(gè)成員都被正常插入。

? ? redis 127.0.0.1:6379> sadd myset a b c

? ? (integer) 3

? ? #由于參數(shù)中的a在myset中已經(jīng)存在,因此本次操作僅僅插入了d和e兩個(gè)新成員。

? ? redis 127.0.0.1:6379> sadd myset a d e

? ? (integer) 2

? ? #判斷a是否已經(jīng)存在,返回值為1表示存在。

? ? redis 127.0.0.1:6379> sismember myset a

? ? (integer) 1

? ? #判斷f是否已經(jīng)存在,返回值為0表示不存在。

? ? redis 127.0.0.1:6379> sismember myset f

? ? (integer) 0

? ? #通過(guò)smembers命令查看插入的結(jié)果,從結(jié)果可以,輸出的順序和插入順序無(wú)關(guān)。

? ? redis 127.0.0.1:6379> smembers myset

? ? 1) "c"

? ? 2) "d"

? ? 3) "a"

? ? 4) "b"

? ? 5) "e"

? ? #獲取Set集合中元素的數(shù)量。

? ? redis 127.0.0.1:6379> scard myset

? ? (integer) 5

?

? ? 2. SPOP/SREM/SRANDMEMBER/SMOVE:

? ? #刪除該鍵,便于后面的測(cè)試。

? ? redis 127.0.0.1:6379> del myset

? ? (integer) 1

? ? #為后面的示例準(zhǔn)備測(cè)試數(shù)據(jù)。

? ? redis 127.0.0.1:6379> sadd myset a b c d

? ? (integer) 4

? ? #查看Set中成員的位置。

? ? redis 127.0.0.1:6379> smembers myset

? ? 1) "c"

? ? 2) "d"

? ? 3) "a"

? ? 4) "b"

? ? #從結(jié)果可以看出,該命令確實(shí)是隨機(jī)的返回了某一成員。

? ? redis 127.0.0.1:6379> srandmember myset

? ? "c"

? ? #Set中尾部的成員b被移出并返回,事實(shí)上b并不是之前插入的第一個(gè)或最后一個(gè)成員。

? ? redis 127.0.0.1:6379> spop myset

? ? "b"

? ? #查看移出后Set的成員信息。

? ? redis 127.0.0.1:6379> smembers myset

? ? 1) "c"

? ? 2) "d"

? ? 3) "a"

? ? #從Set中移出a、d和f三個(gè)成員,其中f并不存在,因此只有a和d兩個(gè)成員被移出,返回為2。

? ? redis 127.0.0.1:6379> srem myset a d f

? ? (integer) 2

? ? #查看移出后的輸出結(jié)果。

? ? redis 127.0.0.1:6379> smembers myset

? ? 1) "c"

? ? #為后面的smove命令準(zhǔn)備數(shù)據(jù)。

? ? redis 127.0.0.1:6379> sadd myset a b

? ? (integer) 2

? ? redis 127.0.0.1:6379> sadd myset2 c d

? ? (integer) 2

? ? #將a從myset移到myset2,從結(jié)果可以看出移動(dòng)成功。

? ? redis 127.0.0.1:6379> smove myset myset2 a

? ? (integer) 1

? ? #再次將a從myset移到myset2,由于此時(shí)a已經(jīng)不是myset的成員了,因此移動(dòng)失敗并返回0。

? ? redis 127.0.0.1:6379> smove myset myset2 a

? ? (integer) 0

? ? #分別查看myset和myset2的成員,確認(rèn)移動(dòng)是否真的成功。

? ? redis 127.0.0.1:6379> smembers myset

? ? 1) "b"

? ? redis 127.0.0.1:6379> smembers myset2

? ? 1) "c"

? ? 2) "d"

? ? 3) "a"

?

? ?3. SDIFF/SDIFFSTORE/SINTER/SINTERSTORE:

? ? #為后面的命令準(zhǔn)備測(cè)試數(shù)據(jù)。

? ? redis 127.0.0.1:6379> sadd myset a b c d

? ? (integer) 4

? ? redis 127.0.0.1:6379> sadd myset2 c

? ? (integer) 1

? ? redis 127.0.0.1:6379> sadd myset3 a c e

? ? (integer) 3

? ? #myset和myset2相比,a、b和d三個(gè)成員是兩者之間的差異成員。再用這個(gè)結(jié)果繼續(xù)和myset3進(jìn)行差異比較,b和d是myset3不存在的成員。

? ? redis 127.0.0.1:6379> sdiff myset myset2 myset3

? ? 1) "d"

? ? 2) "b"

? ? #將3個(gè)集合的差異成員存在在diffkey關(guān)聯(lián)的Set中,并返回插入的成員數(shù)量。

? ? redis 127.0.0.1:6379> sdiffstore diffkey myset myset2 myset3

? ? (integer) 2

? ? #查看一下sdiffstore的操作結(jié)果。

? ? redis 127.0.0.1:6379> smembers diffkey

? ? 1) "d"

? ? 2) "b"

? ? #從之前準(zhǔn)備的數(shù)據(jù)就可以看出,這三個(gè)Set的成員交集只有c。

? ? redis 127.0.0.1:6379> sinter myset myset2 myset3

? ? 1) "c"

? ? #將3個(gè)集合中的交集成員存儲(chǔ)到與interkey關(guān)聯(lián)的Set中,并返回交集成員的數(shù)量。

? ? redis 127.0.0.1:6379> sinterstore interkey myset myset2 myset3

? ? (integer) 1

? ? #查看一下sinterstore的操作結(jié)果。

? ? redis 127.0.0.1:6379> smembers interkey

? ? 1) "c"

? ? #獲取3個(gè)集合中的成員的并集。 ? ?

? ? redis 127.0.0.1:6379> sunion myset myset2 myset3

? ? 1) "b"

? ? 2) "c"

? ? 3) "d"

? ? 4) "e"

? ? 5) "a"

? ? #將3個(gè)集合中成員的并集存儲(chǔ)到unionkey關(guān)聯(lián)的set中,并返回并集成員的數(shù)量。

? ? redis 127.0.0.1:6379> sunionstore unionkey myset myset2 myset3

? ? (integer) 5

? ? #查看一下suiionstore的操作結(jié)果。

? ? redis 127.0.0.1:6379> smembers unionkey

? ? 1) "b"

? ? 2) "c"

? ? 3) "d"

? ? 4) "e"

? ? 5) "a"

?

四、應(yīng)用范圍:

?

? ? ? 1). 可以使用Redis的Set數(shù)據(jù)類(lèi)型跟蹤一些唯一性數(shù)據(jù),比如訪問(wèn)某一博客的唯一IP地址信息。對(duì)于此場(chǎng)景,我們僅需在每次訪問(wèn)該博客時(shí)將訪問(wèn)者的IP存入Redis中,Set數(shù)據(jù)類(lèi)型會(huì)自動(dòng)保證IP地址的唯一性。

? ? ? 2). 充分利用Set類(lèi)型的服務(wù)端聚合操作方便、高效的特性,可以用于維護(hù)數(shù)據(jù)對(duì)象之間的關(guān)聯(lián)關(guān)系。比如所有購(gòu)買(mǎi)某一電子設(shè)備的客戶ID被存儲(chǔ)在一個(gè)指定的Set中,而購(gòu)買(mǎi)另外一種電子產(chǎn)品的客戶ID被存儲(chǔ)在另外一個(gè)Set中,如果此時(shí)我們想獲取有哪些客戶同時(shí)購(gòu)買(mǎi)了這兩種商品時(shí),Set的intersections命令就可以充分發(fā)揮它的方便和效率的優(yōu)勢(shì)了。

Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Laravel8 optimization points Laravel8 optimization points Apr 18, 2025 pm 12:24 PM

Laravel 8 provides the following options for performance optimization: Cache configuration: Use Redis to cache drivers, cache facades, cache views, and page snippets. Database optimization: establish indexing, use query scope, and use Eloquent relationships. JavaScript and CSS optimization: Use version control, merge and shrink assets, use CDN. Code optimization: Use Composer installation package, use Laravel helper functions, and follow PSR standards. Monitoring and analysis: Use Laravel Scout, use Telescope, monitor application metrics.

How to use the Redis cache solution to efficiently realize the requirements of product ranking list? How to use the Redis cache solution to efficiently realize the requirements of product ranking list? Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot? What should I do if the Redis cache of OAuth2Authorization object fails in Spring Boot? Apr 19, 2025 pm 08:03 PM

In SpringBoot, use Redis to cache OAuth2Authorization object. In SpringBoot application, use SpringSecurityOAuth2AuthorizationServer...

Recommended Laravel's best expansion packs: 2024 essential tools Recommended Laravel's best expansion packs: 2024 essential tools Apr 30, 2025 pm 02:18 PM

The essential Laravel extension packages for 2024 include: 1. LaravelDebugbar, used to monitor and debug code; 2. LaravelTelescope, providing detailed application monitoring; 3. LaravelHorizon, managing Redis queue tasks. These expansion packs can improve development efficiency and application performance.

Laravel environment construction and basic configuration (Windows/Mac/Linux) Laravel environment construction and basic configuration (Windows/Mac/Linux) Apr 30, 2025 pm 02:27 PM

The steps to build a Laravel environment on different operating systems are as follows: 1.Windows: Use XAMPP to install PHP and Composer, configure environment variables, and install Laravel. 2.Mac: Use Homebrew to install PHP and Composer and install Laravel. 3.Linux: Use Ubuntu to update the system, install PHP and Composer, and install Laravel. The specific commands and paths of each system are different, but the core steps are consistent to ensure the smooth construction of the Laravel development environment.

Redis's Role: Exploring the Data Storage and Management Capabilities Redis's Role: Exploring the Data Storage and Management Capabilities Apr 22, 2025 am 12:10 AM

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

How to configure slow query log in centos redis How to configure slow query log in centos redis Apr 14, 2025 pm 04:54 PM

Enable Redis slow query logs on CentOS system to improve performance diagnostic efficiency. The following steps will guide you through the configuration: Step 1: Locate and edit the Redis configuration file First, find the Redis configuration file, usually located in /etc/redis/redis.conf. Open the configuration file with the following command: sudovi/etc/redis/redis.conf Step 2: Adjust the slow query log parameters in the configuration file, find and modify the following parameters: #slow query threshold (ms)slowlog-log-slower-than10000#Maximum number of entries for slow query log slowlog-max-len

In a multi-node environment, how to ensure that Spring Boot's @Scheduled timing task is executed only on one node? In a multi-node environment, how to ensure that Spring Boot's @Scheduled timing task is executed only on one node? Apr 19, 2025 pm 10:57 PM

The optimization solution for SpringBoot timing tasks in a multi-node environment is developing Spring...

See all articles