转载请备注来源: 《Redis常用命令及python、php函数调用整理》 | shuwoom.com

1、Redis连接

(1)python redis连接

# coding:utf-8

import redis

# 连接数据库
conn = redis.Redis(host='127.0.0.1', port=6379, password='password')

# 使用连接池连接数据库,可以实现多个Redis实例共享一个连接池
pool = redis.ConnectionPool(host='127.0.0.1', port=6379)
conn = redis.Redis(connection_pool=pool)

(2)php redis连接

<?php

try {
    $redis = new Redis();
    $redis->connect('127.0.0.1', 6379, 1); # 短连接,超过1秒放弃连接
//    $redis->open('127.0.0.1', 6379, 1); # 同上
//    $redis->pconnect('127.0.0.1', 6379, 1); # 长连接,超过1秒放弃连接
    $redis->popen('127.0.0.1', 6379, 1); # 同上

    # 登录验证密码,返回:true或false
    if ($redis->auth('password')) {
        echo 'auth success!';
    } else {
        echo 'auth failed!';
        throw new Exception('Redis auth failed!');
    }

    $redis->select(0); # 选择redis库,0~15,共16个库
    $redis->close(); # 释放资源
//    $redis->ping(); # 检查是否还在连接
} catch (Exception $e) {
    var_dump($e->getMessage());
}

2、Redis中的字符串

(1) python redis字符串函数调用

# coding:utf-8

import redis
import time

# redis连接: redis-cli -h host -p port -a password
conn = redis.Redis(host='127.0.0.1', port=6379, password='password')
##########################################
# 字符串
##########################################

# (1)设置key值
# 命令:SET [key] [value]
conn.set('name', 'shuwoom')

# (2)获取key值
# 命令:GET [key]
print conn.get('name')

# (3)删除key值
# 命令:DEL [KEY]
conn.delete('name')
print conn.get('name')

# (4)将key对应的value加1
# INCR [key]
conn.incr('age')
print conn.get('age')

# (5)将key对应的value减1
# DECR [key]
conn.decr('age')
print conn.get('age')

# (6)将key对应的value加上整数amount
# INCRBY [key] amount
conn.incr('age', 20)
print conn.get('age')

# (7)将key对应的value减去整数amount
# DECRBY [key] amount
conn.decr('age', 10)
print conn.get('age')

# (8)将key对应的value加上浮点数amount
# INCRBYFLOAT [key] [float]
conn.incrbyfloat('score', 88.88)

# (9)设置key的失效时间
conn.set('name', 'shuwoom')
conn.expire('name', 3)
print conn.get('name')
sleep(4)
print conn.get('name')

(2) php redis字符串函数调用

<?php
try {
    $redis = new Redis();
    $redis->connect('127.0.0.1', 6379, 1);

    if ($redis->auth('password')) {
        echo 'auth success!' . PHP_EOL;
    } else {
        echo 'auth failed!' . PHP_EOL;
        throw new Exception('Redis auth failed!');
    }

    # (1)设置key值
    # 命令:SET [key] [value]
    $redis->set('name', 'shuwoom');

    # (2)获取key值
    # 命令:GET [key]
    print $redis->get('name') . PHP_EOL;

    # (3)删除key值
    # 命令:DEL [KEY]
    $redis->delete('name');
    print $redis->get('name') . PHP_EOL;

    # (4)将key对应的value加1
    # INCR [key]
    $redis->incr('age');
    print $redis->get('age') . PHP_EOL;

    # (5)将key对应的value减1
    # DECR [key]
    $redis->decr('age');
    print $redis->get('age') . PHP_EOL;

    # (6)将key对应的value加上整数amount
    # INCRBY [key] amount
    $redis->incrBy('age', 20);
    print $redis->get('age') . PHP_EOL;

    # (7)将key对应的value减去整数amount
    # DECRBY [key] amount
    $redis->decrBy('age', 10);
    print $redis->get('age') . PHP_EOL;

    # (8)将key对应的value加上浮点数amount
    # INCRBYFLOAT [key] [float]
    $redis->incrByFloat('score', 88.88);
    print $redis->get('score') . PHP_EOL;

    # (9)设置key的失效时间
    $redis->set('name', 'shuwoom');
    $redis->expire('name', 3);
    print $redis->get('name') . PHP_EOL;
    sleep(4);
    print $redis->get('name') . PHP_EOL;

    $redis->close();; # 释放资源
} catch (Exception $e) {
    var_dump($e->getMessage());
}

3、Redis中的列表

(1) python redis列表函数调用

# coding:utf-8

import redis

# redis连接: redis-cli -h host -p port -a password
conn = redis.Redis(host='127.0.0.1', port=6379, password='password')
##########################################
# 列表
##########################################
conn.delete('user')
# (1)将一个或多个值推入列表的右端
# 命令:RPUSH key value1 value2 value3 ...
print "[rpush][before]:", conn.lrange('user', 0, -1)
conn.rpush('user', 'user1', 'user2')
print"[rpush][after]:",  conn.lrange('user', 0, -1)
print '\n'

# (2)将一个或多个值推入列表的左端
# 命令:LPUSH key value1 value2 value3 ...
conn.lpush('user', 'user3', 'user4')
print conn.lrange('user', 0, -1)
print '\n'

# (3)移除并返回列表最右端的元素
# 命令:RPOP key
print "[rpop][before]:", conn.lrange('user', 0, -1)
print "[rpop]:", conn.rpop('user')
print "[rpop][after]:", conn.lrange('user', 0, -1)
print '\n'

# (4)移除并返回列表最左端的元素
# 命令:LPOP key
print "[lpop][before]:", conn.lrange('user', 0, -1)
print "[lpop]:", conn.lpop('user')
print "[lpop][after]:", conn.lrange('user', 0, -1)
print '\n'

# (5)返回列表中偏移量为offset的元素(偏移量从0开始)
# LINDEX key
print "[lindex]:", conn.lindex('user', 0)
print '\n'

# (6)返回列表从start偏移量到end偏移量范围内的所有元素(包括偏移量为start和end的元素)
# 命令:LRANGE key start end
print "[lrange][0,-1]:", conn.lrange('user', 0, -1) # 返回全部
print "[lrange][0,2]:", conn.lrange('user', 0, 2)
print '\n'

# (7)对列表进行修剪,值保留从start偏移量到end偏移量范围内的元素(包括偏移量为start和end的元素)
# 命令:LTRIM key start end
print "[ltrim][before]:", conn.lrange('user', 0, -1)
conn.ltrim('user', 1, -1)
print "[ltrim][after]:", conn.lrange('user', 0, -1)
print '\n'

