��ҳ > ��ѡ���� > �������� > ����

redis��PHP�еĻ���ʹ�ð���

����http://www.oschina.net/p/redis

��ѹ�������У�lib Դ�ļ� ��examples ���ӡ�test����

��libĿ¼������������Ŀ�У��Ϳ��Կ�ʼ����predis�����ˡ�

//ʹ��autoload�������ؿ�,�����ص�����Ϊ��require $file;
spl_autoload_register(function($class) {
$file = __DIR__.’/lib/Predis/’.$class.’.php’;
if (file_exists($file)) {
require $file;
return true;
}
});

//�������ӵ�IP���˿ڡ��Լ���Ӧ�����ݿ�
$server = array(
‘host’     => ’127.0.0.1′,
‘port’     => 6379,
‘database’ => 15
);
$redis = new Client($server);

//��ͨset/get����
$redis->set(‘library’, ‘predis’);
$retval = $redis->get(‘library’);
echo $retval; //��ʾ ‘predis’

//setex setһ���洢ʱЧ
$redis->setex(‘str’, 10, ‘bar’); //��ʾ�洢��Ч��Ϊ10��

//setnx/msetnx�൱��add����,���Ḳ������ֵ
$redis->setnx(‘foo’,12); //true
$redis->setnx(‘foo’,34); //false

//getset����,set�ı���,���������滻ǰ��ֵ
$redis->getset(‘foo’,56);//����34

// incrby/incr/decrby/decr ��ֵ�ĵ����͵ݼ�
$redis->incr(‘foo’); //fooΪ57
$redis->incrby(‘foo’,2); //fooΪ59

//exists�����Ƿ�����ijֵ
$redis->exists(‘foo’);//true

//del ɾ��
$redis->del(‘foo’);//true

