解压后里面有:lib 源文件 、examples 例子、test测试
将lib目录拷贝到你的项目中,就可以开始你的predis操作了。
Predis is a flexible and feature-complete Redis client library for PHP >= 5.3.
By default Predis does not require any additional C extension, but it can be optionally paired with phpiredis to lower the overhead of serializing and parsing the Redis protocol. An asynchronous implementation of the client, albeit experimental, is also available through Predis\Async.
Predis can be used with HHVM >= 2.4.0, but there are no guarantees you will not run into unexpected issues (especially with the JIT compiler enabled via Eval.Jit = true) due to HHVM being still under heavy development, thus unstable and not yet 100% compatible with PHP.
More details about the project can be found in our frequently asked questions section or on the online wiki.
predis是php连接redis的操作库,由于它完全使用php编写(不需要安装),大量使用命名空间以及闭包等功能,只支持php5.3以上版本,故实测性能一般,每秒25000次读写,相信改换c语言编写的php扩展后性能会大幅提升(比如使用C扩展phpredis https://github.com/owlient/phpredis)。
将session数据存放到redis也很简单:
session.save_handler = redis
session.save_path = “tcp://127.0.0.1:6379″
官方使用说明:
Loading the library
Predis relies on the autoloading features of PHP to load its files when needed and complies with the PSR-0 standard which makes it compatible with most PHP frameworks. Autoloading is handled automatically when dependencies are managed using Composer, but you can also leverage its own autoloader if you are going to use it in a project or script without any PSR-0 compliant autoloading facility:
// Prepend a base path if Predis is not available in your "include_path".
require 'Predis/Autoloader.php';
Predis\Autoloader::register();
It is possible to easily create a phar archive from the repository just by launching bin/create-phar. The generated phar contains a stub defining an autoloader function for Predis, so you just need to require the phar to start using the library. Alternatively, it is also possible to generate one single PHP file that holds every class like older versions of Predis by launching bin/create-single-file, but this practice is not encouraged.
Connecting to Redis
When not specifying any connection parameter to create a new client, Predis assumes 127.0.0.1 and 6379as the default host and port and uses a connection timeout of 5 seconds:
$client = new Predis\Client();
$client->set('foo', 'bar');
$value = $client->get('foo');
Connection parameters can be supplied either in the form of URI strings or named arrays. While the latter is the preferred way to supply parameters, URI strings can be useful for quick configurations or when parameters are read from a non-structured source:
// Named array of connection parameters:
$client = new Predis\Client([
'scheme' => 'tcp',
'host' => '10.0.0.1',
'port' => 6379,
]);
// Same set of parameters, but using an URI string:
$client = new Predis\Client('tcp://10.0.0.1:6379');
When an array of connections parameters is provided, Predis automatically works in clustering mode using client-side sharding. Both named arrays and URI strings can be mixed for providing each node configuration:
The actual list of supported connection parameters can vary depending on each connection backend so it is recommended to refer to their specific documentation for details.