Redis with phpredis (PHP extension)
Uses the redis PHP extension 6.3. The code is an ordinary application for the original database; nothing in it knows about MachNoSQL.
The only change
Section titled “The only change”REDIS_HOST=redis.example.comREDIS_HOST=machnosql.example.comThe address comes from the environment variable REDIS_HOST, so switching to MachNoSQL changes configuration only.
export REDIS_HOST='machnosql.example.com'pecl install redisexport REDIS_PASSWORD=secretphp app.phpSource
Section titled “Source”The program as it is kept and tested in the Mach Database NoSQL repository; highlighted lines read the connection setting.
<?php// Cache, counter, session and leaderboard on Redis, unchanged on MachNoSQL.// phpredis takes host, port and password separately; they come from the environment.
$redis = new Redis();$redis->connect(getenv('REDIS_HOST'), (int) (getenv('REDIS_PORT') ?: 6379));if (getenv('REDIS_PASSWORD') !== false) { $redis->auth(getenv('REDIS_PASSWORD'));}
$redis->set('cache:home', '<html>...</html>', ['EX' => 60]);echo 'cached: ', $redis->get('cache:home'), ' ttl: ', $redis->ttl('cache:home'), PHP_EOL;
echo 'views: ', $redis->incr('views:home'), PHP_EOL;
$redis->hMSet('session:42', ['user' => 'ada', 'cart_items' => 3]);print_r($redis->hGetAll('session:42'));
$redis->zAdd('leaderboard', 120, 'ada', 95, 'linus', 130, 'grace');print_r($redis->zRevRange('leaderboard', 0, 2, true));
$results = $redis->multi() ->incr('orders:count') ->lPush('orders:recent', 'order-1001') ->lTrim('orders:recent', 0, 99) ->exec();print_r($results);