1. 程式人生 > >redis 在 php 中的應用(Set篇)

redis 在 php 中的應用(Set篇)

之間 進行 group center 集合運算 返回 world cut 數據

上一篇:redis 在 php 中的應用(List篇)

本文為我閱讀了 redis參考手冊 之後編寫,註意 php_redis 和 redis-cli 的區別(主要是返回值類型和參數用法)

Redis的 Set 是 string 類型的無序集合。集合成員是唯一的,這就意味著集合中不能出現重復的數據。

目錄:

Set(集合)
SADD SREM SMEMBERS SCARD SMOVE SPOP SRANDMEMBER
SINTER SINTERSTORE SUNION0 SUNIONSTORE SDIFF SDIFFSTORE SISMEMBER


Set(集合)

1、SADD

Redis Sadd 命令將一個或多個成員元素加入到集合中

(1)已經存在於集合的成員元素將被忽略。

(2)假如集合 key 不存在,則創建一個只包含添加的元素作成員的集合。

(3)當集合 key 不是集合類型時,返回一個錯誤。

註意:在Redis2.4版本以前, SADD 只接受單個成員值。

語法:

redis 127.0.0.1:6379> SADD KEY_NAME VALUE1..VALUEN

返回值: 被添加到集合中的新元素的數量,不包括被忽略的元素。

可用版本:>= 1.0.0

時間復雜度:(N),N是被添加的元素的數量。

具體實例:

<?php
$redis
= new redis(); $redis -> connect(‘127.0.0.1‘,6379); $redis -> flushAll(); $redis -> sAdd(‘myset‘,‘hello‘); $redis -> sAdd(‘myset‘,‘foo‘); $redis -> sAdd(‘myset‘,‘hello‘); // 已存在的 key 被忽略 var_dump($redis -> sMembers(‘myset‘)); //array (size=2) // 0 => string ‘hello‘ (length=5) // 1 => string ‘foo‘ (length=3)

2、SREM

Redis Srem 命令用於移除集合中的一個或多個成員元素,不存在的成員元素會被忽略。

當 key 不是集合類型,返回一個錯誤。

在 Redis 2.4 版本以前, SREM 只接受單個成員值。

語法:

redis 127.0.0.1:6379> SREM KEY MEMBER1..MEMBERN

返回值: 被成功移除的元素的數量,不包括被忽略的元素。

可用版本:>= 1.0.0

時間復雜度:O(N),N為給定member元素的數量。

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> sAdd(‘myset‘,‘hello‘);
$redis -> sAdd(‘myset‘,‘foo‘);
$redis -> sAdd(‘myset‘,‘world‘);
$redis -> sAdd(‘myset‘,‘hi‘);
$redis -> sAdd(‘myset‘,‘welcome‘);

var_dump($redis -> sRem(‘myset‘,‘hello‘,‘foo‘));    // int 2
var_dump($redis -> sMembers(‘myset‘));
//array (size=3)
//  0 => string ‘world‘ (length=5)
//  1 => string ‘welcome‘ (length=7)
//  2 => string ‘hi‘ (length=2)

3、SMEMBERS

Redis Smembers 命令返回集合中的所有的成員。 不存在的集合 key 被視為空集合。

語法:

redis 127.0.0.1:6379> SMEMBERS KEY VALUE

返回值: 集合中的所有成員。

可用版本:>= 1.0.0

時間復雜度:O(N),N為集合的基數。

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> sAdd(‘myset‘,‘hello‘);
$redis -> sAdd(‘myset‘,‘foo‘);
$redis -> sAdd(‘myset‘,‘world‘);
$redis -> sAdd(‘myset‘,‘hi‘);
$redis -> sAdd(‘myset‘,‘welcome‘);

var_dump($redis -> sMembers(‘myset‘));
//array (size=5)
//  0 => string ‘world‘ (length=5)
//  1 => string ‘hello‘ (length=5)
//  2 => string ‘hi‘ (length=2)
//  3 => string ‘foo‘ (length=3)
//  4 => string ‘welcome‘ (length=7)

4、SCARD

Redis Scard 命令返回集合中元素的數量

語法:

redis 127.0.0.1:6379> SCARD KEY_NAME

返回值:集合的數量。 當集合 key 不存在時,返回 0 。

可用版本:>= 1.0.0

時間復雜度:O(1)

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> sAdd(‘myset‘,‘hello‘);
$redis -> sAdd(‘myset‘,‘foo‘);
$redis -> sAdd(‘myset‘,‘world‘);
$redis -> sAdd(‘myset‘,‘hi‘);
$redis -> sAdd(‘myset‘,‘welcome‘);

