REDIS CACHING WITH SAMPLE CODE
CACHING WITH REDIS WITH SAMPLE CODE
In today’s applications, caching has become very essential in scaling web applications.
Caching primarily stores a copy of a given resource and serves it when requested.
In this article we will be looking at how to use caching in a shopping cart system.
There are multiple ways of implementing this but we will be exploring its implementation using Redis cache.
Redis has commands for its operations but in order to use them in our application, we use a library. In this case we will be using phpredis
So in our shopping cart entity will have the following attributes
1. Unique Item Id: Unique Identifier of item purchased
2. User Id: Id of user making purchase
With the cart entity and is attributes we will build a simple CRUD for it
Adding Item to cart:
Redis uses keypair values in storing data. To set value in PHP:
Redis::set('key' , 'value');
The key is the identifier and the value is the data we intend storing. In our case he values are are array that are json encoded
In our cart example, this is what we will have:
$values = array(
'itemid'=>7,
'itemname'=>"Milk",
'quantity'=>3,
);
Redis::set('cart_' . $userid."_".$uniqueitemid, json_encode($values) );
With the above code snippet, this will be the structure for storing the data:
Structure of key:
The key is the unique identifier and we will have to set its values to be unique to the user and the item. Therefore we have the following format.
This means we will have all cart data stored in our Redis Cache in this format cart_{userid}_{uniqueitemid}. Where {userid} and {uniqueitemid} are variables.
So there you have it, we just stored data in our Redis Cache
Reading Data in cache:
As we have stored data in our Redis we will obviously need to read it.
To get the value of a specific key we use the following:
Redis::get($key)
If we want to get the value of a single item in a users cart, we do the following:
Recommended by LinkedIn
Redis::get('cart_' . $userid."_".$uniqueitemid);
In this case we just provide the userid and uniqueitemid then we have the values of that record in the cart
To read all cart items of a user we need the user id as the parameter.
We will also need the wildcard (*) after the userid variable in order to get all keys that are under that ID.
$keys = Redis::keys("cart_$userid"."*");
We now have all the keys that have that specific userid in an array.
$keys = Redis::keys("cart_$userid"."*");
$usercart =[];
foreach ($keys as $key){
$usercart[$key] = json_decode(Redis::get($key),true) ;
}
return $usercart;
We now loop through the array and store the value in the $usercart array.
Deleting Items in Cart
To clear a specific key, we use:
Redis::del($key);
To Clear our cart, we do the following:
1. We get all keys of user using wild card
2. Loop through array of keys and delete based on key
$keys = Redis::keys("cart_$userid"."*");
$usercart =[];
foreach ($keys as $key){
Redis::del($key);
}
return $usercart;
Conclusion
So there you have it, a simple example showing the implementation of Redis for caching.
Nice