### Install PHP Dependencies and Start Redis Fleet Source: https://github.com/snc/sncredisbundle/blob/master/README.md Once in the Nix development environment, update PHP dependencies using Composer and start the Redis fleet with 'overmind'. This prepares the environment for running tests. ```sh composer update # install php package dependencies overmind start & # start redis fleet ``` -------------------------------- ### Install Predis Client Source: https://github.com/snc/sncredisbundle/blob/master/docs/README.md If you intend to use the Predis client library, install it separately using Composer. ```bash $ composer require predis/predis ``` -------------------------------- ### Install SncRedisBundle via Composer Source: https://context7.com/snc/sncredisbundle/llms.txt Install the bundle using Composer. Optionally include the Predis library if the PhpRedis extension is not available. The 'proxy-manager-lts' package is required for phpredis clients when logging is enabled. ```bash composer require snc/redis-bundle # Optional: Predis driver composer require predis/predis # Required only when logging is enabled for phpredis clients: composer require friendsofphp/proxy-manager-lts ``` -------------------------------- ### Complete SncRedisBundle Configuration Example Source: https://github.com/snc/sncredisbundle/blob/master/docs/README.md A comprehensive example of SncRedisBundle configuration, defining multiple Redis clients with various settings including DSN, logging, and cluster options. Also shows Monolog integration. ```yaml snc_redis: clients: default: type: predis alias: default dsn: redis://localhost logging: '%kernel.debug%' cache: type: predis alias: cache dsn: redis://localhost/1 logging: false cluster: type: predis alias: cluster dsn: - redis://127.0.0.1/1 - redis://127.0.0.2/2 - redis://pw@/var/run/redis/redis-1.sock/10 - redis://pw@127.0.0.1:63790/10 options: prefix: foo connection_timeout: 10 connection_persistent: true read_write_timeout: 30 scan: !php/const REDIS::SCAN_PREFIX # OPT_SCAN option, phpredis only iterable_multibulk: false throw_errors: true cluster: predis parameters: # Here you can specify additional context data, see connect/pconnect documentation here # https://github.com/phpredis/phpredis#connect-open # Stream configuration options can be found here https://www.php.net/manual/en/context.ssl.php ssl_context: {'verify_peer': false, 'allow_self_signed': true, 'verify_peer_name': false} monolog: client: cache key: monolog ``` -------------------------------- ### Set up Development Environment with Nix Source: https://github.com/snc/sncredisbundle/blob/master/README.md After installing Nix, run 'nix shell' in the bundle's directory to enter the development environment. This ensures all necessary tools and dependencies are available. ```sh nix shell ``` -------------------------------- ### Configure Complex Predis Setup Source: https://github.com/snc/sncredisbundle/blob/master/docs/README.md Set up multiple Predis clients with different configurations, including connection options and clustered DSNs. ```yaml snc_redis: clients: default: type: predis alias: default dsn: redis://localhost logging: '%kernel.debug%' cache: type: predis alias: cache dsn: redis://secret@localhost/1 options: connection_timeout: 10 read_write_timeout: 30 cluster: type: predis alias: cluster dsn: - redis://localhost/3?weight=10 - redis://localhost/4?weight=5 - redis://localhost/5?weight=1 ``` -------------------------------- ### Install SncRedisBundle Source: https://github.com/snc/sncredisbundle/blob/master/docs/README.md Add the SncRedisBundle package to your project using Composer. ```bash $ composer require snc/redis-bundle ``` -------------------------------- ### Install Symfony Proxy Manager Bridge Source: https://github.com/snc/sncredisbundle/blob/master/docs/README.md Install the symfony/proxy-manager-bridge package to enable lazy-loading of services. This can help prevent unwanted connection calls during cache warmup, especially if a Redis server is unavailable. ```bash $ composer require symfony/proxy-manager-bridge ``` -------------------------------- ### Redis DSN Format Examples Source: https://context7.com/snc/sncredisbundle/llms.txt Illustrates various DSN formats for connecting to Redis, including standard, TLS, and Unix socket connections. Query parameters can specify weight, alias, role, and prefix. ```text redis://[username:password@]host[:port][/database][?weight=N&alias=name&role=master&prefix=foo] rediss://host[:port][/database] # TLS redis:///var/run/redis/redis.sock[/db] # Unix socket # Examples: redis://localhost redis://secret@localhost/2 rediss://user:p%40ss@redis.example.com:6380/0 redis:///var/run/redis/redis.sock/3 redis://localhost?alias=node1&weight=10 ``` -------------------------------- ### Basic Predis Client Configuration Source: https://context7.com/snc/sncredisbundle/llms.txt Configure Redis clients using the Predis driver. This setup supports options like 'cluster', 'replication', and 'connection_persistent'. Persistent connections can be managed using a string identifier. ```yaml snc_redis: clients: default: type: predis alias: default dsn: redis://localhost logging: '%kernel.debug%' session: type: predis alias: session dsn: redis://localhost/1 options: connection_persistent: "session_conn" # string → sets conn_uid connection_timeout: 10 throw_errors: true ``` -------------------------------- ### Install SncRedisBundle with Composer Source: https://github.com/snc/sncredisbundle/blob/master/README.md Use Composer to add the SncRedisBundle to your Symfony project dependencies. This command fetches the latest stable version of the bundle. ```sh composer require snc/redis-bundle ``` -------------------------------- ### ACL Authentication (Redis 6+) Source: https://context7.com/snc/sncredisbundle/llms.txt Configure ACL authentication by providing username and password in the `parameters` or directly in the DSN. Supports both single-node and cluster setups. ```yaml snc_redis: clients: default: type: phpredis alias: default dsn: redis://localhost options: parameters: username: "myuser" password: "mypassword" ``` ```yaml dsn: redis://myuser:myp%40ssword@localhost/0 ``` -------------------------------- ### Run PHPUnit Tests Source: https://github.com/snc/sncredisbundle/blob/master/README.md Execute the test suite using PHPUnit within the Nix development environment. This command verifies the bundle's functionality against the Redis fleet. ```sh php vendor/bin/phpunit # run tests, or anything else you want with php binary ``` -------------------------------- ### Configure PhpRedis Cluster Source: https://github.com/snc/sncredisbundle/blob/master/docs/README.md Set up a Redis cluster using the PhpRedis client by providing an array of DSNs and setting the 'cluster' option to true. ```yaml snc_redis: clients: default: type: phpredis alias: default dsn: - redis://localhost:7000 - redis://localhost:7001 - redis://localhost:7002 options: cluster: true ``` -------------------------------- ### TLS / SSL Configuration Source: https://context7.com/snc/sncredisbundle/llms.txt Configure TLS/SSL connections by using the `rediss://` scheme in the DSN and passing stream context options via `parameters.ssl_context`. ```yaml snc_redis: clients: secure: type: phpredis alias: secure dsn: rediss://redis.example.com:6380 options: parameters: ssl_context: verify_peer: true verify_peer_name: true cafile: '/etc/ssl/certs/ca-certificates.crt' ``` -------------------------------- ### Execute Redis Commands via Console Source: https://context7.com/snc/sncredisbundle/llms.txt Use the `redis:query` console command to interactively run arbitrary Redis commands against configured clients. Output is formatted using Symfony's VarDumper. ```bash # Syntax: php bin/console redis:query [--client=] [args...] # Flush current database on the default client php bin/console redis:query flushdb # Flush all databases on the 'cache' client php bin/console redis:query --client=cache flushall # List all keys matching a pattern php bin/console redis:query keys "*" # Delete a specific key php bin/console redis:query del my_key # Retrieve a value php bin/console redis:query get my_key # Set a value with TTL php bin/console redis:query set my_key "hello" EX 300 # Inspect a hash php bin/console redis:query hgetall my_hash_key ``` -------------------------------- ### RedisDsn Class Usage Source: https://context7.com/snc/sncredisbundle/llms.txt Demonstrates how to use the RedisDsn class to parse connection strings and extract connection details such as host, port, database, credentials, TLS status, and alias. It also shows how to retrieve the persistent connection ID. ```php use Snc\RedisBundle\DependencyInjection\Configuration\RedisDsn; $dsn = new RedisDsn('rediss://appuser:s3cr3t@redis.prod:6380/2?alias=primary'); $dsn->isValid(); // true $dsn->getTls(); // true $dsn->getHost(); // 'redis.prod' $dsn->getPort(); // 6380 $dsn->getDatabase(); // 2 $dsn->getUsername(); // 'appuser' $dsn->getPassword(); // 's3cr3t' $dsn->getAlias(); // 'primary' $dsn->getPersistentId(); // md5 of the DSN string $socket = new RedisDsn('redis:///var/run/redis.sock/0'); $socket->getSocket(); // '/var/run/redis.sock' $socket->getPort(); // 0 ``` -------------------------------- ### Basic PhpRedis Client Configuration Source: https://context7.com/snc/sncredisbundle/llms.txt Configure one or more named Redis clients using the phpredis driver. Each client will be available as a service named 'snc_redis.'. Logging is enabled when 'kernel.debug' is true. Connection options like timeout and serialization can be specified. ```yaml snc_redis: clients: default: type: phpredis alias: default dsn: redis://localhost logging: '%kernel.debug%' cache: type: phpredis alias: cache dsn: redis://localhost/1 options: connection_timeout: 5 read_write_timeout: 30 serialization: php # none | php | igbinary | msgpack | json prefix: "myapp:" ``` -------------------------------- ### Sentinel (High Availability) Configuration Source: https://context7.com/snc/sncredisbundle/llms.txt Configure Sentinel for high availability by listing Sentinel DSNs. The bundle discovers the master transparently after container boot. ```yaml snc_redis: clients: default: type: phpredis # or predis, relay alias: default dsn: - redis://sentinel1.host:26379 - redis://sentinel2.host:26379 - redis://sentinel3.host:26379 options: replication: sentinel service: mymaster # sentinel master name connection_timeout: 2 read_write_timeout: 5 parameters: database: 0 password: "redis-master-password" sentinel_username: "sentinel-user" # optional ACL sentinel_password: "sentinel-pass" # optional ACL ``` -------------------------------- ### Accessing Redis Master in Sentinel Mode Source: https://context7.com/snc/sncredisbundle/llms.txt After the container boots, the Redis master is resolved transparently. You can then use the client as usual. ```php // After container boot the master is resolved transparently /** @var Redis $redis */ $redis = $container->get('snc_redis.default'); $redis->set('ha_key', 'value'); echo $redis->get('ha_key'); // 'value' – served by detected master ``` -------------------------------- ### Enable Redis Client Logging for Web Profiler Source: https://github.com/snc/sncredisbundle/blob/master/docs/README.md Configure a Redis client with logging enabled to inspect commands sent by the client using Symfony's web profiler bundle. Ensure logging is set to '%kernel.debug%' or true. ```yaml snc_redis: clients: default: type: predis alias: default dsn: redis://localhost/ logging: '%kernel.debug%' ``` -------------------------------- ### Configure Sentinel Replication Source: https://github.com/snc/sncredisbundle/blob/master/docs/README.md Configure Redis clients (Predis, PhpRedis, or Relay) for sentinel replication. Specify sentinel DSNs, replication service name, and optional parameters for master/slave connections. ```yaml snc_redis: clients: default: type: "phpredis" # or "predis", or "relay" alias: default dsn: - redis://localhost:26379 - redis://otherhost:26379 options: replication: sentinel service: mymaster parameters: database: 1 password: pass sentinel_username: myuser # default to null sentinel_password: mypass # default to null ``` -------------------------------- ### Configure Monolog Logging to Redis Source: https://github.com/snc/sncredisbundle/blob/master/docs/README.md Set up Monolog to store logs in a Redis LIST. Configure the Redis client and the Monolog handler in your application's configuration. ```yaml snc_redis: clients: monolog: type: predis alias: monolog dsn: redis://localhost/1 logging: false options: connection_persistent: true monolog: client: monolog key: monolog monolog: handlers: main: type: service id: snc_redis.monolog.handler level: debug ``` -------------------------------- ### Configure Default Predis Client Source: https://github.com/snc/sncredisbundle/blob/master/docs/README.md Configure a default Redis client using the Predis type in your `config.yml`. Ensure passwords with special characters in the DSN are URL-encoded. ```yaml snc_redis: clients: default: type: predis alias: default dsn: redis://localhost ``` -------------------------------- ### Persistent Connections Configuration Source: https://context7.com/snc/sncredisbundle/llms.txt Configure persistent connections using `connection_persistent`. It accepts `true` (uses alias as ID), `false` (default), or a custom string for the identifier. ```yaml snc_redis: clients: app_cache: type: phpredis alias: app_cache dsn: redis://localhost/0 options: connection_persistent: "app_cache_conn" # unique persistent ID session_store: type: phpredis alias: session_store dsn: redis://localhost/1 options: connection_persistent: true # uses alias "session_store" as ID tmp: type: phpredis alias: tmp dsn: redis://localhost/2 options: connection_persistent: false # new connection each request (default) ``` -------------------------------- ### Configure Custom Class Overrides Source: https://context7.com/snc/sncredisbundle/llms.txt Customize internal SncRedisBundle classes by specifying their fully qualified class names in the `snc_redis.class` configuration block. ```yaml snc_redis: class: client: Predis\Client client_options: Predis\Configuration\Options connection_parameters: Predis\Connection\Parameters connection_factory: Snc\RedisBundle\Client\Predis\Connection\ConnectionFactory connection_wrapper: Snc\RedisBundle\Client\Predis\Connection\ConnectionWrapper phpredis_client: Redis relay_client: Relay\Relay phpredis_clusterclient: RedisCluster logger: Snc\RedisBundle\Logger\RedisLogger data_collector: Snc\RedisBundle\DataCollector\RedisDataCollector monolog_handler: Monolog\Handler\RedisHandler ``` -------------------------------- ### Configure Redis Authentication with ACL Source: https://github.com/snc/sncredisbundle/blob/master/docs/README.md Set up an authenticated Redis connection using username and password with the phpredis driver. Ensure your Redis server version is 6.0 or higher for ACL support. ```yaml snc_redis: clients: default: type: phpredis alias: default dsn: redis://localhost # dsn: redis://my_username:my_password@localhost <- username and password can be also set here options: parameters: username: my_userame password: my_password ``` -------------------------------- ### Redis Cluster Configuration (Predis) Source: https://context7.com/snc/sncredisbundle/llms.txt Configure Predis for cluster mode by setting `options.cluster: predis` and providing DSNs with optional weights. ```yaml snc_redis: clients: cluster: type: predis alias: cluster dsn: - redis://localhost/1?weight=10 - redis://localhost/2?weight=5 - redis://localhost/3?weight=1 options: cluster: predis ``` -------------------------------- ### Configure Symfony Cache Pools with Redis Source: https://github.com/snc/sncredisbundle/blob/master/docs/README.md Use Redis client service names as cache pool providers in Symfony's framework configuration. Ensure the client is defined in the snc_redis configuration. ```yaml framework: cache: app: cache.adapter.redis # app cache from client config as default adapter/provider default_redis_provider: snc_redis.default pools: some-pool.cache: adapter: cache.adapter.redis # a specific provider, e.g. if you have a snc_redis.clients.cache provider: snc_redis.cache ``` -------------------------------- ### Configure Predis Master-Slave Replication Source: https://github.com/snc/sncredisbundle/blob/master/docs/README.md Configure Predis for master-slave replication by providing multiple DSNs and specifying the replication type. The master DSN must be tagged with the 'master' role. ```yaml snc_redis: clients: default: type: predis alias: default dsn: - redis://master-host?role=master - redis://slave-host1 - redis://slave-host2 options: replication: predis ``` -------------------------------- ### Configure Monolog with Custom Formatter for Redis Source: https://github.com/snc/sncredisbundle/blob/master/docs/README.md Extend Monolog logging to Redis by adding a custom formatter. This allows for more control over how log records are structured before being stored. ```yaml snc_redis: clients: monolog: type: predis alias: monolog dsn: redis://localhost/1 logging: false options: connection_persistent: true monolog: client: monolog key: monolog formatter: my_custom_formatter ``` -------------------------------- ### Configure Redis Sessions with Symfony Source: https://github.com/snc/sncredisbundle/blob/master/docs/README.md Integrate Redis as a session handler for Symfony applications. Define your Redis client and reference it in the framework session configuration. Note that this solution does not perform session locking. ```yaml snc_redis: clients: session: type: predis alias: session dsn: redis://localhost/1 ``` ```yaml framework: ... session: handler_id: Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler services: Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler: arguments: ['@snc_redis.session'] ``` -------------------------------- ### Redis Data Collector Methods Source: https://context7.com/snc/sncredisbundle/llms.txt The `RedisDataCollector` integrates with Symfony's Web Profiler and provides methods to retrieve Redis performance metrics. ```php // Available on the 'redis' profiler panel: $collector->getCommandCount(); // int $collector->getErroredCommandsCount(); // int $collector->getTime(); // float (total ms) $collector->getCommands(); // array of logged commands ``` -------------------------------- ### Configure Monolog Handler for Redis Source: https://context7.com/snc/sncredisbundle/llms.txt Configure the Monolog handler to store application logs in a Redis LIST. Ensure the referenced Redis client has logging disabled. ```yaml snc_redis: clients: monolog: type: predis alias: monolog dsn: redis://localhost/1 logging: false options: connection_persistent: true monolog: client: monolog key: monolog # Redis LIST key formatter: my_formatter # optional custom formatter service id monolog: handlers: redis: type: service id: snc_redis.monolog.handler level: debug ``` -------------------------------- ### Access Redis Profiler Data Programmatically Source: https://context7.com/snc/sncredisbundle/llms.txt Access Redis profiler data programmatically using the injected `RedisLogger` service. This allows retrieving command counts, execution times, and command details. ```php // Accessing profiler data programmatically use Snc\RedisBundle\Logger\RedisLogger; class MyController { public function __construct(private RedisLogger $redisLogger) {} public function debugAction(): Response { // ... perform Redis operations ... $total = $this->redisLogger->getNbCommands(); // int: total commands executed since last reset $commands = $this->redisLogger->getCommands(); // list foreach ($commands as $entry) { echo $entry['cmd']; // e.g. "SET foo bar" echo $entry['executionMS']; // e.g. 0.342 echo $entry['conn']; // client alias, e.g. "default" echo $entry['error']; // false or error message string } $this->redisLogger->reset(); // clear recorded commands } } ``` -------------------------------- ### Enable Debug Logging for Redis Calls Source: https://context7.com/snc/sncredisbundle/llms.txt Enable debug logging by setting `logging: true` (defaults to `kernel.debug`). Redis calls are intercepted and logged for the Symfony Web Profiler. ```yaml snc_redis: clients: default: type: phpredis alias: default dsn: redis://localhost logging: '%kernel.debug%' # enable in debug mode ``` -------------------------------- ### Configure Persistent Connections for Predis/PhpRedis Source: https://github.com/snc/sncredisbundle/blob/master/docs/README.md Manage persistent Redis connections by specifying a unique identifier. This prevents clients from sharing the same socket, crucial for multiple clients communicating with the same Redis server. ```yaml snc_redis: clients: app_cache: type: predis # or phpredis alias: app_cache dsn: redis://localhost/0 options: connection_persistent: "app_cache_connection" # Custom persistent ID session_store: type: predis # or phpredis alias: session_store dsn: redis://localhost/1 options: connection_persistent: true # Uses alias as persistent ID simple_cache: type: phpredis alias: simple_cache dsn: redis://localhost/2 options: connection_persistent: false # No persistent connection ``` -------------------------------- ### Register SncRedisBundle in Symfony Source: https://context7.com/snc/sncredisbundle/llms.txt Register the bundle in your Symfony application's kernel to enable its compiler passes and DI extension. ```php // config/bundles.php return [ // ... Snc\RedisBundle\SncRedisBundle::class => ['all' => true], ]; ``` -------------------------------- ### Register SncRedisBundle in Kernel Source: https://github.com/snc/sncredisbundle/blob/master/docs/README.md Register the SncRedisBundle within your Symfony application's kernel to enable its functionality. ```php snc_redis_default->set($key, $value, 3600); $stored = $this->snc_redis_default->get($key); // $stored === $value } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.