var_dump($redis -> sCard(‘myset‘));     // int 5

5、SMOVE

Redis Smove 命令將指定成員 member 元素從 source 集合移動到 destination 集合

(1)SMOVE 是原子性操作。

(2)如果 source 集合不存在或不包含指定的 member 元素,則 SMOVE 命令不執行任何操作,僅返回 0 。否則, member 元素從 source 集合中被移除,並添加到 destination 集合中去。

(3)當 destination 集合已經包含 member 元素時, SMOVE 命令只是簡單地將 source 集合中的 member 元素刪除。

(4)當 source 或 destination 不是集合類型時,返回一個錯誤。

語法:

redis 127.0.0.1:6379> SMOVE SOURCE DESTINATION MEMBER

返回值:如果成員元素被成功移除,返回 1 。 如果成員元素不是 source 集合的成員,並且沒有任何操作對 destination 集合執行,那麽返回 0 。

可用版本:>= 1.0.0

時間復雜度:O(1)

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> sAdd(‘myset‘,‘hello‘);
$redis -> sAdd(‘myset‘,‘foo‘);
$redis -> sAdd(‘myset‘,‘world‘);
$redis -> sAdd(‘myset‘,‘hi‘);
$redis -> sAdd(‘myset‘,‘welcome‘);
$redis -> sAdd(‘destinationSet‘,‘welcome‘);

// The first case : member 包含在 source 中
var_dump($redis -> sMove(‘myset‘,‘destinationSet‘,‘foo‘)); // boolean true var_dump($redis -> sMembers(‘myset‘)); //array (size=4) // 0 => string ‘hello‘ (length=5) // 1 => string ‘hi‘ (length=2) // 2 => string ‘world‘ (length=5) // 3 => string ‘welcome‘ (length=7) // The second case : member 不在 source 中 var_dump($redis -> sMove(‘myset‘,‘destinationSet‘,‘not_exists‘)); // boolean false var_dump($redis -> sMembers(‘myset‘)); //array (size=4) // 0 => string ‘hi‘ (length=2) // 1 => string ‘world‘ (length=5) // 2 => string ‘hello‘ (length=5) // 3 => string ‘welcome‘ (length=7) // The third case : destination 中已經包含 member 元素 var_dump($redis -> sMove(‘myset‘,‘destinationSet‘,‘welcome‘)); // boolean true var_dump($redis -> sMembers(‘myset‘)); // 只是將 welcome 從 myset 中移除 //array (size=3) // 0 => string ‘world‘ (length=5) // 1 => string ‘hello‘ (length=5) // 2 => string ‘hi‘ (length=2)

6、SPOP

Redis Spop 命令用於移除並返回集合中的一個隨機元素。

語法:

redis 127.0.0.1:6379> SPOP KEY

返回值:被移除的隨機元素。 當集合不存在或是空集時,返回 nil 。

可用版本:>= 1.0.0

時間復雜度:O(1)

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> sAdd(‘myset‘,‘hello‘);
$redis -> sAdd(‘myset‘,‘foo‘);
$redis -> sAdd(‘myset‘,‘world‘);
$redis -> sAdd(‘myset‘,‘hi‘);
$redis -> sAdd(‘myset‘,‘welcome‘);

// 其值會從原集合中移除
var_dump($redis -> sPop(‘myset‘));      // string world
var_dump($redis -> sMembers(‘myset‘));
//array (size=4)
//  0 => string ‘hi‘ (length=2)
//  1 => string ‘foo‘ (length=3)
//  2 => string ‘hello‘ (length=5)
//  3 => string ‘welcome‘ (length=7)

7、SRANDMEMBER

Redis Srandmember 命令用於返回集合中的一個隨機元素

從 Redis 2.6 版本開始, Srandmember 命令接受可選的 count 參數:

  • 如果 count 為正數,且小於集合基數,那麽命令返回一個包含 count 個元素的數組,數組中的元素各不相同。如果 count 大於等於集合基數,那麽返回整個集合。
  • 如果 count 為負數,那麽命令返回一個數組,數組中的元素可能會重復出現多次,而數組的長度為 count 的絕對值。

該操作和 SPOP 相似,但 SPOP 將隨機元素從集合中移除並返回,而 Srandmember 則僅僅返回隨機元素,而不對集合進行任何改動

語法:

redis 127.0.0.1:6379> SRANDMEMBER KEY [count]

