[9] Redis – PHP
28 stycznia 2022To jest przykład użycia Redisa w PHP.
[1] Zainstaluj moduł klienta Redis PHP.
[root@vlsr01 ~]$ dnf install php-pear php-devel gcc make [root@vlsr01 ~]# pecl channel-update pecl.php.net [root@vlsr01 ~]# pecl install redis [root@vlsr01 ~]# echo "extension=redis.so" > /etc/php.d/99-redis.ini
[2] Podstawowe użycie Redis w PHP.
[user01@vlsr01 ~]$ mcedit use_redis.php //utwórz nowy <?php $redis = new Redis(); $redis->connect("127.0.0.1",6379); $redis->auth("TajneHasło"); //ustaw i pobierz klucz $redis->set('key01', 'value01'); print 'key01.value : ' . $redis->get('key01') . "\n"; //dodaj i pobierz klucz $redis->append('key01', ',value02'); print 'key01.value : ' . $redis->get('key01') . "\n"; $redis->set('key02', 1); print 'key02.value : ' . $redis->get('key02') . "\n"; //inkrementuj $redis->incr('key02', 100); print 'key02.value : ' . $redis->get('key02') . "\n"; //decrementuj $redis->decr('key02', 51); print 'key02.value : ' . $redis->get('key02') . "\n"; //listuj $redis->lPush('list01', 'value01'); $redis->rPush('list01', 'value02'); print 'list01.value : '; print_r ($redis->lRange('list01', 0, -1)); //mieszaj $redis->hSet('hash01', 'key01', 'value01'); $redis->hSet('hash01', 'key02', 'value02'); print 'hash01.value : '; print_r ($redis->hGetAll('hash01')); //ustaw $redis->sAdd('set01', 'member01'); $redis->sAdd('set01', 'member02'); print 'set01.value : '; print_r ($redis->sMembers('set01')); ?> #uruchom [user01@vlsr01 ~]$ php use_redis.php key01.value : value01 key01.value : value01,value02 key02.value : 1 key02.value : 101 key02.value : 50 list01.value : Array ( [0] => value01 [1] => value03 [2] => value02 [3] => value01 [4] => value02 ) hash01.value : Array ( [key01] => value01 [key02] => value02 [key03] => value03 ) set01.value : Array ( [0] => member02 [1] => member01 [2] => member03 )