# string是redis最基本的类型,而且string类型是二进制安全的.
# 意思是redis的string可以包含任何数据,比如jpg图片或者序列化的对象.
$redis
->set('key','LY');
$redis
->set('number','1');
$redis
->setex('key',5,'LY');
$redis
->psetex('key',5000,'LY');
$redis
->setnx('key','LW');
$redis
->
delete
('key');
$redis
->getSet('key','LW');
$ret
=
$redis
->multi()
->set('key1', 'val1')
->get('key1')
->setnx('key', 'val2')
->get('key2')->
exec
();
$redis
->watch('key');
function
f(
$redis
,
$chan
,
$msg
) {
switch
(
$chan
) {
case
'chan-1':
echo
$msg
;
break
;
case
'chan-2':
echo
$msg
;
break
;
case
'chan-2':
echo
$msg
;
break
;
}
}
$redis
->subscribe(
array
('chan-1', 'chan-2', 'chan-3'), 'f');
$redis
->publish('chan-1', 'hello, world!');
$redis
->exists('key');
$redis
->incr('number');
$redis
->incrby('number',-10);
$redis
->incrByFloat('number', +/- 1.5);
$redis
->decr('number');
$redis
->decrBy('number',10);
$mget
=
$redis
->mget(
array
('number','key'));
$redis
->mset(
array
('key0' => 'value0', 'key1' => 'value1'));
$redis
->msetnx(
array
('key0' => 'value0', 'key1' => 'value1'));
$redis
->append('key', '-Smudge');
$redis
->getRange('key', 0, 5);
$redis
->getRange('key', -6, -1);
$redis
->setRange('key', 0, 'Smudge');
$redis
->
strlen
('key');
$redis
->getBit('key');
$redis
->setBit('key');
# redis hash是一个string类型的field和value的映射表.
# 它的添加,删除操作都是O(1)(平均).hash特别适合用于存储对象。
$redis
->hSet('h', 'name', 'LY');
$redis
->hSetNx('h', 'name', 'LY');
$redis
->hGet('h', 'name');
$redis
->hLen('h');
$redis
->hDel('h','email');
$redis
->hKeys('h');
$redis
->hVals('h');
$redis
->hGetAll('h');
$redis
->hExists('h', 'email');
$redis
->hSet('h', 'age', 28);
$redis
->hIncrBy('h', 'age', -2);
$redis
->hIncrByFloat('h', 'age', -0.33);
$redis
->hMset('h',
array
('score' => '80', 'salary' => 2000));
$redis
->hMGet('h',
array
('score','salary'));
$redis
->
delete
('list-key');
$redis
->lPush('list-key', 'A');
$redis
->rPush('list-key', 'B');
$redis
->lPushx('list-key', 'C');
$redis
->rPushx('list-key', 'C');
$redis
->lPop('list-key');
$redis
->rPop('list-key');
$redis
->blPop();
$redis
->brPop();
$redis
->lSize('list-key');
$redis
->lGet('list-key',-1);
$redis
->lSet('list-key', 0, 'X');
$redis
->lRange('list-key', 0, 3);
$redis
->lTrim('list-key', 0, 1);
$redis
->lRem('list-key', 'C', 2);
$redis
->lInsert('list-key', Redis::BEFORE, 'C', 'X');
$redis
->rpoplpush('list-key', 'list-key2');
$redis
->brpoplpush();
# set无序集合 不允许出现重复的元素 服务端可以实现多个 集合操作
$redis
->sMembers('key');
$redis
->sAdd('key' , 'LY');
$redis
->sRem('key' , 'LY');
$redis
->sMove('key','key1','LY');
$redis
->sIsMember('key','LY');
$redis
->sCard('key');
$redis
->sPop('key');
$redis
->sRandMember('key');
$redis
->sInter('key','key1');
$redis
->sInterStore('store','key','key1');
$redis
->sUnion('key','key1');
$redis
->sUnionStore('store','key','key1');
$redis
->sDiff('key','key1','key2');
# (stored set) 和 set 一样是字符串的集合,不同的是每个元素都会关联一个 double 类型的 score
# redis的list类型其实就是一个每个子元素都是string类型的双向链表。
$redis
->zAdd('tkey', 1, 'A');
$redis
->zRange('tkey',0,-1);
$redis
->zRange('tkey',0,-1, true);
array
([A] => 0.01,[B] => 0.02,[D] => 0.03);
$redis
->zDelete('tkey', 'B');
$redis
->zRevRange('tkey', 0, -1);
$redis
->zRevRange('tkey', 0, -1,true);
$redis
->zRangeByScore('tkey', 0, 0.2,
array
('withscores' => true));
$redis
->zRangeByScore('tkey', 0.1, 0.36,
array
('withscores' => TRUE, 'limit' =>
array
(0, 1)));
$redis
->zCount('tkey', 2, 10);
$redis
->zRemRangeByScore('tkey', 1, 3);
$redis
->zRemRangeByRank('tkey', 0, 1);
$redis
->zSize('tkey');
$redis
->zScore('tkey', 'A');
$redis
->zRank('tkey', 'A');
$redis
->zIncrBy('tkey', 2.5, 'A');
$redis
->zUnion('union',
array
('tkey', 'tkey1'));
$redis
->zUnion('ko2',
array
('k1', 'k2'),
array
(5, 2));
$redis
->zUnion('ko2',
array
('k1', 'k2'),
array
(10, 2),'MAX');
$redis
->zInter('ko1',
array
('k1', 'k2'));
$redis
->zInter('ko1',
array
('k1', 'k2'),
array
(5, 1));
$redis
->zInter('ko1',
array
('k1', 'k2'),
array
(5, 1),'MAX');