# (8)获取列表长度,key不存在则返回0
print "[llen]", conn.llen('user')
print '\n'

# (9)从非空列表中移除并返回列表最右端的元素或在timeout秒之内阻塞并等待可弹出的元素出现
# 命令:BRPOP key1 key2 ...
conn.delete('list1')
conn.delete('list2')

conn.lpush('list1', 'a')
print "list1:", conn.lrange('list1', 0, -1)
conn.lpush('list2', '1', '2', '3', '4')
print "list2:", conn.lrange('list2', 0, -1)

print "[brpop]:", conn.brpop(['list1', 'list2'], 1)
print "[brpop]:", conn.brpop(['list1', 'list2'], 1)
print "[brpop]:", conn.brpop(['list1', 'list2'], 1)

# (10)从非空列表中移除并返回列表最左端的元素或在timeout秒之内阻塞并等待可弹出的元素出现
# 命令:BLPOP key1 key2 ...
conn.delete('list1')
conn.delete('list2')

conn.lpush('list1', 'a')
print "list1:", conn.lrange('list1', 0, -1)
conn.lpush('list2', '1', '2', '3', '4')
print "list2:", conn.lrange('list2', 0, -1)
print '\n'

print "[blpop]:", conn.blpop(['list1', 'list2'], 1)
print "[blpop]:", conn.blpop(['list1', 'list2'], 1)
print "[blpop]:", conn.blpop(['list1', 'list2'], 1)
print '\n'

# (11)从source-key列表中弹出位于最右端的元素,然后将这个元素推入dest-key列表的最左端,并返回这个元素。如果source-key为空,则在timeout秒之内阻塞并等待可弹出的元素出现
# 命令:BRPOPLPUSH source-key dest-key timeout
conn.delete('list1')
conn.delete('list2')

conn.lpush('list1', 'a', 'b', 'c', 'd')
print "list1:", conn.lrange('list1', 0, -1)
conn.lpush('list2', '1', '2', '3', '4')
print "list2:", conn.lrange('list2', 0, -1)

print '[brpoplpush]:', conn.rpoplpush('list1', 'list2')
print "list1:", conn.lrange('list1', 0, -1)
print "list2:", conn.lrange('list2', 0, -1)
print '\n'

# (12)从source-key列表中弹出位于最右端的元素,然后将这个元素推入dest-key列表的最左端,并返回这个元素
# 命令:RPOPLPUSH source-key dest-key
conn.delete('list1')
conn.delete('list2')

conn.lpush('list1', 'a', 'b', 'c', 'd')
print "list1:", conn.lrange('list1', 0, -1)
conn.lpush('list2', '1', '2', '3', '4')
print "list2:", conn.lrange('list2', 0, -1)

print '[brpoplpush]:', conn.brpoplpush('list1', 'list2', 1)
print "list1:", conn.lrange('list1', 0, -1)
print "list2:", conn.lrange('list2', 0, -1)
print '\n'

(2) php redis列表函数调用

<?php
try {
    $redis = new Redis();
    $redis->connect('127.0.0.1', 6379, 1);

    if ($redis->auth('password')) {
        echo 'auth success!' . PHP_EOL;
    } else {
        echo 'auth failed!' . PHP_EOL;
        throw new Exception('Redis auth failed!');
    }

    $redis->delete('user');
    # (1)将一个或多个值推入列表的右端
    # 命令:RPUSH key value1 value2 value3 ...
    print "[rpush][before]:" . json_encode($redis->lrange('user', 0, -1)) . PHP_EOL;
    $redis->rpush('user', 'user1', 'user2');
    print"[rpush][after]:" .  json_encode($redis->lrange('user', 0, -1)) . PHP_EOL;
    
    # (2)将一个或多个值推入列表的左端
    # 命令:LPUSH key value1 value2 value3 ...
    $redis->lpush('user', 'user3', 'user4');
    print json_encode($redis->lrange('user', 0, -1)) . PHP_EOL;
    
    # (3)移除并返回列表最右端的元素
    # 命令:RPOP key
    print "[rpop][before]:" . json_encode($redis->lrange('user', 0, -1)) . PHP_EOL;
    $redis->rpop('user');
    print "[rpop][after]:" . json_encode($redis->lrange('user', 0, -1)) . PHP_EOL;
    
    # (4)移除并返回列表最左端的元素
    # 命令:LPOP key
    print "[lpop][before]:" . json_encode($redis->lrange('user', 0, -1)) . PHP_EOL;
    $redis->lpop('user');
    print "[lpop][after]:" . json_encode($redis->lrange('user', 0, -1)) . PHP_EOL;
    
    # (5)返回列表中偏移量为offset的元素(偏移量从0开始)
    # LINDEX key
    print "[lindex]:" . $redis->lindex('user', 0);
    
    # (6)返回列表从start偏移量到end偏移量范围内的所有元素(包括偏移量为start和end的元素)
    # 命令:LRANGE key start end
    print "[lrange][0,-1]:" . json_encode($redis->lrange('user', 0, -1)) . PHP_EOL; # 返回全部
    print "[lrange][0,2]:" . json_encode($redis->lrange('user', 0, 2)) . PHP_EOL;
    
    # (7)对列表进行修剪,值保留从start偏移量到end偏移量范围内的元素(包括偏移量为start和end的元素)
    # 命令:LTRIM key start end
    print "[ltrim][before]:" . json_encode($redis->lrange('user', 0, -1)) . PHP_EOL;
    $redis->ltrim('user', 1, -1);
    print "[ltrim][after]:" . json_encode($redis->lrange('user', 0, -1)) . PHP_EOL;

    # (8)获取列表长度,key不存在则返回0
    print "[llen]" . $redis->lLen('user') . PHP_EOL;

    # (9)从非空列表中移除并返回列表最右端的元素或在timeout秒之内阻塞并等待可弹出的元素出现
    # 命令:BRPOP key1 key2 ...
    $redis->delete('list1');
    $redis->delete('list2');
    
    $redis->lpush('list1', 'a');
    print "list1:" . json_encode($redis->lrange('list1', 0, -1)) . PHP_EOL;
    $redis->lpush('list2', '1', '2', '3', '4');
    print "list2:" . json_encode($redis->lrange('list2', 0, -1)). PHP_EOL;
    
    print "[brpop]:" . json_encode($redis->brpop(['list1', 'list2'], 1)). PHP_EOL;
    print "[brpop]:" . json_encode($redis->brpop(['list1', 'list2'], 1)). PHP_EOL;
    print "[brpop]:" . json_encode($redis->brpop(['list1', 'list2'], 1)). PHP_EOL;
    
    # (10)从非空列表中移除并返回列表最左端的元素或在timeout秒之内阻塞并等待可弹出的元素出现
    # 命令:BLPOP key1 key2 ...
    $redis->delete('list1');
    $redis->delete('list2');
    
    $redis->lpush('list1', 'a');
    print "list1:" . json_encode($redis->lrange('list1', 0, -1)). PHP_EOL;
    $redis->lpush('list2', '1', '2', '3', '4');
    print "list2:" . json_encode($redis->lrange('list2', 0, -1)). PHP_EOL;
    print PHP_EOL;
    
    print "[blpop]:" . json_encode($redis->blpop(['list1', 'list2'], 1)). PHP_EOL;
    print "[blpop]:" . json_encode($redis->blpop(['list1', 'list2'], 1)). PHP_EOL;
    print "[blpop]:" . json_encode($redis->blpop(['list1', 'list2'], 1)). PHP_EOL;
    print PHP_EOL;
    
    # (11)从source-key列表中弹出位于最右端的元素,然后将这个元素推入dest-key列表的最左端,并返回这个元素。如果source-key为空,则在timeout秒之内阻塞并等待可弹出的元素出现
    # 命令:BRPOPLPUSH source-key dest-key timeout
    $redis->delete('list1');
    $redis->delete('list2');
    
    $redis->lpush('list1', 'a', 'b', 'c', 'd');
    print "list1:" . json_encode($redis->lrange('list1', 0, -1)). PHP_EOL;
    $redis->lpush('list2', '1', '2', '3', '4');
    print "list2:" . json_encode($redis->lrange('list2', 0, -1)). PHP_EOL;
    
    print '[brpoplpush]:' . $redis->rpoplpush('list1', 'list2'). PHP_EOL;
    print "list1:" . json_encode($redis->lrange('list1', 0, -1)). PHP_EOL;
    print "list2:" . json_encode($redis->lrange('list2', 0, -1)). PHP_EOL;
    print PHP_EOL;
    
    # (12)从source-key列表中弹出位于最右端的元素,然后将这个元素推入dest-key列表的最左端,并返回这个元素
    # 命令:RPOPLPUSH source-key dest-key
    $redis->delete('list1');
    $redis->delete('list2');
    
    $redis->lpush('list1', 'a', 'b', 'c', 'd');
    print "list1:" . json_encode($redis->lrange('list1', 0, -1)). PHP_EOL;
    $redis->lpush('list2', '1', '2', '3', '4');
    print "list2:" . json_encode($redis->lrange('list2', 0, -1)). PHP_EOL;
    
    print '[brpoplpush]:' . $redis->brpoplpush('list1', 'list2', 1). PHP_EOL;
    print "list1:" . json_encode($redis->lrange('list1', 0, -1)). PHP_EOL;
    print "list2:" . json_encode($redis->lrange('list2', 0, -1)). PHP_EOL;
    print PHP_EOL;


    $redis->close();; # 释放资源
} catch (Exception $e) {
    var_dump($e->getMessage());
}