返回值:(1)只提供集合 key 參數時,返回一個元素

    (2) 如果提供了 count 參數, 若 0<count<集合基數,那麽返回一個包含count個元素的數組, 若 count>集合基數,那麽返回整個集合

    (3)如果提供了 count 參數, 若 count<0,且 count的絕對值<集合基數,那麽返回一個包含count個元素的數組,若 count的絕對值>集合基數,那麽返回包含count個元素的數組,元素可能重復出現多次

可用版本:>= 1.0.0

時間復雜度:O(1)

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> sAdd(‘myset‘,‘hello‘);
$redis -> sAdd(‘myset‘,‘foo‘);
$redis -> sAdd(‘myset‘,‘world‘);
$redis -> sAdd(‘myset‘,‘hi‘);
$redis -> sAdd(‘myset‘,‘welcome‘);

// The first case : 當沒有 count 參數時,返回一個隨機值,其值不會從原集合中移除
var_dump($redis -> sRandMember(‘myset‘));      // string foo
var_dump($redis -> sMembers(‘myset‘));
//array (size=5)
//  0 => string ‘hello‘ (length=5)
//  1 => string ‘hi‘ (length=2)
//  2 => string ‘foo‘ (length=3)
//  3 => string ‘world‘ (length=5)
//  4 => string ‘welcome‘ (length=7)

// The second case : 當 0 < count < 集合基數
var_dump($redis -> sRandMember(‘myset‘,3));   // 返回包含 count 個元素的集合
//array (size=3)
//  0 => string ‘world‘ (length=5)
//  1 => string ‘hello‘ (length=5)
//  2 => string ‘foo‘ (length=3)

// The third case : 當 0 < count 且 集合基數 < count
var_dump($redis -> sRandMember(‘myset‘,10));        // 返回整個集合
//array (size=5)
//  0 => string ‘world‘ (length=5)
//  1 => string ‘hello‘ (length=5)
//  2 => string ‘foo‘ (length=3)
//  3 => string ‘hi‘ (length=2)
//  4 => string ‘welcome‘ (length=7)

// The fourth case : 當 count<0 且  |count| < 集合基數
var_dump($redis -> sRandMember(‘myset‘,-3));       // 返回包含 count 個元素的集合
//array (size=3)
//  0 => string ‘hello‘ (length=5)
//  1 => string ‘welcome‘ (length=7)
//  2 => string ‘world‘ (length=5)

// The fifth case : 當 count<0 且  |count| > 集合基數
var_dump($redis -> sRandMember(‘myset‘,-8));       // 返回包含 count 個元素的集合,有重復
//array (size=8)
//  0 => string ‘world‘ (length=5)
//  1 => string ‘welcome‘ (length=7)
//  2 => string ‘world‘ (length=5)
//  3 => string ‘welcome‘ (length=7)
//  4 => string ‘hello‘ (length=5)
//  5 => string ‘hello‘ (length=5)
//  6 => string ‘world‘ (length=5)
//  7 => string ‘welcome‘ (length=7)

8、SINTER

Redis Sinter 命令返回給定所有給定集合的交集。 不存在的集合 key 被視為空集。 給定集合當中有一個空集時,結果也為空集(根據集合運算定律)。

語法:

redis 127.0.0.1:6379> SINTER KEY KEY1..KEYN

返回值:交集成員的列表。

可用版本:>= 1.0.0

時間復雜度:O(1)

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> sAdd(‘myset‘,‘hello‘);
$redis -> sAdd(‘myset‘,‘foo‘);
$redis -> sAdd(‘myset‘,‘world‘);
$redis -> sAdd(‘myset‘,‘hi‘);
$redis -> sAdd(‘myset‘,‘welcome‘);

$redis -> sAdd(‘otherset‘,‘hello‘);
$redis -> sAdd(‘otherset‘,‘world‘);
$redis -> sAdd(‘otherset‘,‘welcome‘);

// The first case : 集合都不為空 , 原集合不變
var_dump($redis -> sInter(‘myset‘,‘otherset‘));
//array (size=3)
//  0 => string ‘welcome‘ (length=7)
//  1 => string ‘world‘ (length=5)
//  2 => string ‘hello‘ (length=5)

// The second case : 有空集合
var_dump($redis -> sInter(‘myset‘,‘emptyset‘));     // array (size=0) empty

9、SINTERSTORE

Redis Sinterstore 命令將給定集合之間的交集存儲在指定的集合中。如果指定的集合已經存在,則將其覆蓋。

語法:

redis 127.0.0.1:6379> SINTERSTORE DESTINATION_KEY KEY KEY1..KEYN

返回值:交集成員的列表。

可用版本:>= 1.0.0

