This extension provides an API for communicating with Redis database, a persistent key-value database with built-in net interface written in ANSI-C for Posix systems.
It is a fork of alfonsojimenez's phpredis, adding many methods and fixing a lot of issues.
This code is maintained by Owlient. You can send comments, patches, questions here on github or to:
phpize ./configure make && make install
You can generate a debian package for PHP5, accessible from Apache 2 by running ./mkdeb-apache2.sh
or with dpkg-buildpackage
or svn-buildpackage
.
This extension exports a single class, Redis
(and RedisException
used in case of errors).
If the install fails on OSX, type the following commands in your shell before trying again:
MACOSX_DEPLOYMENT_TARGET=10.6 CFLAGS="-arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp" CCFLAGS="-arch i386 -arch x86_64 -g -Os -pipe" CXXFLAGS="-arch i386 -arch x86_64 -g -Os -pipe" LDFLAGS="-arch i386 -arch x86_64 -bind_at_load" export CFLAGS CXXFLAGS LDFLAGS CCFLAGS MACOSX_DEPLOYMENT_TARGET
phpredis can be used to store PHP sessions. To do this, configure session.save_handler
and session.save_path
in your php.ini to tell phpredis where to store the sessions:
session.save_handler = redis session.save_path = "tcp://host1:6379?weight=1, tcp://host2:6379?weight=2&timeout=2.5, tcp://host3:6379?weight=2"
session.save_path
can have a simple host:port
format too, but you need to provide the tcp://
scheme if you want to use the parameters. The following parameters are available:
- weight (integer): the weight of a host is used in comparison with the others in order to customize the session distribution on several hosts. If host A has twice the weight of host B, it will get twice the amount of sessions. In the example, host1 stores 20% of all the sessions (1/(1+2+2)) while host2 and host3 each store 40% (2/1+2+2). The target host is determined once and for all at the start of the session, and doesn't change. The default weight is 1.
- timeout (float): the connection timeout to a redis host, expressed in seconds. If the host is unreachable in that amount of time, the session storage will be unavailable for the client. The default timeout is very high (86400 seconds).
Sessions have a lifetime expressed in seconds and stored in the INI variable "session.gc_maxlifetime". You can change it with ini_set()
.
phpredis throws a RedisException
object if it can't reach the Redis server. That can happen in case of connectivity issues, if the Redis service is down, or if the redis host is overloaded. In any other problematic case that does not involve an unreachable server (such as a key not existing, an invalid command, etc), phpredis will return FALSE
.
Creates a Redis client
$redis = new Redis();
Connects to a Redis instance.
host: string. can be a host, or the path to a unix domain socket
port: int, optional
timeout: float, value in seconds (optional, default is 0 meaning unlimited)
BOOL: TRUE
on success, FALSE
on error.
$redis->connect('127.0.0.1', 6379); $redis->connect('127.0.0.1'); // port 6379 by default $redis->connect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout. $redis->connect('/tmp/redis.sock'); // unix domain socket.
Connects to a Redis instance or reuse a connection already established with pconnect
/popen
.
The connection will not be closed on close
or end of request until the php process ends.
So be patient on to many open FD's (specially on redis server side) when using persistent
connections on many servers connecting to one redis server.
Also more than one persistent connection can be made identified by either host + port + timeout or unix socket + timeout.
This feature is not available in threaded versions. pconnect
and popen
then working like their non
persistent equivalents.
host: string. can be a host, or the path to a unix domain socket
port: int, optional
timeout: float, value in seconds (optional, default is 0 meaning unlimited)
BOOL: TRUE
on success, FALSE
on error.
$redis->pconnect('127.0.0.1', 6379); $redis->pconnect('127.0.0.1'); // port 6379 by default - same connection like before. $redis->pconnect('127.0.0.1', 6379, 2.5); // 2.5 sec timeout and would be another connection then the two before. $redis->pconnect('/tmp/redis.sock'); // unix domain socket - would be another connection then the three before.
Set client option.
parameter name
parameter value
BOOL: TRUE
on success, FALSE
on error.
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // don't serialize data $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); // use built-in serialize/unserialize $redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); // use igBinary serialize/unserialize
Get client option.
parameter name
Parameter value.
$redis->getOption(Redis::OPT_SERIALIZER); // return Redis::SERIALIZER_NONE, Redis::SERIALIZER_PHP, or Redis::SERIALIZER_IGBINARY.
Check the current connection status
(none)
STRING: +PONG
on success. Throws a RedisException object on connectivity error, as described above.
Get the value related to the specified key
key
String or Bool: If key didn't exist, FALSE
is returned. Otherwise, the value related to this key is returned.
$redis->get('key');
Set the string value in argument as value of the key.
Key
Value
Timeout (optional). Calling SETEX
is preferred if you want a timeout.
Bool TRUE
if the command is successful.
$redis->set('key', 'value');
Set the string value in argument as value of the key, with a time to live.
Key TTL Value
Bool TRUE
if the command is successful.
$redis->setex('key', 3600, 'value'); // sets key → value, with 1h TTL.
Set the string value in argument as value of the key if the key doesn't already exist in the database.
key value
< F438 div class="markdown-heading" dir="auto">