4、Redis中的集合

(1) python redis集合函数调用

# coding:utf-8

import redis

# redis连接: redis-cli -h host -p port -a password
conn = redis.Redis(host='127.0.0.1', port=6379, password='password')
##########################################
# 集合
##########################################
conn.delete('user')
# (1)将一个或多个元素添加到即合理,并返回被添加元素中不存在集合里面的元素数量
# 命令:SADD key value1 value2 value3 ...
print "[sadd][before]:", conn.smembers('user')
conn.sadd('user', 'user1', 'user2', 'user3')
print "[sadd][after]:", conn.smembers('user')
print '\n'

# (2)从集合中移除一个或多个元素,并返回被移除元素的数量
# 命令:SREM key value1 value2 value3 ...
print "[srem][before]:", conn.smembers('user')
conn.srem('user', 'user1')
print "[srem][after]:",  conn.smembers('user')
print '\n'

# (3)检查value是否存在于key中
# 命令:SISMEMBER key value
print "[sismember][before]:", conn.smembers('user')
conn.sismember('user', 'user1')
print "[sismember][after]:",  conn.smembers('user')
print '\n'

# (4)返回集合包含的元素数量
# 命令:SCARD key
print '[scard]:', conn.scard('user')
print '\n'

# (5)返回集合包含的所有元素
# 命令:SMEMBERS key
print '[smembers]:', conn.smembers('user')
print '\n'

# (6)从集合中随机返回一个或多个元素,当count为正时,返回的随机元素不会重复;当count为负时,返回的随机元素会重复
# 命令:SRANDMEMBER key [count]...
print '[srandmember]:', conn.srandmember('user', 1)
print '\n'

# (7)随机地移除集合中的一个元素,并返回被移除的元素
# 命令:SPOP key
print '[spop];', conn.spop('user')
print '\n'

# (8)如果集合source-key包含元素item,那么从集合source-key里面移除item,并将item添加到集合dest-key中,如果item被成功移除,则返回1,否则返回0
# 命令:SMOVE source-key dest-key item
conn.delete('list1')
conn.delete('list2')
conn.sadd('list1', 'a', 'b')
conn.sadd('list2', '1', '2')

print 'list1:', conn.smembers('list1')
print 'list2:', conn.smembers('list2')
print '[smove];', conn.smove('list1', 'list2', 'b')
print 'list1:', conn.smembers('list1')
print 'list2:', conn.smembers('list2')
print '\n'

# (9)返回那些存在于第一集合、但不存在于其它集合中的元素(差集运算)
# 命令:SDIFF key1 key2 ...
conn.delete('list1')
conn.delete('list2')
conn.sadd('list1', 'a', 'b')
conn.sadd('list2', '1', '2', 'a')

print 'list1:', conn.smembers('list1')
print 'list2:', conn.smembers('list2')
print '[sdiff];', conn.sdiff('list1', 'list2')
print '\n'

# (10)将那些存在于第一集合、但不存在于其它集合中的元素存储到dest-key里面
# 命令:SDIFFSTORE dest-key key1 key2 ...
conn.delete('list1')
conn.delete('list2')
conn.delete('dest-key')
conn.sadd('list1', 'a', 'b')
conn.sadd('list2', '1', '2', 'a')

