POSTS
Using Redis.
Readis
Redis is an open source, BSD licensed, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.
1.Installation
a.Mac OSX[
]1
b.Ubuntu
-> % sudo apt-get install redis-server
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libjemalloc1 redis-tools
The following NEW packages will be installed:
libjemalloc1 redis-server redis-tools
0 upgraded, 3 newly installed, 0 to remove and 84 not upgraded.
Need to get 410 kB of archives.
After this operation, 1,272 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/universe libjemalloc1 amd64 3.5.1-2 [76.8 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ trusty/universe redis-tools amd64 2:2.8.4-2 [65.7 kB]
Get:3 http://archive.ubuntu.com/ubuntu/ trusty/universe redis-server amd64 2:2.8.4-2 [267 kB]
Fetched 410 kB in 2s (176 kB/s)
Selecting previously unselected package libjemalloc1.
(Reading database ... 300693 files and directories currently installed.)
Preparing to unpack .../libjemalloc1_3.5.1-2_amd64.deb ...
Unpacking libjemalloc1 (3.5.1-2) ...
Selecting previously unselected package redis-tools.
Preparing to unpack .../redis-tools_2%3a2.8.4-2_amd64.deb ...
Unpacking redis-tools (2:2.8.4-2) ...
Selecting previously unselected package redis-server.
Preparing to unpack .../redis-server_2%3a2.8.4-2_amd64.deb ...
Unpacking redis-server (2:2.8.4-2) ...
Processing triggers for man-db (2.6.7.1-1) ...
Processing triggers for ureadahead (0.100.0-16) ...
ureadahead will be reprofiled on next reboot
Setting up libjemalloc1 (3.5.1-2) ...
Setting up redis-tools (2:2.8.4-2) ...
Setting up redis-server (2:2.8.4-2) ...
Starting redis-server: redis-server.
Processing triggers for libc-bin (2.19-0ubuntu6) ...
Processing triggers for ureadahead (0.100.0-16) ...
stevelo@stevelo-pc [12:01:47] [~]
1.Installation PHP extension
https://github.com/nicolasff/phpredis
install extension
luosteve@SteveMac [01:26:11] [/tmp]
-> % git clone https://github.com/nicolasff/phpredis.git
Cloning into 'phpredis'...
remote: Counting objects: 4731, done.
remote: Compressing objects: 100% (1721/1721), done.
remote: Total 4731 (delta 2999), reused 4731 (delta 2999)
Receiving objects: 100% (4731/4731), 2.33 MiB | 319.00 KiB/s, done.
Resolving deltas: 100% (2999/2999), done.
Checking connectivity... done.
luosteve@SteveMac [01:26:43] [/tmp]
-> % cd phpredis
luosteve@SteveMac [01:26:49] [/tmp/phpredis] [master]
-> % phpize
Configuring for:
PHP Api Version: 20121113
Zend Module Api No: 20121212
Zend Extension Api No: 220121212
phpize 1.07s user 0.34s system 97% cpu 1.446 total
luosteve@SteveMac [01:27:34] [/tmp/phpredis] [master *]
-> % ./configure
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking for cc... cc
........................................................................
creating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
./configure 1.70s user 2.00s system 99% cpu 3.702 total
luosteve@SteveMac [01:27:43] [/tmp/phpredis] [master *]
-> % make
/bin/sh /tmp/phpredis/libtool --mode=compile cc -I. -I/tmp/phpredis -DPHP_ATOM_INC -I/tmp/phpredis/include -I/tmp/phpredis/main -I/tmp/phpredis -I/Users/luosteve/.phpbrew/php/php-5.5.11/include/php -I/Users/luosteve/.phpbrew/php/php-5.5.11/include/php/main -I/Users/luosteve/.phpbrew/php/php-5.5.11/include/php/TSRM -I/Users/luosteve/.phpbrew/php/php-5.5.11/include/php/Zend -I/Users/luosteve/.phpbrew/php/php-5.5.11/include/php/ext -I/Users/luosteve/.phpbrew/php/php-5.5.11/include/php/ext/
.......................................................................
#add "extension=redis.so" to you php.ini
PHP Session handler
You just need add this to you php.ini.
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"
#tcp:://<hostname or IP>:<port>?weight=<int>?&timeout=<float>?timeout=<float>
Easy way
set
// Simple key -> value set
$redis->set('key', 'value');
// Will redirect, and actually make an SETEX call
$redis->set('key','value', 10);
// Will set the key, if it doesn't exist, with a ttl of 10 seconds
$redis->set('key', 'value', Array('nx', 'ex'=>10));
// Will set a key, if it does exist, with a ttl of 1000 miliseconds
$redis->set('key', 'value', Array('xx', 'px'=>1000));
get
$redis->get('key');
get
$redis->get('key');
del, delete
$redis->set('key1', 'val1');
$redis->set('key2', 'val2');
$redis->set('key3', 'val3');
$redis->set('key4', 'val4');
$redis->delete('key1', 'key2'); /* return 2 */
exists
$redis->set('key', 'value');
$redis->exists('key'); /* TRUE */
$redis->exists('NonExistingKey'); /* FALSE */
incr, incrBy
$redis->incr('key1'); /* key1 didn't exists, set to 0 before the increment */
/* and now has the value 1 */
$redis->incr('key1'); /* 2 */
$redis->incr('key1'); /* 3 */
$redis->incr('key1'); /* 4 */
$redis->incrBy('key1', 10); /* 14 */
decr, decrBy
$redis->decr('key1'); /* key1 didn't exists, set to 0 before the increment */
/* and now has the value -1 */
$redis->decr('key1'); /* -2 */
$redis->decr('key1'); /* -3 */
$redis->decrBy('key1', 10); /* -13 */
mGet, getMultiple
$redis->set('key1', 'value1');
$redis->set('key2', 'value2');
$redis->set('key3', 'value3');
$redis->mGet(array('key1', 'key2', 'key3')); /* array('value1', 'value2', 'value3');
$redis->mGet(array('key0', 'key1', 'key5')); /* array(`FALSE`, 'value2', `FALSE`);
getSet
$redis->set('x', '42');
$exValue = $redis->getSet('x', 'lol'); // return '42', replaces x by 'lol'
$newValue = $redis->get('x')' // return 'lol'
mset, msetnx
$redis->mset(array('key0' => 'value0', 'key1' => 'value1'));
var_dump($redis->get('key0'));
var_dump($redis->get('key1'));
migrate
Migrates a key to a different Redis instance.
$redis->migrate('backup', 6379, 'foo', 0, 3600);
$redis->migrate('backup', 6379, 'foo', 0, 3600, true, true); /* copy and replace */
$redis->migrate('backup', 6379, 'foo', 0, 3600, false, true); /* just REPLACE flag */
read
- Redis data types and abstractions
- Redis Lua scripting: Redis 2.6 Lua scripting feature documentation.
- Pipelining: Learn how to send multiple commands at once, saving on round trip time.
- Redis Desktop Manager