CentOS Stream 8
Sponsored Link

Redis 6 : Use on PHP2021/06/28

 
This is an example to use Redis on PHP.
[1] Install PHP Redis Client Module.
[root@www ~]#
dnf -y install php-pear php-devel gcc make

[root@www ~]#
pecl install redis

[root@www ~]#
echo "extension=redis.so" > /etc/php.d/99-redis.ini

[2] Basic usage on PHP.
[cent@www ~]$
vi use_redis.php
<?php
$redis = new Redis();
$redis->connect("127.0.0.1",6379);
$redis->auth("password");

// set and get Key
$redis->set('key01', 'value01');
print 'key01.value : ' . $redis->get('key01') . "\n";

// append and get Key
$redis->append('key01', ',value02');
print 'key01.value : ' . $redis->get('key01') . "\n";

$redis->set('key02', 1);
print 'key02.value : ' . $redis->get('key02') . "\n";

// increment
$redis->incr('key02', 100);
print 'key02.value : ' . $redis->get('key02') . "\n";

// decrement
$redis->decr('key02', 51);
print 'key02.value : ' . $redis->get('key02') . "\n";

// list
$redis->lPush('list01', 'value01');
$redis->rPush('list01', 'value02');
print 'list01.value : ';
print_r ($redis->lRange('list01', 0, -1));

// hash
$redis->hSet('hash01', 'key01', 'value01');
$redis->hSet('hash01', 'key02', 'value02');
print 'hash01.value : ';
print_r ($redis->hGetAll('hash01'));

// set
$redis->sAdd('set01', 'member01');
$redis->sAdd('set01', 'member02');
print 'set01.value : ';
print_r ($redis->sMembers('set01'));
?>

# run

[cent@www ~]$
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] => value02
)
hash01.value : Array
(
    [key01] => value01
    [key02] => value02
)
set01.value : Array
(
    [0] => member02
    [1] => member01
)
Matched Content