复制代码
spl_autoload_register(function($class) {
$file = __DIR__.'/lib/Predis/'.$class.'.php';
if (file_exists($file)) {
require $file;
return true;
}
});
$server = array(
'host' => '127.0.0.1',
'port' => 6379,
'database' => 15
);
$redis = new Client($server);
$redis->set('library', 'predis');
$retval = $redis->get('library');
echo $retval;
$redis->setex('str', 10, 'bar');
$redis->setnx('foo',12);
$redis->setnx('foo',34);
$redis->getset('foo',56);
$redis->incr('foo');
$redis->incrby('foo',2);
$redis->exists('foo');
$redis->del('foo');
$redis->type('foo');
$redis->set('str','test');
$redis->type('str');
$redis->append('str','_123');
$redis->setrange('str',0,'abc');
$redis->setrange('str',2,'cd');
$redis->substr('str',0,2);
$redis->strlen('str');
$redis->setbit('binary',31,1);
$redis->getbit('binary',31);
$redis->set('foo1',123);
$redis->set('foo2',456);
$redis->keys('foo*');
$redis->keys('f?o?');
$redis->randomkey();
$redis->rename('str','str2');
$redis->expire('foo', 1);
$redis->ttl('foo');
$redis->expire('foo');
$redis->dbsize();
$redis->rpush('fooList', 'bar1');
$redis->lpush('fooList', 'bar0');
$redis->rpushx('fooList', 'bar2');
$redis->llen('fooList');
$redis->lrange('fooList',0,1);
$redis->lrange('fooList',0,-1);
$redis->lindex('fooList',1);
$redis->lset('fooList',1,'123');
$redis->lrem('fooList',1,'_');
$redis->lpop('fooList');
$redis->rpop('fooList');
$redis->ltrim('fooList', 0,1);
$redis->rpush('list1','ab0');
$redis->rpush('list1','ab1');
$redis->rpush('list2','ab2');
$redis->rpush('list2','ab3');
$redis->rpoplpush('list1','list2');
$redis->rpoplpush('list2','list2');
$redis->linsert('list2', 'before','ab1','123');
$redis->linsert('list2', 'after','ab1','456');
$redis->blpop('list3',10);
$redis->sadd('set1','ab');
$redis->sadd('set1','cd');
$redis->sadd('set1','ef');
$redis->srem('set1','cd');
$redis->spop('set1');
$redis->sadd('set2','123');
$redis->smove('set1','set2','ab');
$redis->scard('set2');
$redis->sismember('set2','123');
$redis->smembers('set2');
$redis->sadd('set1','ab');
$redis->sinter('set2','set1');
$redis->set('foo',0);
$redis->sinterstore('foo','set1');
$redis->sinterstore('foo',array('set1','set2'));
$redis->srandmember('set1');
$redis->zadd('zset1',1,'ab');
$redis->zadd('zset1',2,'cd');
$redis->zadd('zset1',3,'ef');
$redis->zincrby('zset1',10,'ab');
$redis->zrem('zset1','ef');
$redis->zrange('zset1',0,1);
$redis->zrange('zset1',0,-1);
$redis->zrevrange('zset1',0,-1);
$redis->zadd('zset1',3,'ef');
$redis->zadd('zset1',5,'gh');
$redis->zrangebyscore('zset1',2,9);
$redis->zrangebyscore('zset1',2,9,'withscores');
$redis->zrangebyscore('zset1',2,9,array('withscores' =>true,'limit'=>array(1, 2)));
$redis->zunionstore('zset3',array('zset1','zset2','zset0'));
$redis->zunionstore('zset3',array('zset1','zset2'),array('weights' => array(5,0)));
$redis->zunionstore('zset3',array('zset1','zset2'),array('aggregate' => 'max'));
$redis->zcount('zset1',3,5);
$redis->zcount('zset1','(3',5));
$redis->zcard('zset1');
$redis->zscore('zset1','ef');
$redis->zremrangebyscore('zset1',0,2);
$redis->zrank('zset1','ef');
$redis->zremrangebyrank('zset1',0,10);
$redis->hset('hash1','key1','v1');
$redis->hset('hash1','key2','v2');
$redis->hget('hash1','key1');
$redis->hexists ('hash1','key1');
$redis->hdel('hash1','key2');
$redis->hlen('hash1');
$redis->hsetnx('hash1','key1','v2');
$redis->hsetnx('hash1','key2','v2');
$redis->hmset('hash1',array('key3'=>'v3','key4'=>'v4'));
$redis->hmget('hash1',array('key3','key4'));
$redis->hincrby('hash1','key5',3);
$redis->hincrby('hash1','key5',10);
$redis->hkeys('hash1');
$redis->hvals('hash1');
$redis->hgetall('hash1');
$redis->rpush('tab',3);
$redis->rpush('tab',2);
$redis->rpush('tab',17);
$redis->sort('tab');
$redis->sort('tab',array('sort' => 'desc'));
$redis->sort('tab',array('limit' => array(1, 2)));
$redis->sort('tab',array('limit' => array('alpha' => true)));
$redis->sort('tab',array('limit' => array('store' => 'ordered')));
$redis->sort('tab',array('limit' => array('get' => 'pre_*')));
$redis->select('mydb');
$redis->flushdb();
$redis->set('foo', 'bar');
$redis->move('foo', 'mydb2');
$redis->info();
$redis->slaveof('127.0.0.1',80);
$redis->slaveof();
$redis->save();
$redis->bgsave();
$redis->bgrewriteaof();
$redis->lastsave();
$mkv = array(
'usr:0001' => 'First user',
'usr:0002' => 'Second user',
'usr:0003' => 'Third user'
);
$redis->mset($mkv);
$retval = $redis->mget(array_keys($mkv));
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);
function zpop($client, $zsetKey) {
$element = null;
$options = array(
'cas' => true,
'watch' => $zsetKey,
'retry' => 3,
);
$txReply = $client->multiExec($options, function($tx)
use ($zsetKey, &$element) {
@list($element) = $tx->zrange($zsetKey, 0, 0);
if (isset($element)) {
$tx->multi();
$tx->zrem($zsetKey, $element);
}
});
return $element;
}
$zpopped = zpop($redis, 'zset');
echo isset($zpopped) ? "ZPOPed $zpopped" : "Nothing to ZPOP!", "\n";
$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']
);
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通