時間復雜度:O(N * M),N為給定集合當中基數最小的集合,M為給定集合的個數。

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> sAdd(‘myset‘,‘hello‘);
$redis -> sAdd(‘myset‘,‘foo‘);
$redis -> sAdd(‘myset‘,‘world‘);
$redis -> sAdd(‘myset‘,‘hi‘);
$redis -> sAdd(‘myset‘,‘welcome‘);

$redis -> sAdd(‘otherset‘,‘hello‘);
$redis -> sAdd(‘otherset‘,‘world‘);
$redis -> sAdd(‘otherset‘,‘welcome‘);

$redis -> sAdd(‘other_destinationset‘,‘hello‘);

// The first case : 目標集合不存在
var_dump($redis -> sInterStore(‘destinationset‘,‘myset‘,‘otherset‘));   // int 3
var_dump($redis -> sMembers(‘destinationset‘));
//array (size=3)
//  0 => string ‘welcome‘ (length=7)
//  1 => string ‘world‘ (length=5)
//  2 => string ‘hello‘ (length=5)

// The second case : 目標集合已存在
var_dump($redis -> sInterStore(‘other_destinationset‘,‘myset‘,‘otherset‘));   
var_dump($redis -> sMembers(‘other_destinationset‘));       // 覆蓋
//array (size=3)
//  0 => string ‘welcome‘ (length=7)
//  1 => string ‘world‘ (length=5)
//  2 => string ‘hello‘ (length=5)

10、SUNION

Redis Sunion 命令返回給定集合的並集。不存在的集合 key 被視為空集。

語法:

redis 127.0.0.1:6379> SUNION KEY KEY1..KEYN

返回值:並集成員的列表。

可用版本:>= 1.0.0

時間復雜度:O(N),N是所有給定集合的成員數量之和

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> sAdd(‘myset‘,‘hello‘);
$redis -> sAdd(‘myset‘,‘foo‘);
$redis -> sAdd(‘myset‘,‘world‘);
$redis -> sAdd(‘myset‘,‘hi‘);
$redis -> sAdd(‘myset‘,‘welcome‘);

$redis -> sAdd(‘otherset‘,‘hello‘);
$redis -> sAdd(‘otherset‘,‘world‘);
$redis -> sAdd(‘otherset‘,‘good‘);

// The first case : 集合都不為空 , 原集合不變
var_dump($redis -> sUnion(‘myset‘,‘otherset‘));
//array (size=6)
//  0 => string ‘world‘ (length=5)
//  1 => string ‘hello‘ (length=5)
//  2 => string ‘welcome‘ (length=7)
//  3 => string ‘good‘ (length=4)
//  4 => string ‘hi‘ (length=2)
//  5 => string ‘foo‘ (length=3)

// The second case : 有空集合
var_dump($redis -> sUnion(‘myset‘,‘emptyset‘));
//array (size=5)
//  0 => string ‘world‘ (length=5)
//  1 => string ‘hello‘ (length=5)
//  2 => string ‘foo‘ (length=3)
//  3 => string ‘hi‘ (length=2)
//  4 => string ‘welcome‘ (length=7)

11、SUNIONSTORE

Redis Sunionstore 命令將給定集合的並集存儲在指定的集合 destination 中。如果 destination 已經存在,則將其覆蓋。

語法:

redis 127.0.0.1:6379> SUNIONSTORE DESTINATION KEY KEY1..KEYN

返回值:結果集中的元素數量。

可用版本:>= 1.0.0

時間復雜度:O(N),N是所有給定集合的成員數量之和。

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> sAdd(‘myset‘,‘hello‘);
$redis -> sAdd(‘myset‘,‘foo‘);
$redis -> sAdd(‘myset‘,‘world‘);
$redis -> sAdd(‘myset‘,‘hi‘);
$redis -> sAdd(‘myset‘,‘welcome‘);

$redis -> sAdd(‘otherset‘,‘hello‘);
$redis -> sAdd(‘otherset‘,‘world‘);
$redis -> sAdd(‘otherset‘,‘good‘);

$redis -> sAdd(‘other_destinationset‘,‘hello‘);

// The first case : 目標集合不存在
var_dump($redis -> sUnionStore(‘destinationset‘,‘myset‘,‘otherset‘));   // int 6
var_dump($redis -> sMembers(‘destinationset‘));
//array (size=6)
//  0 => string ‘world‘ (length=5)
//  1 => string ‘hello‘ (length=5)
//  2 => string ‘welcome‘ (length=7)
//  3 => string ‘good‘ (length=4)
//  4 => string ‘hi‘ (length=2)
//  5 => string ‘foo‘ (length=3)