print 'list1:', conn.smembers('list1')
print 'list2:', conn.smembers('list2')
print '[sdiffstore];', conn.sdiffstore('dest-key', 'list1', 'list2')
print 'dest-key:', conn.smembers('dest-key')
print '\n'

# (11)返回那些存在于所有集合的元素(交集运算)
# 命令:SINTER key1 key2 ...
conn.delete('list1')
conn.delete('list2')
conn.sadd('list1', 'a', 'b')
conn.sadd('list2', '1', '2', 'a')

print 'list1:', conn.smembers('list1')
print 'list2:', conn.smembers('list2')
print '[sinter];', conn.sinter('list1', 'list2')
print '\n'

# (12)将那些存在于所有集合的元素存储到dest-key里面
# 命令:SINTERSTORE dest-key key1 key2 ...
conn.delete('list1')
conn.delete('list2')
conn.delete('dest-key')
conn.sadd('list1', 'a', 'b')
conn.sadd('list2', '1', '2', 'a')

print 'list1:', conn.smembers('list1')
print 'list2:', conn.smembers('list2')
print '[sinterstore];', conn.sinterstore('dest-key', 'list1', 'list2')
print 'dest-key:', conn.smembers('dest-key')
print '\n'

# (13)返回那些至少存在于一个集合中的元素(并集运算)
# 命令:SUNION key1 key2 ...
conn.delete('list1')
conn.delete('list2')
conn.sadd('list1', 'a', 'b')
conn.sadd('list2', '1', '2', 'a')

print 'list1:', conn.smembers('list1')
print 'list2:', conn.smembers('list2')
print '[sunion];', conn.sunion('list1', 'list2')
print '\n'

# (14)将那些至少存在于一个集合中的元素存储到dest-key里面
# 命令:SUNIONSTORE dest-key key1 key2 ...
conn.delete('list1')
conn.delete('list2')
conn.delete('dest-key')
conn.sadd('list1', 'a', 'b')
conn.sadd('list2', '1', '2', 'a')

print 'list1:', conn.smembers('list1')
print 'list2:', conn.smembers('list2')
print '[sunionstore];', conn.sunionstore('dest-key', 'list1', 'list2')
print 'dest-key:', conn.smembers('dest-key')
print '\n'

(2) php redis集合函数调用

<?php
try {
    $redis = new Redis();
    $redis->connect('127.0.0.1', 6379, 1);

    if ($redis->auth('password')) {
        echo 'auth success!' . PHP_EOL;
    } else {
        echo 'auth failed!' . PHP_EOL;
        throw new Exception('Redis auth failed!');
    }

    $redis->delete('user');
    # (1)将一个或多个元素添加到即合理,并返回被添加元素中不存在集合里面的元素数量
    # 命令:SADD key value1 value2 value3 ...
    print "[sadd][before]:" . json_encode($redis->smembers('user')). PHP_EOL;
    $redis->sadd('user', 'user1', 'user2', 'user3');
    print "[sadd][after]:" . json_encode($redis->smembers('user')). PHP_EOL;
    print PHP_EOL;
    
    # (2)从集合中移除一个或多个元素,并返回被移除元素的数量
    # 命令:SREM key value1 value2 value3 ...
    print "[srem][before]:" . json_encode($redis->smembers('user'));
    $redis->srem('user', 'user1');
    print "[srem][after]:" . json_encode($redis->smembers('user'));
    print PHP_EOL;
    
    # (3)检查value是否存在于key中
    # 命令:SISMEMBER key value
    print "[sismember][before]:" . json_encode($redis->smembers('user')). PHP_EOL;
    $redis->sismember('user', 'user1');
    print "[sismember][after]:" . json_encode($redis->smembers('user')). PHP_EOL;
    print PHP_EOL;
    
    # (4)返回集合包含的元素数量
    # 命令:SCARD key
    print '[scard]:' . $redis->scard('user'). PHP_EOL;
    print PHP_EOL;
    
    # (5)返回集合包含的所有元素
    # 命令:SMEMBERS key
    print '[smembers]:' . json_encode($redis->smembers('user')). PHP_EOL;
    print PHP_EOL;
    
    # (6)从集合中随机返回一个或多个元素,当count为正时,返回的随机元素不会重复;当count为负时,返回的随机元素会重复
    # 命令:SRANDMEMBER key [count]...
    print '[srandmember]:' . json_encode($redis->srandmember('user', 1)). PHP_EOL;
    print PHP_EOL;
    
    # (7)随机地移除集合中的一个元素,并返回被移除的元素
    # 命令:SPOP key
    print '[spop];' . $redis->spop('user'). PHP_EOL;
    print PHP_EOL;
    
    # (8)如果集合source-key包含元素item,那么从集合source-key里面移除item,并将item添加到集合dest-key中,如果item被成功移除,则返回1,否则返回0
    # 命令:SMOVE source-key dest-key item
    $redis->delete('list1');
    $redis->delete('list2');
    $redis->sadd('list1', 'a', 'b');
    $redis->sadd('list2', '1', '2');
    
    print 'list1:' . json_encode(json_encode($redis->smembers('list1'))). PHP_EOL;
    print 'list2:' . json_encode(json_encode($redis->smembers('list2'))). PHP_EOL;
    print '[smove];' . $redis->smove('list1', 'list2', 'b'). PHP_EOL;
    print 'list1:' . json_encode(json_encode($redis->smembers('list1'))). PHP_EOL;
    print 'list2:' . json_encode(json_encode($redis->smembers('list2'))) . PHP_EOL;
    print PHP_EOL;

    # (9)返回那些存在于第一集合、但不存在于其它集合中的元素(差集运算)
    # 命令:SDIFF key1 key2 ...
    $redis->delete('list1') ;
    $redis->delete('list2');
    $redis->sadd('list1', 'a', 'b');
    $redis->sadd('list2', '1', '2', 'a');
    
    print 'list1:' . json_encode($redis->smembers('list1')) . PHP_EOL;
    print 'list2:' . json_encode($redis->smembers('list2')) . PHP_EOL;
    print '[sdiff];' . json_encode($redis->sdiff('list1', 'list2')). PHP_EOL;
    print PHP_EOL;
    
    # (10)将那些存在于第一集合、但不存在于其它集合中的元素存储到dest-key里面
    # 命令:SDIFFSTORE dest-key key1 key2 ...
    $redis->delete('list1');
    $redis->delete('list2') ;
    $redis->delete('dest-key');
    $redis->sadd('list1', 'a', 'b');
    $redis->sadd('list2', '1', '2', 'a');
    
    print 'list1:' . json_encode($redis->smembers('list1')) . PHP_EOL;
    print 'list2:' . json_encode($redis->smembers('list2')) . PHP_EOL;
    print '[sdiffstore];' . $redis->sdiffstore('dest-key', 'list1', 'list2'). PHP_EOL;
    print 'dest-key:' . json_encode($redis->smembers('dest-key')). PHP_EOL;
    print PHP_EOL;
    
    # (11)返回那些存在于所有集合的元素(交集运算)
    # 命令:SINTER key1 key2 ...
    $redis->delete('list1') ;
    $redis->delete('list2') ;
    $redis->sadd('list1', 'a', 'b');
    $redis->sadd('list2', '1', '2', 'a');
    
    print 'list1:' . json_encode($redis->smembers('list1')) . PHP_EOL;
    print 'list2:' . json_encode($redis->smembers('list2')) . PHP_EOL; 
    print '[sinter];' . json_encode($redis->sinter('list1', 'list2')). PHP_EOL;
    print PHP_EOL;
    
    # (12)将那些存在于所有集合的元素存储到dest-key里面
    # 命令:SINTERSTORE dest-key key1 key2 ...
    $redis->delete('list1') ;
    $redis->delete('list2') ;
    $redis->delete('dest-key');
    $redis->sadd('list1', 'a', 'b');
    $redis->sadd('list2', '1', '2', 'a');
    
    print 'list1:' . json_encode($redis->smembers('list1')) . PHP_EOL;
    print 'list2:' . json_encode($redis->smembers('list2')) . PHP_EOL;
    print '[sinterstore];' . json_encode($redis->sinterstore('dest-key', 'list1', 'list2')). PHP_EOL;
    print 'dest-key:' . json_encode($redis->smembers('dest-key')). PHP_EOL;
    print PHP_EOL;
    
    # (13)返回那些至少存在于一个集合中的元素(并集运算)
    # 命令:SUNION key1 key2 ...
    $redis->delete('list1');
    $redis->delete('list2');
    $redis->sadd('list1', 'a', 'b');
    $redis->sadd('list2', '1', '2', 'a');
    
    print 'list1:' . json_encode($redis->smembers('list1')) . PHP_EOL;
    print 'list2:' . json_encode($redis->smembers('list2')) . PHP_EOL;
    print '[sunion];' . json_encode($redis->sunion('list1', 'list2')). PHP_EOL;
    print PHP_EOL;
    
    # (14)将那些至少存在于一个集合中的元素存储到dest-key里面
    # 命令:SUNIONSTORE dest-key key1 key2 ...
    $redis->delete('list1');
    $redis->delete('list2');
    $redis->delete('dest-key');
    $redis->sadd('list1', 'a', 'b');
    $redis->sadd('list2', '1', '2', 'a');
    
    print 'list1:' . json_encode($redis->smembers('list1')) . PHP_EOL;
    print 'list2:' . json_encode($redis->smembers('list2')) . PHP_EOL;
    print '[sunionstore];' . json_encode($redis->sunionstore('dest-key', 'list1', 'list2')). PHP_EOL;
    print 'dest-key:' . json_encode($redis->smembers('dest-key')). PHP_EOL;
    print PHP_EOL;
    



    $redis->close();; # 释放资源
} catch (Exception $e) {
    var_dump($e->getMessage());
}

