To get, set and delete data from Redis in Symfony, you can use the predis/predis
package, which provides a PHP client library for Redis. Here's a step-by-step guide on how to use Redis in Symfony:
1. Install the predis/predis package:
Open a terminal in your Symfony project directory and run the following command to install the predis/predis
package via Composer:
composer require predis/predis
2. Configure Redis connection parameters:
In your Symfony project, open the config/packages/redis.yaml
file (create it if it doesn't exist) and add the Redis connection parameters:
parameters:
redis_host: 127.0.0.1
redis_port: 6379
You can adjust the redis_host
and redis_port
values to match your Redis server configuration.
3. Create a Redis service:
Next, you can create a Redis service to interact with the Redis server. Open the src/Service/RedisService.php
file (create the directory and file if they don't exist) and add the following code:
<?php
namespace App\Service;
use Predis\Client;
class RedisService
{
private $client;
public function __construct(string $redisHost, int $redisPort)
{
$this->client = new Client([
'scheme' => 'tcp',
'host' => $redisHost,
'port' => $redisPort,
]);
}
public function set(string $key, $value): void
{
$this->client->set($key, $value);
}
public function get(string $key)
{
return $this->client->get($key);
}
public function delete(string $key): void
{
$this->client->del($key);
}
}
4. Inject the Redis service into your controller:
Now, you can use the RedisService
in your controller to set and get data from Redis. Open your desired controller, for example, src/Controller/DefaultController.php
, and inject the RedisService
:
<?php
namespace App\Controller;
use App\Service\RedisService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
{
/**
* @Route("/set-redis-data", name="set_redis_data")
*/
public function setRedisData(RedisService $redisService): Response
{
// Set data in Redis
$redisService->set('my_key', 'Hello, Redis in Symfony!');
return new Response('Data set in Redis.');
}
/**
* @Route("/get-redis-data", name="get_redis_data")
*/
public function getRedisData(RedisService $redisService): Response
{
// Get data from Redis
$data = $redisService->get('my_key');
return new Response('Data from Redis: ' . $data);
}
/**
* @Route("/delete-redis-data", name="delete_redis_data")
*/
public function deleteRedisData(RedisService $redisService): Response
{
// Delete data from Redis
$redisService->delete('my_key');
return new Response('Data deleted from Redis.');
}
}
5. Test the endpoints:
After implementing the controller methods, you can now test the endpoints. Open your browser or use a tool like curl
to access the following URLs:
- -
http://localhost/set-redis-data
: This will set the data "Hello, Redis in Symfony!" in Redis. - -
http://localhost/get-redis-data
: This will retrieve the data from Redis and display it on the page. - -
http://localhost/delete-redis-data
: This will delete the data associated with the keymy_key
from Redis.
That's it! You have now successfully set up and used Redis in Symfony using the predis/predis
package.