Skip to content

Redis Sample Application

Build a REST app that performs Redis operations through a RedisDemoController. You use Winter Boot for the application runtime and the Winter Redis module from Winter Modules for single-node access via PhpRedisTemplate.

You need PHP 8.5 or later with the swoole, redis, and pcntl extensions. You also need a Redis server. The sample config points at localhost:30637 with password redis123 — replace the host, port, and password with your own server.

Start Redis before you run the app:

Terminal window
docker run -d -p 30637:6379 --name redis \
redis:7 redis-server --requirepass redis123

The sample uses this layout:

redis/
├── bin/
│ └── application.php # Application entry point
├── config/
│ ├── application.yml # Winter Boot application config
│ └── redis-config.yml # Redis single-node connections config
├── src/
│ ├── RedisSampleApplication.php # Main application class
│ └── controller/
│ └── RedisDemoController.php # Redis operations controller
└── composer.json # Dependencies

Require the framework and the modules package:

Terminal window
composer require suvera/winter-boot suvera/winter-modules

The redis PHP extension must be installed separately:

Terminal window
pecl install redis

Switch between the source files. Each tab shows the exact file from the sample.

The single entry point. It points Winter Boot at the config directory and at the sample namespace.

<?php
namespace dev\example;
use dev\winterframework\stereotype\WinterBootApplication;
#[WinterBootApplication(
configDirectory: [__DIR__ . "/../config"],
scanNamespaces: [
['dev\\example', __DIR__ . '']
]
)]
class RedisSampleApplication {
public static function main(): void {
$winterApp = new \dev\winterframework\core\app\WinterWebSwooleApplication();
$winterApp->run(self::class);
}
}

Switch between the two config files. application.yml enables the module, and redis-config.yml defines the primary single-node connection.

The full sample file registers RedisModule and sets the server and app identity:

server:
port: 8080
address: 0.0.0.0
context-path: /
winter:
application:
name: Redis Sample Application
id: redis-sample-app
version: 1.0.0
modules:
- module: dev\winterframework\data\redis\RedisModule
enabled: true
configFile: redis-config.yml

See Configuration for every application.yml key.

Start the app, then call the endpoints to store a string, work a hash, and run the full demo.

1. Start the application:

Terminal window
composer install
php bin/application.php

2. Check connectivity:

Terminal window
curl "http://localhost:8080/redisdemo/ping"

3. Set a string with a 60s TTL:

Terminal window
curl -X POST "http://localhost:8080/redisdemo/string?key=my-key&value=Hello%20Redis&ttl=60"

4. Get the string:

Terminal window
curl "http://localhost:8080/redisdemo/string?key=my-key"

5. Increment a counter:

Terminal window
curl -X POST "http://localhost:8080/redisdemo/incr?key=visits&by=5"

6. Work with a hash:

Terminal window
curl -X POST "http://localhost:8080/redisdemo/hash?key=user:1&field=name&value=Winter%20Boot"
curl "http://localhost:8080/redisdemo/hash?key=user:1"

7. Run the complete demo:

Terminal window
curl "http://localhost:8080/redisdemo/demo"

The demo writes string, counter, hash, list, and set keys under a time-based prefix, verifies each read, sets a TTL, cleans up, and returns one status entry per operation.

  • Read the Redis module for cluster, array, sentinel, and Dynomite templates.
  • Browse all Libraries when you need Kafka, SQS, S3, or Doctrine in the same app.