5、Redis中的散列

(1) python redis散列函数调用

# coding:utf-8

import redis

# redis连接: redis-cli -h host -p port -a password
conn = redis.Redis(host='127.0.0.1', port=6379, password='password')
##########################################
# 散列
##########################################
conn.delete('user')

# (1)从散列里面返回一个或多个key的value值
# 命令:HMSET key-name k1 v1 k2 v2 ...
print '[hmset]:', conn.hmset('user', {'name':'shuwoom', 'age': 28})
print '\n'

# (2)从散列里面返回一个或多个key的value值
# 命令:HMGET key-name key1 key2 ...
print '[hmget]:', conn.hmget('user', ['name', 'age'])
print '\n'

# (3)返回散列包含的键值对数量
# 命令:HLEN key-name
print '[hlen]:', conn.hlen('user')

# (4)删除散列里面的一个或多个键值对,并返回成功找到并删除的键值对数量
# 命令:HDEL key-name key1 key2 ...
print '[hdel]', conn.hdel('user', 'name', 'age')
print '\n'

# (5)检查给定键是否存在于散列中
# 命令:HEXISTS key-name key
conn.hmset('user', {'name':'shuwoom', 'age': 28})
print '[hexists]:', conn.hexists('user', 'age')
print '\n'

# (6)获取散列包含的所有key值
# 命令:HKEYS key-name
print '[hkeys]:', conn.hkeys('user')
print '\n'

# (7)获取散列包含的所有value值
# 命令:HVALS key-name
print '[hvals]:', conn.hvals('user')
print '\n'

# (8)获取散列包含的所有键值对
# 命令:HGETALL key-name
print '[hgetall]:', conn.hgetall('user')
print '\n'

# (9)将键key存储的值value加上整数amount
# 命令:HINCRBY key-name key amount
print '[hincrby]:', conn.hincrby('user', 'age', 10)
print '\n'

# (10)将键key存储的值value加上浮点数amount
# 命令:HINCRBYFLOAT key-name key amount
print '[hincrbyfloat]:', conn.hincrbyfloat('user', 'age', 10.5)
print '\n'

(2) php redis散列函数调用

