Using Redis cache with Symfony

History

Redis was created in 2009 by Salvatore Sanfilippo, who still remains a lead developer of the project. Redis is sometimes described as “Memcached on steroids,” which is hardly surprising considering that parts of Redis were built in response to lessons learned from using Memcached. Redis has more features than Memcached and is,more powerful and flexible.

How to configure Redis

Create a blank Symfony project:

Add Redis-Bundle:

After installing you see in your AppKernel class adding the Symfony Bundle Redis Bundle class in the bundle’s array, and create a new config file

How to use

Let’s look at a simple example of creating a RedisStorageManager

Now, let’s imagine that we need to create a function that is looking for something

You may be wondering what this new getRedisKey method is. It is a key that allows us to reduce the possibility of crossing similar keys for different user flows.

Next, we’ll try to save something in Redis. In this case, automatic cleaning of the so-called lifetime can be very helpful

If we consider cases (search, store, delete), then our Redis Storage Manager -> will increase the functionality that will easily allow us to manage data

That’s it! You can use the Redis Store anywhere in your project and manipulate the data easily.

Senior Software Engineer from Kyiv, in @MacPaw company.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store