//type ���ͼ���,�ַ�������string,�б����� list,set������set/zset,hash������hash
$redis->type(‘foo’);//������,����none
$redis->set(‘str’,'test’);
$redis->type(‘str’); //�ַ���������string

//append ���ӵ��Ѵ����ַ���
$redis->append(‘str’,'_123′); //�����ۼӺ����ַ�������8,�˽�strΪ ‘test_123′

//setrange �����滻����
$redis->setrange(‘str’,0,’abc’); //����3,����2Ϊ0ʱ��ͬ��set����
$redis->setrange(‘str’,2,’cd’);//����4,��ʾ�ӵ�2���ַ����滻,��ʱ’str’Ϊ’abcd’

//substr ���ֻ�ȡ����
$redis->substr(‘str’,0,2);//��ʾ�ӵ�0������ȡ����2���ַ�����3��������’abc’

//strlen ��ȡ�ַ�������
$redis->strlen(‘str’); //����4

//setbit/getbit λ�洢�ͻ�ȡ
$redis->setbit(‘binary’,31,1);  //��ʾ�ڵ�31λ����1,���߿��ܻ��д�С������?����û��ϵ,getbit Ӧ�ò���������
$redis->getbit(‘binary’,31);    //����1

//keys ģ�����ҹ���,֧��*���Լ�?��(ƥ��һ���ַ�)
$redis->set(‘foo1′,123);
$redis->set(‘foo2′,456);
$redis->keys(‘foo*’); //����foo1��foo2��array
$redis->keys(‘f?o?’);  //ͬ��

//randomkey ��������һ��key
$redis->randomkey(); //�����Ƿ��� ‘foo1′������’foo2′�������κ�һ����redis��key

//rename/renamenx ��key���и���,����ͬ����renamenx�������ij��Ѵ��ڵ�key
$redis->rename(‘str’,'str2′); //��ԭ������Ϊ’str’��key�ij���’str2′

//expire ����key-value��ʱЧ��,ttl ��ȡʣ����Ч��,persist ��������Ϊ���ô洢
$redis->expire(‘foo’, 1); //������Ч��Ϊ1��
$redis->ttl(‘foo’); //������Ч��ֵ1s
$redis->expire(‘foo’); //ȡ��expire��Ϊ

//dbsize ����redis��ǰ���ݿ��ļ�¼����
$redis->dbsize();

/*
�����
*/

//rpush/rpushx �����б�����,�Ӷ��к�����Ԫ��
//lpush/lpushx ��rpush/rpushx�������Dz��뵽���е�ͷ��,ͬ��,’x'������ֻ���Ѵ��ڵ�key���в���
$redis->rpush(‘fooList’, ‘bar1′); //����һ���б��ij���1
$redis->lpush(‘fooList’, ‘bar0′); //����һ���б��ij���2
$redis->rpushx(‘fooList’, ‘bar2′); //����3,rpushxֻ���Ѵ��ڵĶ���������,���򷵻�0
//llen���ص�ǰ�б�����
$redis->llen(‘fooList’);//3

//lrange ���ض�����һ��������Ԫ��
$redis->lrange(‘fooList’,0,1); //��������������0������1����2��Ԫ��
$redis->lrange(‘fooList’,0,-1);//���ص�0����������һ��,�൱�ڷ�������Ԫ��,ע��redis�кܶ�ʱ�����õ�����,��ͬ

//lindex ����ָ��˳��λ�õ�listԪ��
$redis->lindex(‘fooList’,1); //����’bar1′

//lset �޸Ķ�����ָ��λ�õ�value
$redis->lset(‘fooList’,1,’123′);//�޸�λ��1��Ԫ��,����true

//lrem ɾ������������ָ���������ַ�
$redis->lrem(‘fooList’,1,’_'); //ɾ������������(����ʹ��-1)1���ַ�’_'(����)

//lpop/rpop ����ջ�ṹ�ص���(��ɾ��)���������ҵ�һ��Ԫ��
$redis->lpop(‘fooList’); //’bar0′
$redis->rpop(‘fooList’); //’bar2′

//ltrim �����޸ģ���������������Ԫ�أ�����ɾ��
$redis->ltrim(‘fooList’, 0,1); //������������0������1��Ԫ��

//rpoplpush ��һ��������pop��Ԫ�ز�push����һ������
$redis->rpush(‘list1′,’ab0′);
$redis->rpush(‘list1′,’ab1′);
$redis->rpush(‘list2′,’ab2′);
$redis->rpush(‘list2′,’ab3′);
$redis->rpoplpush(‘list1′,’list2′);//����list1 =>array(‘ab0′),list2 =>array(‘ab1′,’ab2′,’ab3′)
$redis->rpoplpush(‘list2′,’list2′);//Ҳ������ͬһ������,������һ��Ԫ���Ƶ�ͷ��list2 =>array(‘ab3′,’ab1′,’ab2′)

//linsert �ڶ��е��м�ָ��Ԫ��ǰ��������Ԫ��
$redis->linsert(‘list2′, ‘before’,'ab1′,’123′); //��ʾ��Ԫ��’ab1′֮ǰ����’123′
$redis->linsert(‘list2′, ‘after’,'ab1′,’456′);   //��ʾ��Ԫ��’ab1′֮������’456′

//blpop/brpop �������ȴ�һ���жӲ�Ϊ��ʱ����pop�����������ҵ�һ��Ԫ�أ�����������php��������˵�dz����ã�
//brpoplpush ͬ�����������ȴ�����������ͬrpoplpushһ��
$redis->blpop(‘list3′,10); //����list3Ϊ����һֱ�ȴ�,ֱ����Ϊ��ʱ����һԪ�ص���,10������ʱ

/**
set������
*/

//sadd ����Ԫ��,����true,�ظ�����false
$redis->sadd(‘set1′,’ab’);
$redis->sadd(‘set1′,’cd’);
$redis->sadd(‘set1′,’ef’);

//srem �Ƴ�ָ��Ԫ��
$redis->srem(‘set1′,’cd’); //ɾ��’cd’Ԫ��

//spop ������Ԫ��
$redis->spop(‘set1′);

//smove �ƶ���ǰset����ָ��Ԫ�ص���һ��set��
$redis->sadd(‘set2′,’123′);
$redis->smove(‘set1′,’set2′,’ab’);//�ƶ�’set1′�е�’ab’��’set2′,����true or false

//scard ���ص�ǰset��Ԫ�ظ���
$redis->scard(‘set2′);//2

//sismember �ж�Ԫ���Ƿ����ڵ�ǰ��
$redis->sismember(‘set2′,’123′); //true or false

//smembers ���ص�ǰ��������Ԫ��
$redis->smembers(‘set2′); //array(’123′,’ab’);

//sinter/sunion/sdiff  ������������Ԫ�صĽ���/����/����
$redis->sadd(‘set1′,’ab’);
$redis->sinter(‘set2′,’set1′); //����array(‘ab’)

//sinterstore/sunionstore/sdiffstore ������������/����/����Ԫ��copy������������
$redis->set(‘foo’,0);
$redis->sinterstore(‘foo’,'set1′); //���ߵ�ͬ�ڽ�’set1′������copy��’foo’�У�����’foo’תΪset��
$redis->sinterstore(‘foo’,array(‘set1′,’set2′)); //��’set1′��’set2′����ͬ��Ԫ��copy��’foo’����,����’foo’ԭ������

//srandmember ���ر���һ������Ԫ��
$redis->srandmember(‘set1′);

/**
����set������
*/

//sadd ����Ԫ��,����������,����true,�ظ�����false
$redis->zadd(‘zset1′,1,’ab’);
$redis->zadd(‘zset1′,2,’cd’);
$redis->zadd(‘zset1′,3,’ef’);

//zincrby ��ָ��Ԫ������ֵ������,�ı�Ԫ�����д���
$redis->zincrby(‘zset1′,10,’ab’);//����11

//zrem �Ƴ�ָ��Ԫ��
$redis->zrem(‘zset1′,’ef’); //true or false

//zrange ��λ�ô��򷵻ر���ָ��������Ԫ��
$redis->zrange(‘zset1′,0,1); //����λ��0��1֮��(����)��Ԫ��
$redis->zrange(‘zset1′,0,-1);//����λ��0�͵�����һ��Ԫ��֮����Ԫ��(�൱������Ԫ��)

//zrevrange ͬ��,���ر���ָ��������Ԫ��,����������
$redis->zrevrange(‘zset1′,0,-1); //Ԫ��˳����zrange�෴

//zrangebyscore/zrevrangebyscore ��˳��/���򷵻ر���ָ������������Ԫ��
$redis->zadd(‘zset1′,3,’ef’);
$redis->zadd(‘zset1′,5,’gh’);
$redis->zrangebyscore(‘zset1′,2,9); //��������ֵ2-9֮����Ԫ�� array(‘ef’,'gh’)
//������ʽ
$redis->zrangebyscore(‘zset1′,2,9,’withscores’); //��������ֵ2-9֮����Ԫ�ز���������ֵ array(array(‘ef’,3),array(‘gh’,5))
$redis->zrangebyscore(‘zset1′,2,9,array(‘withscores’ =>true,’limit’=>array(1, 2))); //��������ֵ2-9֮����Ԫ��,’withscores’ =>true��ʾ��������ֵ; ‘limit’=>array(1, 2),��ʾ���෵��2��,����Ϊarray(array(‘ef’,3),array(‘gh’,5))

//zunionstore/zinterstore ���������IJ���/����������һ������
$redis->zunionstore(‘zset3′,array(‘zset1′,’zset2′,’zset0′)); //��’zset1′,’zset2′,’zset0′�IJ�������’zset3′
//��������
$redis->zunionstore(‘zset3′,array(‘zset1′,’zset2′),array(‘weights’ => array(5,0)));//weights������ʾȨ�أ����б�ʾ������ֵ����5��Ԫ������ǰ������0�����ں�
$redis->zunionstore(‘zset3′,array(‘zset1′,’zset2′),array(‘aggregate’ => ‘max’));//’aggregate’ => ‘max’��’min’��ʾ��������ͬ��Ԫ����ȡ��ֵ����ȡСֵ

//zcount ͳ��һ������������Ԫ�ظ���
$redis->zcount(‘zset1′,3,5);//2
$redis->zcount(‘zset1′,’(3′,5)); //’(3′��ʾ����ֵ��3-5֮�䵫����3,ͬ��Ҳ����ʹ��’(5′��ʾ����Ϊ5������5

//zcard ͳ��Ԫ�ظ���
$redis->zcard(‘zset1′);//4

//zscore ��ѯԪ�ص�����
$redis->zscore(‘zset1′,’ef’);//3

//zremrangebyscore ɾ��һ������������Ԫ��
$redis->zremrangebyscore(‘zset1′,0,2); //ɾ��������0-2֮����Ԫ��(‘ab’,'cd’),����ɾ��Ԫ�ظ���2

//zrank/zrevrank ����Ԫ�����ڱ�˳��/������λ��(��������)
$redis->zrank(‘zset1′,’ef’);//����0,��Ϊ���ǵ�һ��Ԫ��;zrevrank�򷵻�1(����һ��)

//zremrangebyrank ɾ������ָ��λ��������Ԫ��
$redis->zremrangebyrank(‘zset1′,0,10); //ɾ��λ��Ϊ0-10��Ԫ��,����ɾ����Ԫ�ظ���2

/**
hash������
*/

//hset/hget ��ȡhash��������
$redis->hset(‘hash1′,’key1′,’v1′); //��keyΪ’key1′ valueΪ’v1′��Ԫ�ش���hash1��
$redis->hset(‘hash1′,’key2′,’v2′);
$redis->hget(‘hash1′,’key1′);  //ȡ����’hash1′�е�key ‘key1′��ֵ,����’v1′

//hexists ����hash���е�ָ��key�Ƿ�����
$redis->hexists (‘hash1′,’key1′); //true or false

//hdel ɾ��hash����ָ��key��Ԫ��
$redis->hdel(‘hash1′,’key2′); //true or false

//hlen ����hash��Ԫ�ظ���
$redis->hlen(‘hash1′); //1

//hsetnx ����һ��Ԫ��,�������ظ�
$redis->hsetnx(‘hash1′,’key1′,’v2′); //false
$redis->hsetnx(‘hash1′,’key2′,’v2′); //true

//hmset/hmget ��ȡ����Ԫ�ص�hash��
$redis->hmset(‘hash1′,array(‘key3′=>’v3′,’key4′=>’v4′));
$redis->hmget(‘hash1′,array(‘key3′,’key4′)); //������Ӧ��ֵ array(‘v3′,’v4′)

//hincrby ��ָ��key�����ۼ�
$redis->hincrby(‘hash1′,’key5′,3); //����3
$redis->hincrby(‘hash1′,’key5′,10); //����13

//hkeys ����hash�������key
$redis->hkeys(‘hash1′); //����array(‘key1′,’key2′,’key3′,’key4′,’key5′)

//hvals ����hash�������value
$redis->hvals(‘hash1′); //����array(‘v1′,’v2′,’v3′,’v4′,13)

//hgetall ��������hash��Ԫ��
$redis->hgetall(‘hash1′); //����array(‘key1′=>’v1′,’key2′=>’v2′,’key3′=>’v3′,’key4′=>’v4′,’key5′=>13)

/**
��������
*/

//sort ����
$redis->rpush(‘tab’,3);
$redis->rpush(‘tab’,2);
$redis->rpush(‘tab’,17);
$redis->sort(‘tab’);  //����array(2,3,17)
//ʹ�ò���,������ʹ�� array(‘sort’ => ‘desc’,'limit’ => array(1, 2))
$redis->sort(‘tab’,array(‘sort’ => ‘desc’));  //��������,����array(17,3,2)
$redis->sort(‘tab’,array(‘limit’ => array(1, 2)));  //����˳��λ����1��Ԫ��2��(������2��ָ����,������λ��),����array(3,17)
$redis->sort(‘tab’,array(‘limit’ => array(‘alpha’ => true))); //�����ַ����򷵻�array(17,2,3),��Ϊ17�����ַ���’1′��������λ��
$redis->sort(‘tab’,array(‘limit’ => array(‘store’ => ‘ordered’))); //��ʾ����������,����Ԫ�ظ���
$redis->sort(‘tab’,array(‘limit’ => array(‘get’ => ‘pre_*’))); //ʹ����ͨ����’*'����Ԫ��,��ʾֻ������’pre_’��ͷ��Ԫ��

/**
redis��������
*/

//select ָ��Ҫ���������ݿ�
$redis->select(‘mydb’); //ָ��Ϊmydb,�������򴴽�

//flushdb ���յ�ǰ��
$redis->flushdb();

//move �ƶ�������Ԫ�ص�������
$redis->set(‘foo’, ‘bar’);
$redis->move(‘foo’, ‘mydb2′); //��’mydb2′������

//info ��ʾ������״̬��Ϣ
$redis->info();

//slaveof ���ôӷ�����
$redis->slaveof(’127.0.0.1′,80); //����127.0.0.1�˿�80�ķ�����Ϊ�ӷ�����
$redis->slaveof(); //�����ӷ�����

//ͬ���������������ݵ�����
$redis->save();
//�첽�������������ݵ�����
$redis->bgsave();
//??
$redis->bgrewriteaof();
//�����������´��̵�ʱ��
$redis->lastsave();

//set/get����key-value
$mkv = array(
‘usr:0001′ => ‘First user’,
‘usr:0002′ => ‘Second user’,
‘usr:0003′ => ‘Third user’
);
$redis->mset($mkv); //�洢����key��Ӧ��value
$retval = $redis->mget(array_keys($mkv)); //��ȡ����key��Ӧ��value
print_r($retval);

//��������
$replies = $redis->pipeline(function($pipe) {
$pipe->ping();
$pipe->flushdb();
$pipe->incrby(‘counter’, 10); //��������
$pipe->incrby(‘counter’, 30);
$pipe->exists(‘counter’);
$pipe->get(‘counter’);
$pipe->mget(‘does_not_exist’, ‘counter’);
});
print_r($replies);

//CAS,�����Բ���

function zpop($client, $zsetKey) {
$element = null;
$options = array(
‘cas’   => true,     // Initialize with support for CAS operations
‘watch’ => $zsetKey, // Key that needs to be WATCHed to detect changes
‘retry’ => 3,        // Number of retries on aborted transactions, after
// which the client bails out with an exception.
);

$txReply = $client->multiExec($options, function($tx)
use ($zsetKey, &$element) {
@list($element) = $tx->zrange($zsetKey, 0, 0);
if (isset($element)) {
$tx->multi();     // With CAS, MULTI *must* be explicitly invoked.
$tx->zrem($zsetKey, $element);
}
});
return $element;
}
$zpopped = zpop($redis, ‘zset’);
echo isset($zpopped) ? “ZPOPed $zpopped” : “Nothing to ZPOP!”, “\n”;

//�Դ�ȡ��key��ǰ׺,��: ‘nrk:’
$redis->getProfile()->setPreprocessor(new KeyPrefixPreprocessor(‘nrk:’));

//�ֲ�ʽ�洢��һЩ����
$multiple_servers = array(
array(
‘host’     => ’127.0.0.1′,
‘port’     => 6379,
‘database’ => 15,
‘alias’    => ‘first’,
),
array(
‘host’     => ’127.0.0.1′,
‘port’     => 6380,
‘database’ => 15,
‘alias’    => ‘second’,
),
);

use Predis\Distribution\IDistributionStrategy;

class NaiveDistributionStrategy implements IDistributionStrategy {
private $_nodes, $_nodesCount;

public function __constructor() {
$this->_nodes = array();
$this->_nodesCount = 0;
}

public function add($node, $weight = null) {
$this->_nodes[] = $node;
$this->_nodesCount++;
}

public function remove($node) {
$this->_nodes = array_filter($this->_nodes, function($n) use($node) {
return $n !== $node;
});
$this->_nodesCount = count($this->_nodes);
}

public function get($key) {
$count = $this->_nodesCount;
if ($count === 0) {
throw new RuntimeException(‘No connections’);
}
return $this->_nodes[$count > 1 ? abs(crc32($key) % $count) : 0];
}

public function generateKey($value) {
return crc32($value);
}
}

//���ü��ֲ�����
$options = array(
‘key_distribution’ => new NaiveDistributionStrategy(),
);

$redis = new Predis\Client($multiple_servers, $options);

for ($i = 0; $i set(“key:$i”, str_pad($i, 4, ’0′, 0));
$redis->get(“key:$i”);
}

$server1 = $redis->getClientFor(‘first’)->info();
$server2 = $redis->getClientFor(‘second’)->info();

printf(“Server ‘%s’ has %d keys while server ‘%s’ has %d keys.\n”,
‘first’, $server1['db15']['keys'], ‘second’, $server2['db15']['keys']

  • ��һƪ��ʹ�� PHP ֱ���ڹ����ڴ��д洢���ݼ�
  • ��һƪ��Redis��7�ּ�������Ӧ�ó���
  • �˽���Щ�֣��ڵ���˼ �е���˼ �ĵ���˼ ������˼ ������˼ ʹ����˼ �õ���˼ ������˼