<?php
try {
    $redis = new Redis();
    $redis->connect('127.0.0.1', 6379, 1);

    if ($redis->auth('password')) {
        echo 'auth success!' . PHP_EOL;
    } else {
        echo 'auth failed!' . PHP_EOL;
        throw new Exception('Redis auth failed!');
    }

    $redis->delete('user');
    # (1)从散列里面返回一个或多个key的value值
    # 命令:HMSET key-name k1 v1 k2 v2 ...
    print '[hmset]:' . $redis->hmset('user', ['name' => 'shuwoom', 'age' => 28]) . PHP_EOL;
    print PHP_EOL;
    
    # (2)从散列里面返回一个或多个key的value值
    # 命令:HMGET key-name key1 key2 ...
    print '[hmget]:' . json_encode($redis->hmget('user', ['name', 'age'])). PHP_EOL;
    print PHP_EOL;
    
    # (3)返回散列包含的键值对数量
    # 命令:HLEN key-name
    print '[hlen]:' . $redis->hlen('user'). PHP_EOL;
    
    # (4)删除散列里面的一个或多个键值对,并返回成功找到并删除的键值对数量
    # 命令:HDEL key-name key1 key2 ...
    print '[hdel]' . $redis->hdel('user', 'name', 'age'). PHP_EOL;
    print PHP_EOL;
    
    # (5)检查给定键是否存在于散列中
    # 命令:HEXISTS key-name key
    $redis->hmset('user', ['name' => 'shuwoom', 'age' => 28]);
    print '[hexists]:' . $redis->hexists('user', 'age'). PHP_EOL;
    print PHP_EOL;
    
    # (6)获取散列包含的所有key值
    # 命令:HKEYS key-name
    print '[hkeys]:' . json_encode($redis->hkeys('user')) . PHP_EOL;
    print PHP_EOL;
    
    # (7)获取散列包含的所有value值
    # 命令:HVALS key-name
    print '[hvals]:' . json_encode($redis->hvals('user')) . PHP_EOL;
    print PHP_EOL;
    
    # (8)获取散列包含的所有键值对
    # 命令:HGETALL key-name
    print '[hgetall]:' . json_encode($redis->hgetall('user')) . PHP_EOL;
    print PHP_EOL;
    
    # (9)将键key存储的值value加上整数amount
    # 命令:HINCRBY key-name key amount
    print '[hincrby]:' . $redis->hincrby('user', 'age', 10) . PHP_EOL;
    print PHP_EOL;
    
    # (10)将键key存储的值value加上浮点数amount
    # 命令:HINCRBYFLOAT key-name key amount
    print '[hincrbyfloat]:' . $redis->hincrbyfloat('user', 'age', 10.5) . PHP_EOL;
    print PHP_EOL;

    $redis->close();; # 释放资源
} catch (Exception $e) {
    var_dump($e->getMessage());
}

6、Redis中的有序集合

(1) python redis有序集合函数调用

# coding:utf-8

import redis

# redis连接: redis-cli -h host -p port -a password
conn = redis.Redis(host='127.0.0.1', port=6379, password='password')
##########################################
# 有序集合
##########################################
conn.delete('zset-key')
# (1)将带有给定分值的成员添加到有序集合里面
# 命令:ZADD key-name score member [score member] ...
print "[zadd][before]:", conn.zrange('zset-key', 0, -1, withscores=True)
conn.zadd('zset-key', 'a', 3, 'b', 2, 'c', 1)
print "[zadd][after]:", conn.zrange('zset-key', 0, -1, withscores=True)
print '\n'

# (2)从有序集合中移除给定的成员,并返回被移除元素的数量
# 命令:ZREM key-name member [member ...]
print "[zrem][before]:", conn.zrange('zset-key', 0, -1, withscores=True)
conn.zrem('zset-key', 'b')
print "[zrem][before]:", conn.zrange('zset-key', 0, -1, withscores=True)
print '\n'

# (3)返回有序集合包含的元素数量
# 命令:ZCARD key-name
print '[zcard]:', conn.zcard('zset-key')
print '\n'

# (4)将member成员的分值加上amount
# 命令:ZINCRBY key-name amount member
print '[zincrby]:', conn.zincrby('zset-key', 'c', 3)
print '\n'

# (5)返回分值介于min和max之间的成员数量(包含min和max的分值)
# 命令:ZCOUNT key-name min max
conn.delete('zset-key')
conn.zadd('zset-key', 'a', 3, 'b', 2, 'c', 1)
print "zset-key:", conn.zrange('zset-key', 0, -1, withscores=True)
print '[zcount]:', conn.zcount('zset-key', 1, 3)
print '\n'

# (6)返回成员member在有序集合中的排名
# 命令:ZRANK key-name member
print "zset-key:", conn.zrange('zset-key', 0, -1, withscores=True)
print '[zrank]:', conn.zrank('zset-key', 'a')
print '\n'

# (7)返回成员member的分值
# 命令:ZSCORE key-name member
print "zset-key:", conn.zrange('zset-key', 0, -1, withscores=True)
print '[zscore]:', conn.zscore('zset-key', 'a')
print '\n'

# (8)返回有序集合中排名介于start和stop之间的成员,如果给定了可选的WITHSCORES选项,则返回结果会带上分值
# 命令:ZRANGE key-name start stop [WITHSCORES]
print '[zrange]:', conn.zrange('zset-key', 0, -1, withscores=True)
print '\n'

# (9)返回成员member在有序集合中的排名,成员按照分值从大到小排列
# 命令:ZREVRANK key-name member
print "zset-key:", conn.zrange('zset-key', 0, -1, withscores=True)
print '[zrevrank]:', conn.zrevrank('zset-key', 'a')
print '\n'

# (10)返回有序集合中排名介于start和stop之间的成员(成员分值按照从大到小排列),如果给定了可选的WITHSCORES选项,则返回结果会带上分值
# 命令:ZREVRANGE key-name start stop [WITHSCORES]
print '[zrevrange]:', conn.zrevrange('zset-key', 0, -1, withscores=True)
print '\n'

# (11)返回有序集合中,分值介于min和max之间的所有成员
# 命令:ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
print "zset-key:", conn.zrange('zset-key', 0, -1, withscores=True)
print '[zrangebyscore]:', conn.zrangebyscore('zset-key', 1, 3, withscores=True)
print '\n'

# (12)返回有序集合中,分值介于min和max之间的所有成员(成员分值按照从大到小排列)
# 命令:ZREVRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
print "zset-key:", conn.zrange('zset-key', 0, -1, withscores=True)
print '[zrevrangebyscore]:', conn.zrevrangebyscore('zset-key', 1, 3, withscores=True)
print '\n'

# (13)移除有序集合中排名介于start和stop之间的所有成员
# 命令:ZREMRANGEBYRANK key-name start stop
conn.delete('zset-key')
conn.zadd('zset-key', 'a', 3, 'b', 2, 'c', 1)
print "zset-key:", conn.zrange('zset-key', 0, -1, withscores=True)
print '[zremrangebyrank]:', conn.zremrangebyrank('zset-key', 1, 3)
print '\n'

