Skip to content

Winter Data Redis: PhpRedis Client Module

Winter Data Redis connects Winter Boot applications to Redis. It supports single nodes, Redis Cluster, Redis Arrays, Redis Sentinel, and token-based ring topologies such as Netflix Dynomite through the PhpRedis extension.

Install suvera/winter-modules and the redis PHP extension as described in Installation, then enable the module in application.yml:

application.yml
modules:
- module: 'dev\winterframework\data\redis\RedisModule'
enabled: true
configFile: redis-config.yml

The module uses the PhpRedis C extension (Predis is not supported). Pick the template that matches your Redis topology; each entry in redis-config.yml becomes a bean named after its name, and the first entry of each kind is also the default for an unnamed #[Autowired].

Use PhpRedisTemplate when dealing with single Redis node(s).

Configuration for PhpRedisTemplate (redis-config.yml)

Section titled “Configuration for PhpRedisTemplate (redis-config.yml)”
phpredis:
singles:
- name: redisNode01Bean
host: 192.168.1.10
port: 6379
- name: redisNode02Bean
host: 192.168.1.12
port: 6379
#[Autowired]
private PhpRedisTemplate $redis; // This defaults to "redisNode01Bean"
#[Autowired("redisNode01Bean")]
private PhpRedisTemplate $redis1; // same object as above, by name
#[Autowired("redisNode02Bean")]
private PhpRedisTemplate $redis2;

Use PhpRedisClusterTemplate when dealing with Redis Cluster.

Configuration for PhpRedisClusterTemplate (redis-config.yml)

Section titled “Configuration for PhpRedisClusterTemplate (redis-config.yml)”
phpredis:
clusters:
- name: cluster01Bean
clusterName: cluster01
hosts: [ 192.168.1.10:6379, 192.168.1.11:6379, 192.168.1.12:6379]
- name: cluster02Bean
clusterName: cluster02
hosts: [ 192.168.1.14:6379, 192.168.1.15:6379]
#[Autowired]
private PhpRedisClusterTemplate $redis; // This defaults to "cluster01Bean"
#[Autowired("cluster01Bean")]
private PhpRedisClusterTemplate $redis1; // same object as above, by name
#[Autowired("cluster02Bean")]
private PhpRedisClusterTemplate $redis2;

Use PhpRedisArrayTemplate when dealing with Redis Arrays.

Configuration for PhpRedisArrayTemplate (redis-config.yml)

Section titled “Configuration for PhpRedisArrayTemplate (redis-config.yml)”
phpredis:
arrays:
- name: beanUniqueName01
arrayName: arrayName01
hosts: [192.168.1.10:6379, 192.168.1.11:6379, 192.168.1.12:6379]
options:
- consistent: true
auth: mysecretpassword
function: extract_key_part_func
previous: [ ]
retry_timeout: 0
lazy_connect: false
connect_timeout: 0.5
read_timeout: 0.5
algorithm: sha256
distributor: dist_func
#[Autowired]
private PhpRedisArrayTemplate $redis; // This defaults to "beanUniqueName01"
#[Autowired("beanUniqueName01")]
private PhpRedisArrayTemplate $redis1; // same object as above, by name

Use PhpRedisSentinelTemplate when dealing with Redis Sentinel.

Configuration for PhpRedisSentinelTemplate (redis-config.yml)

Section titled “Configuration for PhpRedisSentinelTemplate (redis-config.yml)”
phpredis:
sentinels:
- name: sentinel01
persistence: true
host: 127.0.0.1
port: 6379
timeout: 0
readTimeout: 0
#[Autowired]
private PhpRedisSentinelTemplate $redis; // This defaults to "sentinel01"

Use PhpRedisTokenTemplate when dealing with token-based ring topology Redis clusters such as Netflix Dynomite.

Configuration for PhpRedisTokenTemplate (redis-config.yml)

Section titled “Configuration for PhpRedisTokenTemplate (redis-config.yml)”
phpredis:
tokens:
# 1st Cluster
- name: DynomiteCluster01
hosts:
- host: 10.1.2.3
port: 7801
token: 4294967295
- host: 10.1.2.4
port: 7801
token: 2863311530
- host: 10.1.2.5
port: 7801
token: 1431655765
persistence: false
strictTokenRing: false
timeout: 0
retryInterval:
reserved:
readTimeout: 0
idleTimeout: 300
hashProvider: dev\winterframework\util\hash\MurmurHash3Provider
# 2nd Cluster
- name: DynomiteCluster02
# ...settings here ...
#[Autowired("DynomiteCluster01")]
private PhpRedisTokenTemplate $redis;

Besides the templates, the Redis module provides:

  • RedisCache — a distributed backend for #[Cacheable]. See Caching.
  • RedisSessionStore — store user sessions in Redis. See Sessions.
  • AsyncRedisQueueStore — a persistent queue for #[Async] calls. See Async Tasks.
  • Reliable queue (RedisQueueService) — Redis Streams with consumer workers, redelivery, and dead-letter routing. See the Redis queue example.

RedisLockManager (dev\winterframework\data\redis\lock) makes #[Lockable] exclusive across all pods, with locks kept in Redis. It needs Winter Boot 2.1.6 or later. See Locking for setup and behavior.