The phpredis extension provides an API for communicating with the Redis key-value store. It is released under the PHP License, version 3.01. This code has been developed and maintained by Owlient from November 2009 to March 2011.
You can send comments, patches, questions here on github or to n.favrefelix@gmail.com (@yowgi).
Everything you should need to install PhpRedis on your system.
phpize
./configure [--enable-redis-igbinary]
make && make install
If you would like phpredis to serialize your data using the igbinary library, run configure with --enable-redis-igbinary
.
make install
copies redis.so
to an appropriate location, but you still need to enable the module in the PHP config file. To do so, either edit your php.ini or add a redis.ini file in /etc/php5/conf.d
with the following contents: extension=redis.so
.
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). Check out https://github.com/ukko/phpredis-phpdoc for a PHP stub that you can use in your IDE for code completion.
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
If that still fails and you are running Zend Server CE, try this right before "make": ./configure CFLAGS="-arch i386"
.
Taken from Compiling phpredis on Zend Server CE/OSX .
See also: Install Redis & PHP Extension PHPRedis with Macports.
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).
- persistent (integer, should be 1 or 0): defines if a persistent connection should be used. (experimental setting)
- prefix (string, defaults to "PHPREDIS_SESSION:"): used as a prefix to the Redis key in which the session is stored. The key is composed of the prefix followed by the session ID.
- auth (string, empty by default): used to authenticate with the server prior to sending commands.
- database (integer): selects a different database.
Sessions have a lifetime expressed in seconds and stored in the INI variable "session.gc_maxlifetime". You can change it with ini_set()
.
The session handler requires a version of Redis with the SETEX
command (at least 2.0).
phpredis can also connect to a unix domain socket: session.save_path = "unix:///var/run/redis/redis.sock?persistent=1&weight=1&database=0
.
See instructions from @char101 on how to build phpredis on Windows.
See dedicated page.
Description: Creates a Redis client
$redis = new Redis();
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
.
Description: Available Redis Constants
Redis data types, as returned by type
Redis::REDIS_STRING - String
Redis::REDIS_SET - Set
Redis::REDIS_LIST - List
Redis::REDIS_ZSET - Sorted set
Redis::REDIS_HASH - Hash
Redis::REDIS_NOT_FOUND - Not found / other
@TODO: OPT_SERIALIZER, AFTER, BEFORE,...
- connect, open - Connect to a server
- pconnect, popen - Connect to a server (persistent)
- auth - Authenticate to the server
- select - Change the selected database for the current connection
- close - Close the connection
- setOption - Set client option
- getOption - Get client option
- ping - Ping the server
- echo - Echo the given string
Description: 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.
Description: 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 host + persistent_id 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)
persistent_id: string. identity for the requested persistent connection
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 than the two before.
$redis->pconnect('127.0.0.1', 6379, 2.5, 'x'); // x is sent as persistent_id and would be another connection the the three before.
$redis->pconnect('/tmp/redis.sock'); // unix domain socket - would be another connection than the four before.
Description: Authenticate the connection using a password. Warning: The password is sent in plain-text over the network.
STRING: password
BOOL: TRUE
if the connection is authenticated, FALSE
otherwise.
$redis->auth('foobared');
Description: Change the selected database for the current connection.
INTEGER: dbindex, the database number to switch to.
TRUE
in case of success, FALSE
in case of failure.
See method for example: move
Description: Disconnects from the Redis instance, except when pconnect
is used.
Description: 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
$redis->setOption(Redis::OPT_PREFIX, 'myAppName:'); // use custom prefix on all keys
Description: Get client option.
parameter name
Parameter value.
< F438 div class="markdown-heading" dir="auto">