# (14)移除有序集合中分值介于min和max之间的所有成员
# 命令:ZREMRANGEBYSCORE key-name min max
conn.delete('zset-key')
conn.zadd('zset-key', 'a', 3, 'b', 2, 'c', 1)
print "zset-key:", conn.zrange('zset-key', 0, -1, withscores=True)
print '[zremrangebyscore]:', conn.zremrangebyscore('zset-key', 2, 3)
print '\n'

# (14)对给定的有序集合执行类似于集合的交集运算
# 命令:ZINTERSTORE dest-key key-count key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
conn.delete('zset-1')
conn.delete('zset-2')
conn.zadd('zset-1', 'a', 1, 'b', 2, 'c', 3)
conn.zadd('zset-2', 'b', 4, 'c', 2, 'd', 1)
print "zset-1:", conn.zrange('zset-1', 0, -1, withscores=True)
print "zset-2:", conn.zrange('zset-2', 0, -1, withscores=True)
print '[zinterstore]:', conn.zinterstore('zset-i', ['zset-1', 'zset-2'], aggregate='sum')
print "zset-i:", conn.zrange('zset-i', 0, -1, withscores=True)
print '\n'

# (15)对给定的有序集合执行类似于集合的并集运算
# 命令:ZUNIONSTORE dest-key key-count key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
conn.delete('zset-1')
conn.delete('zset-2')
conn.zadd('zset-1', 'a', 1, 'b', 2, 'c', 3)
conn.zadd('zset-2', 'b', 4, 'c', 2, 'd', 1)
print "zset-1:", conn.zrange('zset-1', 0, -1, withscores=True)
print "zset-2:", conn.zrange('zset-2', 0, -1, withscores=True)
print '[zunionstore]:', conn.zunionstore('zset-u', ['zset-1', 'zset-2'], aggregate='min')
print "zset-u:", conn.zrange('zset-u', 0, -1, withscores=True)
print '\n'

(2) php redis有序集合函数调用

<?php
try {
    $redis = new Redis();
    $redis->connect('127.0.0.1', 6379, 1);

    if ($redis->auth('password')) {
        echo 'auth success!' . PHP_EOL;
    } else {
        echo 'auth failed!' . PHP_EOL;
        throw new Exception('Redis auth failed!');
    }

    $redis->delete('zset-key');
    # (1)将带有给定分值的成员添加到有序集合里面
    # 命令:ZADD key-name score member [score member] ...
    print "[zadd][before]:" . json_encode($redis->zrange('zset-key', 0, -1, $withscores=TRUE)). PHP_EOL;
    $redis->zadd('zset-key', 3,'a', 2, 'b', 1, 'c');
    print "[zadd][after]:" . json_encode($redis->zrange('zset-key', 0, -1, $withscores=TRUE)). PHP_EOL;
    print PHP_EOL;

    # (2)从有序集合中移除给定的成员,并返回被移除元素的数量
    # 命令:ZREM key-name member [member ...]
    print "[zrem][before]:" . json_encode($redis->zrange('zset-key', 0, -1, $withscores=TRUE)). PHP_EOL;
    $redis->zrem('zset-key', 'b');
    print "[zrem][before]:" . json_encode($redis->zrange('zset-key', 0, -1, $withscores=TRUE)). PHP_EOL;
    print PHP_EOL;

    # (3)返回有序集合包含的元素数量
    # 命令:ZCARD key-name
    print '[zcard]:' . $redis->zcard('zset-key'). PHP_EOL;
    print PHP_EOL;

    # (4)将member成员的分值加上amount
    # 命令:ZINCRBY key-name amount member
    print '[zincrby]:' . $redis->zincrby('zset-key', 3, 'c'). PHP_EOL;
    print PHP_EOL;

    # (5)返回分值介于min和max之间的成员数量(包含min和max的分值)
    # 命令:ZCOUNT key-name min max
    $redis->delete('zset-key');
    $redis->zadd('zset-key', 3,'a', 2, 'b', 1, 'c');
    print "zset-key:" . json_encode($redis->zrange('zset-key', 0, -1, $withscores=TRUE)). PHP_EOL;
    print '[zcount]:' . $redis->zcount('zset-key', 1, 3). PHP_EOL;
    print PHP_EOL;

    # (6)返回成员member在有序集合中的排名
    # 命令:ZRANK key-name member
    print "zset-key:" . json_encode($redis->zrange('zset-key', 0, -1, $withscores=TRUE)). PHP_EOL;
    print '[zrank]:' . $redis->zrank('zset-key', 'a'). PHP_EOL;
    print PHP_EOL;

    # (7)返回成员member的分值
    # 命令:ZSCORE key-name member
    print "zset-key:" . json_encode($redis->zrange('zset-key', 0, -1, $withscores=TRUE)). PHP_EOL;
    print '[zscore]:' . $redis->zscore('zset-key', 'a'). PHP_EOL;
    print PHP_EOL;

    # (8)返回有序集合中排名介于start和stop之间的成员,如果给定了可选的WITHSCORES选项,则返回结果会带上分值
    # 命令:ZRANGE key-name start stop [WITHSCORES]
    print '[zrange]:' . json_encode($redis->zrange('zset-key', 0, -1, $withscores=TRUE)). PHP_EOL;
    print PHP_EOL;

    # (9)返回成员member在有序集合中的排名,成员按照分值从大到小排列
    # 命令:ZREVRANK key-name member
    print "zset-key:" . json_encode($redis->zrange('zset-key', 0, -1, $withscores=TRUE)). PHP_EOL;
    print '[zrevrank]:' . $redis->zrevrank('zset-key', 'a'). PHP_EOL;
    print PHP_EOL;

    # (10)返回有序集合中排名介于start和stop之间的成员(成员分值按照从大到小排列),如果给定了可选的WITHSCORES选项,则返回结果会带上分值
    # 命令:ZREVRANGE key-name start stop [WITHSCORES]
    print '[zrevrange]:' .json_encode( $redis->zrevrange('zset-key', 0, -1, $withscores=TRUE)). PHP_EOL;
    print PHP_EOL;

    # (11)返回有序集合中,分值介于min和max之间的所有成员
    # 命令:ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
    print "zset-key:" . json_encode($redis->zrange('zset-key', 0, -1, $withscores=TRUE)). PHP_EOL;
    print '[zrangebyscore]:' . json_encode( $redis->zrangebyscore('zset-key', 1, 3, [$withscores => TRUE])). PHP_EOL;
    print PHP_EOL;

    # (12)返回有序集合中,分值介于min和max之间的所有成员(成员分值按照从大到小排列)
    # 命令:ZREVRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
    print "zset-key:" . json_encode($redis->zrange('zset-key', 0, -1, $withscores=TRUE)). PHP_EOL;
    print '[zrevrangebyscore]:' . json_encode($redis->zrevrangebyscore('zset-key', 1, 3, [$withscores => TRUE])). PHP_EOL;
    print PHP_EOL;

    # (13)移除有序集合中排名介于start和stop之间的所有成员
    # 命令:ZREMRANGEBYRANK key-name start stop
    $redis->delete('zset-key');
    $redis->zadd('zset-key', 3,'a', 2, 'b', 1, 'c');
    print "zset-key:" . json_encode($redis->zrange('zset-key', 0, -1, $withscores=TRUE)). PHP_EOL;
    print '[zremrangebyrank]:' . json_encode($redis->zremrangebyrank('zset-key', 1, 3)). PHP_EOL;
    print PHP_EOL;

    # (14)移除有序集合中分值介于min和max之间的所有成员
    # 命令:ZREMRANGEBYSCORE key-name min max
    $redis->delete('zset-key');
    $redis->zadd('zset-key', 3,'a', 2, 'b', 1, 'c');
    print "zset-key:" . json_encode($redis->zrange('zset-key', 0, -1, $withscores=TRUE)). PHP_EOL;
    print '[zremrangebyscore]:' . json_encode($redis->zremrangebyscore('zset-key', 2, 3)). PHP_EOL;
    print PHP_EOL;

    # (14)对给定的有序集合执行类似于集合的交集运算
    # 命令:ZINTERSTORE dest-key key-count key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
    $redis->delete('zset-1');
    $redis->delete('zset-2');
    $redis->zadd('zset-1', 1, 'a', 2,'b', 3, 'c');
    $redis->zadd('zset-2', 4, 'b', 2, 'c', 1, 'd');
    print "zset-1:" . json_encode($redis->zrange('zset-1', 0, -1, $withscores=TRUE)). PHP_EOL;
    print "zset-2:" . json_encode($redis->zrange('zset-2', 0, -1, $withscores=TRUE)). PHP_EOL;
    print '[zinterstore]:' . json_encode($redis->zInter('zset-i', ['zset-1', 'zset-2'], [1,1], $aggregateFunction='SUM')). PHP_EOL;
    print "zset-i:" . json_encode($redis->zrange('zset-i', 0, -1, $withscores=TRUE)). PHP_EOL;
    print PHP_EOL;

    # (15)对给定的有序集合执行类似于集合的并集运算
    # 命令:ZUNIONSTORE dest-key key-count key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
    $redis->delete('zset-1');
    $redis->delete('zset-2');
    $redis->zadd('zset-1', 1, 'a', 2,'b', 3, 'c');
    $redis->zadd('zset-2', 4, 'b', 2, 'c', 1, 'd');
    print "zset-1:" . json_encode($redis->zrange('zset-1', 0, -1, $withscores=TRUE)). PHP_EOL;
    print "zset-2:" . json_encode($redis->zrange('zset-2', 0, -1, $withscores=TRUE)). PHP_EOL;
    print '[zunionstore]:' . json_encode($redis->zUnion('zset-u', ['zset-1', 'zset-2'], [1,1], $aggregateFunction='MIN')). PHP_EOL;
    print "zset-u:" . json_encode($redis->zrange('zset-u', 0, -1, $withscores=TRUE)). PHP_EOL;
    print PHP_EOL;

    $redis->close();; # 释放资源
} catch (Exception $e) {
    var_dump($e->getMessage());
}

