Predis (opens new window) integration into Nette/DI (opens new window)

# Content

# Setup

composer require contributte/redis
1
extensions:
	redis: Contributte\Redis\DI\RedisExtension # For Nette 3+
	redis: Contributte\Redis\DI\RedisExtension24 # For Nette 2.4
1
2
3

# Configuration

redis:
	# Setup Tracy panel
	debug: %debugMode%

	# Default client factory
	clientFactory: Predis\Client

	connection:
		default:
			uri: tcp://127.0.0.1:6379

			# Options passed directly to Predis\Client
			# https://github.com/nrk/predis#client-configuration
			options: []
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# Sessions

Setup Nette\Http\Session to store session with Redis

redis:
	connection:
		default:
			sessions: true

			## you can also configure session
			sessions:
				ttl: 3600 # time in seconds after which is session invalidated
1
2
3
4
5
6
7
8

# Cache

Replaces Nette\Caching\IStorage in DIC with RedisStorage

redis:
	connection:
		default:
			storage: true
1
2
3
4

# Custom serializer

To use custom serializer for storage you need to implement Contributte/Redis/Serializer/Serializer interface.

After that, register serializer as a service and place it under redis.serializer key.

redis:
	serializer: @customSerializer
	connection:
		default:
			storage: true

services:
	customSerializer: App\Serializers\YourSerializer
1
2
3
4
5
6
7
8

This package provides several serializers:

  • DefaultSerializer
  • IgbinarySerializer
  • SnappySerializer

# ENV variables

In the need of passing down ENV variables to the configuration you can use nette/configurator and dynamic parameters (opens new window).

$configurator->addDynamicParameters([
	'env' => getenv(),
]);
1
2
3
redis:
	connection:
		default:
			uri: %env.REDIS_HOST%
1
2
3
4

# Sessions and cache

When using sessions and cache make sure you use 2 different databases. One for cache and one for sessions. In case you will use only 1 database for both you will loose sessions when clearing cache. This would be preferred config:

connection:
	default:
		uri: tcp://127.0.0.1:6379
		sessions: false
		storage: true
		options: ['parameters': ['database': 0]]
	session:
		uri: tcp://127.0.0.1:6379
		sessions: true
		storage: false
		options: ['parameters': ['database': 1]]
1
2
3
4
5
6
7
8
9
10
11