Redis with Predis (PHP)
Uses Predis 3.7. 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://:secret@redis.example.com:6379/0redis://:secret@machnosql.example.com:6379/0The address comes from the environment variable REDIS_URL, so switching to MachNoSQL changes configuration only.
export REDIS_URL='redis://:secret@machnosql.example.com:6379/0'composer installphp 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.require __DIR__ . '/vendor/autoload.php';
$redis = new Predis\Client(getenv('REDIS_URL'));
$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->hset('session:42', 'user', 'ada', 'cart_items', 3);print_r($redis->hgetall('session:42'));
$redis->zadd('leaderboard', ['ada' => 120, 'linus' => 95, 'grace' => 130]);print_r($redis->zrevrange('leaderboard', 0, 2, ['withscores' => true]));
$results = $redis->transaction(function ($tx) { $tx->incr('orders:count'); $tx->lpush('orders:recent', 'order-1001'); $tx->ltrim('orders:recent', 0, 99);});print_r($results);{ "name": "example/machnosql-redis-predis", "require": { "php": ">=8.3", "predis/predis": "^3.7" }}