7、Redis中的事务属性

特点:要么全部成功,要么全部失败

(1) python redis事务应用

# coding:utf-8

import redis

# 连接数据库
conn = redis.Redis(host='172.16.16.11', port=6379, password='CloudScan_2019')

conn.delete('user:xiaohong')
conn.delete('user:xiaoming')

# 小红转账给小明10元
val = 10

conn.hmset('user:xiaohong',  {'id': 111, 'account':100})
conn.hmset('user:xiaoming',  {'id': 222, 'account':100})

pipe = conn.pipeline()
try :
    # 监控小红的信息是否发生变化
    pipe.watch('user:xiaohong')
    # conn.hmset('user:xiaohong', {'account': 5})  # 模拟小红交易前账户余额发生变化
    account = pipe.hmget('user:xiaohong', 'account')

    if account < val:
        pipe.unwatch()

    pipe.multi()

    pipe.hincrby('user:xiaohong', 'account', val)
    pipe.hincrby('user:xiaoming', 'account', -val)

    pipe.execute()
except redis.exceptions.WatchError:
    print 'Watch error'

print conn.hmget('user:xiaohong', 'account')
print conn.hmget('user:xiaoming', 'account')

(2) php redis事务应用

<?php

try {
    $redis = new Redis();
    $redis->connect('172.16.16.11', 6379, 1);

    if ($redis->auth('CloudScan_2019')) {
        echo 'auth success!' . PHP_EOL;
    } else {
        echo 'auth failed!' . PHP_EOL;
        throw new Exception('Redis auth failed!');
    }

    $redis->delete('user:xiaohong');
    $redis->delete('user:xiaoming');
    
    # 小红转账给小明10元
    $val = 10;

    $redis->hmset('user:xiaohong', ['id' => 111, 'account' => 100]);
    $redis->hmset('user:xiaoming', ['id' => 111, 'account' => 100]);

    # 监控小红的信息是否发生变化
    $redis->watch('user:xiaohong');
    $redis->hMset('user:xiaohong', ['account' => 5]); # 模拟小红交易前账户余额发生变化
    $values = $redis->hMGet('user:xiaohong', ['account']);
    $account = $values['account'];
    if ($account < $val) {
        $redis->unwatch();
        throw new Exception('Not enough account!');
    }
    
    $redis->multi();

    $redis->hincrby('user:xiaohong', 'account', $val);
    $redis->hincrby('user:xiaoming', 'account', -$val);
    
    $redis->exec();
    
    print json_encode($redis->hmget('user:xiaohong', ['account'])) . PHP_EOL;
    print json_encode($redis->hmget('user:xiaoming', ['account'])) . PHP_EOL;

    
    $redis->close();; # 释放资源
} catch (Exception $e) {
    var_dump($e->getMessage());
}

转载请备注来源: 《Redis常用命令及python、php函数调用整理》 | shuwoom.com 

打赏

发表评论

电子邮件地址不会被公开。