// The second case : 目標集合已存在
var_dump($redis -> sUnionStore(‘other_destinationset‘,‘myset‘,‘otherset‘));   // int 6
var_dump($redis -> sMembers(‘other_destinationset‘));       // 覆蓋
//array (size=6)
//  0 => string ‘world‘ (length=5)
//  1 => string ‘hello‘ (length=5)
//  2 => string ‘welcome‘ (length=7)
//  3 => string ‘good‘ (length=4)
//  4 => string ‘hi‘ (length=2)
//  5 => string ‘foo‘ (length=3)

12、SDIFF

Redis Sdiff 命令返回給定集合之間的差集。不存在的集合 key 將視為空集。

語法:

redis 127.0.0.1:6379> SDIFF FIRST_KEY OTHER_KEY1..OTHER_KEYN

返回值:包含差集成員的列表。

可用版本:>= 1.0.0

時間復雜度:O(N),N是所有給定集合的成員數量之和。

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> sAdd(‘myset‘,‘hello‘);
$redis -> sAdd(‘myset‘,‘foo‘);
$redis -> sAdd(‘myset‘,‘world‘);
$redis -> sAdd(‘myset‘,‘hi‘);
$redis -> sAdd(‘myset‘,‘welcome‘);

$redis -> sAdd(‘otherset‘,‘hello‘);
$redis -> sAdd(‘otherset‘,‘world‘);
$redis -> sAdd(‘otherset‘,‘good‘);

var_dump($redis -> sDiff(‘myset‘,‘otherset‘));  // 此處的差集指的是第一個集合的元素,不包含後面集合的元素
//array (size=3)
//  0 => string ‘welcome‘ (length=7)
//  1 => string ‘foo‘ (length=3)
//  2 => string ‘hi‘ (length=2)

13、SDIFFSTORE

Redis Sdiffstore 命令將給定集合之間的差集存儲在指定的集合中。如果指定的集合 key 已存在,則會被覆蓋。

語法:

redis 127.0.0.1:6379> SDIFFSTORE DESTINATION_KEY KEY1..KEYN

返回值:結果集中的元素數量。

可用版本:>= 1.0.0

時間復雜度:O(N),N是所有給定集合的成員數量之和。

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> sAdd(‘myset‘,‘hello‘);
$redis -> sAdd(‘myset‘,‘foo‘);
$redis -> sAdd(‘myset‘,‘world‘);
$redis -> sAdd(‘myset‘,‘hi‘);
$redis -> sAdd(‘myset‘,‘welcome‘);

$redis -> sAdd(‘otherset‘,‘hello‘);
$redis -> sAdd(‘otherset‘,‘world‘);
$redis -> sAdd(‘otherset‘,‘good‘);

$redis -> sAdd(‘other_destinationset‘,‘hello‘);

// The first case : 目標集合不存在
var_dump($redis -> sDiffStore(‘destinationset‘,‘myset‘,‘otherset‘));   // int 3, 此處指的是第一個集合中的元素
var_dump($redis -> sMembers(‘destinationset‘));
//array (size=3)
//  0 => string ‘welcome‘ (length=7)
//  1 => string ‘foo‘ (length=3)
//  2 => string ‘hi‘ (length=2)

// The second case : 目標集合已存在
var_dump($redis -> sDiffStore(‘other_destinationset‘,‘myset‘,‘otherset‘));   // int 3
var_dump($redis -> sMembers(‘other_destinationset‘));       // 覆蓋
//array (size=3)
//  0 => string ‘welcome‘ (length=7)
//  1 => string ‘foo‘ (length=3)
//  2 => string ‘hi‘ (length=2)

14、SISMEMBER

Redis Sismember 命令判斷成員元素是否是集合的成員。

語法:

redis 127.0.0.1:6379> SISMEMBER KEY VALUE

返回值:如果成員元素是集合的成員,返回 1 。 如果成員元素不是集合的成員,或 key 不存在,返回 0 。

可用版本:>= 1.0.0

時間復雜度:O(1)

具體實例:

<?php
$redis = new redis();
$redis -> connect(‘127.0.0.1‘,6379);
$redis -> flushAll();

$redis -> sAdd(‘myset‘,‘hello‘);
$redis -> sAdd(‘myset‘,‘foo‘);
$redis -> sAdd(‘myset‘,‘world‘);
$redis -> sAdd(‘myset‘,‘hi‘);
$redis -> sAdd(‘myset‘,‘welcome‘);

var_dump($redis -> sIsMember(‘myset‘,‘hello‘));     // true
var_dump($redis -> sIsMember(‘myset‘,‘good‘));      // false

如有轉載,請註明出處:http://www.cnblogs.com/chrdai/p/6846352.html

redis 在 php 中的應用(Set篇)