redis 交集、并集、差集的具體使用
更新時間:2021年02月23日 14:12:56 作者:xiaojin21cen
這篇文章主要介紹了redis 交集、并集、差集的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
一、sinter 、sunion 、sdiff
redis 支持 Set集合的數(shù)據存儲,其中有三個比較特殊的方法:
- sinter key [key …] 查看一個集合的全部成員,該集合是所有給定集合的交集。
- sunion key [key …] 查看一個集合的全部成員,該集合是所有給定集合的并集。
- sdiff key [key …] 查看所有給定 key 與第一個 key 的差集
1.1、sinter 交集的示例
redis> SMEMBERS group_1 1) "LI LEI" 2) "TOM" 3) "JACK" redis> SMEMBERS group_2 1) "HAN MEIMEI" 2) "JACK" redis> SINTER group_1 group_2 # 取的是交集的數(shù)據 1) "JACK"
1.2、sunion 并集的示例
redis> SMEMBERS songs 1) "Billie Jean" redis> SMEMBERS my_songs 1) "Believe Me" redis> SUNION songs my_songs # 取的是集合的并集數(shù)據據 1) "Billie Jean" 2) "Believe Me"
1.3、sdiff 差集的示例
redis> SMEMBERS peter_movies 1) "bet man" 2) "start war" 3) "2012" redis> SMEMBERS joe_movies 1) "hi, lady" 2) "Fast Five" 3) "2012" redis> SDIFF peter_movies joe_movies # 取的是兩個集合的差集的數(shù)據 1) "bet man" 2) "start war"
二、sinterstore、sunionstore、sdiffstore
- sinterstore destination key [key …] 將 交集 數(shù)據存儲到某個對象中
- sunionstore destination key [key …] 將 并集 數(shù)據存儲到某個對象中
- sdiffstore destination key [key …] 將 差集 數(shù)據存儲到某個對象中
2.1、sinterstore 交集的示例
redis> SMEMBERS songs 1) "good bye joe" 2) "hello,peter" redis> SMEMBERS my_songs 1) "good bye joe" 2) "falling" redis> SINTERSTORE song_interset songs my_songs # 將交集的數(shù)據存儲到 song_interset 對象中 (integer) 1 redis> SMEMBERS song_interset # 查看 song_interset 對象中的 所有數(shù)據 1) "good bye joe"
2.2、sunionstore 并集的示例
redis> SMEMBERS NoSQL 1) "MongoDB" 2) "Redis" redis> SMEMBERS SQL 1) "sqlite" 2) "MySQL" redis> SUNIONSTORE db NoSQL SQL # 將并集的數(shù)據存儲到 db 對象中 (integer) 4 redis> SMEMBERS db # 查看 db 對象中的 所有數(shù)據 1) "MySQL" 2) "sqlite" 3) "MongoDB" 4) "Redis"
2.3、sdiffstore 差集的示例
redis> SMEMBERS joe_movies 1) "hi, lady" 2) "Fast Five" 3) "2012" redis> SMEMBERS peter_movies 1) "bet man" 2) "start war" 3) "2012" redis> SDIFFSTORE joe_diff_peter joe_movies peter_movies # 將差集的數(shù)據存儲到 joe_diff_peter 對象中 (integer) 2 redis> SMEMBERS joe_diff_peter # 查看 joe_diff_peter 對象中的 所有數(shù)據 1) "hi, lady" 2) "Fast Five"
到此這篇關于redis 交集、并集、差集的具體使用的文章就介紹到這了,更多相關redis 交集、并集、差集內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關文章
通過prometheus監(jiān)控redis實時運行狀態(tài)的操作方法
本文詳細介紹了如何通過Prometheus監(jiān)控Redis的運行狀態(tài),包括安裝配置Redis、Redis Exporter以及Prometheus,配置Prometheus監(jiān)控Redis指標,以及常見的Redis指標和告警規(guī)則,需要的朋友可以參考下2025-02-02

