Redis is a server: all commands involve network or IPC roundtrips. It is meaningless to compare it to embedded data stores such as SQLite, Berkeley DB, etc ... because the cost of most operations is precisely dominated by network/protocol management. redis manual
Access to the Redis port should be denied to everybody but trusted clients in the network, so the servers running Redis should be directly accessible only by the computers implementing the application using Redis.
redis.conf
# secure, just localhost bind 127.0.0.1 # development all interfaces bind 0.0.0.0
in production, use ssh tunnell(port 22) or webadmin UI connecting in localhost.
sudo /etc/init.d/redis-server start
redis-cli ping
dati stringa:
set test test1 get test keys user:* # le chiavi che rispondono alla regex del test
dati lista, servono a fare code e stack in cui si lavora spesso sulle estremità
#con lpush il messaggio più recente sarà in testa, con rpush sarà accodato lpush list a lpush list b lrange list 0 2 # i primi 3 lrange list 0 -1 # dal primo, all'ultimo lrange list -3 -1 # ultimi 3 ltrim list 0 2 # toglie elementi dalla coda, qui la lascia a 3 elementi, ideale per mantenere una lista limitata in grandezza
gli hash sono molto simili a una tabella in memoria
hmset user:1 name mario email test hmget user:1 name email
ordinamento dei risultati
SORT user:1:followers BY ALPHA SORT user:1:followers->name BY ALPHA
# concatenare comandi per estrarre valori dal db redis-cli KEYS user:*:name | xargs redis-cli MGET
sorted sets: viene uilizzato un campo score per tenere ordinato il set
zadd hackers 1940 "Alan Kay" zadd hackers 1953 "Richard Stallman" zadd hackers 1965 "Yukihiro Matsumoto" zrange hackers 0 -1
delete multiple keys:
redis-cli keys "ereg_*" | xargs redis-cli del
see also bitmaps, hyperloglogs
List commands:
Hash commands:
Sets Commands:
TTL COmmands:
git clone https://github.com/ErikDubbelboer/phpRedisAdmin.git cd phpRedisAdmin git clone https://github.com/nrk/predis.git vendor
Predis:
require "predis/autoload.php";
Predis\Autoloader::register();
$R = new Predis\Client(array(
'scheme' => 'tcp',
'host' => 'hostname',
'port' => port,
'password' => 'password',
'persistent' => '1' // persistent!
));
$R->set("foo", "bar");
$R->setex("key", "value", $ttl=1000);//expiring data!
$value = $R->get("foo");
var_dump($value);
if( $R->exists('random') ){
echo $R->get('random');
}
// counters
$R->set("counter", 0);
$R->incr("counter"); // 1
$R->incr("counter"); // 2
$R->decr("counter"); // 1
$R->incrby("counter", 5); // 20
$R->decrby("counter", 10); // 10
// LIST
$R->rpush("languages", "french"); // [french]
$R->rpush("languages", "arabic"); // [french, arabic]
$R->lpush("languages", "english"); // [english, french, arabic]
$R->lpush("languages", "swedish"); // [swedish, english, french, arabic]
$R->lpop("languages"); // [english, french, arabic]
$R->rpop("languages"); // [english, french]
$R->llen("languages"); // 2
$R->lrange("languages", 0, -1); // returns all elements
$R->lrange("languages", 0, 1); // [english, french]
//----------------------------------------------------------------------------
// hash
$key = ';linus torvalds';;
$R->hset($key, ';age';, 44);
$R->hset($key, ';country';, ';finland';);
$R->hset($key, 'occupation', 'software engineer');
$R->hset($key, 'reknown', 'linux kernel');
$R->hset($key, 'to delete', 'i will be deleted');
$R->get($key, 'age'); // 44
$R->get($key, 'country')); // Finland
$R->del($key, 'to delete');
$R->hincrby($key, 'age', 20); // 64
$R->hmset($key, [
'age' => 44,
'country' => 'finland',
'occupation' => 'software engineer',
'reknown' => 'linux kernel',
]);
// finally
$data = $R->hgetall($key);
print_r($data);
//------------------------------------------------------------------------------
// sets
$key = "countries";
$R->sadd($key, ';china';);
$R->sadd($key, ['england', 'france', 'germany']);
$R->sadd($key, 'china'); // this entry is ignored
$R->srem($key, ['england', 'china']);
$R->sismember($key, 'england'); // false
$R->smembers($key); // ['france', 'germany']
//------------------------------------------------------------------------------
// expiration example
$key = "expire in 1 hour";
$R->expire($key, 3600); // expires in 1 hour
$R->expireat($key, time() + 3600); // expires in 1 hour
sleep(600); // don't try this, just an illustration for time spent
$R->ttl($key); // 3000, ergo expires in 50 minutes
$R->persist($key); // this will never expire.
pecl install redis # Now get Redis from http://pecl.php.net/package/redis and make a package cd php-redis-2.2.3/ debuild dpkg -c ../php5-redis_2.2.3-1_amd64.deb sudo dpkg -i ../php5-redis_2.2.3-1_amd64.deb
test code connection
<?php
$R = new Redis() or die("Cannot load Redis module.");
$R->connect('localhost');
$R->set('random', rand(5000,6000));
queuing system in a distributed platform. You can implement such a queue using the LPUSH and BRPOP commands. With LPUSH you add elements to the start of the list, while the BRPOP removes elements from the list, blocking when no elements are available. But if you want that queue to be reliable, you can consider using the BRPOPLPUSH command, that can add the returned element to an ‘in progress' queue before returning it to the client. This way you can remove the element from the ‘in progress' queue when you successfully handled them, or put them back on the original queue if you failed.
be used in the same manner as memcache.
Pub/Sub. Keeping a map of who is interested in updates to what data is a common task in systems. Redis has a pub/sub feature to make this easy using commands like SUBSCRIBE, UNSUBSCRIBE, and PUBLISH.
PHP-Resque: Workers and Queueing