Reading list Switch to dark mode

    How to Use Redis for API Caching in CS-Cart?

    Updated 10 August 2023

    Using Redis for API caching in CS-Cart can significantly improve the performance and response time of your application by reducing the load on your database and speeding up data retrieval.

    Redis (Remote Dictionary Server) is an in-memory data store that allows you to cache frequently accessed data and retrieve it quickly without hitting the database every time.

    Here’s a step-by-step guide on how to implement API caching with Redis in CS-Cart:

    Step 1: Install Redis

    Start by installing Redis on your server or using a managed Redis service. Follow the instructions below to install Redis:

    1. For Linux users, run the following commands:
    sudo apt update
    sudo apt install redis-server

    2. For macOS users, use Homebrew:

    Start your headless eCommerce
    Find out More
    brew install redis

    Step 2: Configure Redis in CS-Cart

    Once Redis is installed, you need to configure CS-Cart to use Redis for caching. To do this,

    $config['cache_backend'] = 'redis';
    $config['cache_redis_server'] = 'localhost'; // Change this to your Redis server address
    $config['cache_redis_port'] = 6379; // Change this to your Redis server port
    $config['cache_redis_database'] = 0; // Change this to your desired database number (0 by default)

    Step 3: Enable Caching for API Responses

    In your CS-Cart API implementation, you can now enable caching for API responses that are suitable for caching. You can choose which API responds to the cache based on their frequency of access and how often the data changes.

    Besides that, the Redis API Caching can also be useful for improving the performance of your CS Cart Mobile App.

    Now let’s go ahead and enable caching for API responses in your CS-Cart API implementation.

    We will use predis package, and download it via Composer or GitHub.


    Next, you may create a helper class that will be helpful to execute Redis operations.

    namespace Tygh\YourAddonName;
    use Predis\Client;
    class Helper
        protected $redis;
        public function __construct()
            $config = Registry::get("config");
            $this->redis = new Client([
                'scheme' => 'tcp',
                'host'   => $config['cache_redis_server'], // Adjust based on your Redis server 
                'port'   => $config['cache_redis_port'], // Adjust based on your Redis server 
        public function get($key)
            $data = $this->redis->get($key);
            return $data !== null ? unserialize($data) : false;
        public function set($key, $value, $expiration = 3600) 
            $this->redis->setex($key, $expiration, serialize($value));

    This helper class will create a Redis client instance. The get() and set() methods are used for retrieving and storing data.

    Let’s just now implement Redis caching into product API.

    namespace Tygh\Api\Entities;
    use Tygh\Api\AEntity;
    use Tygh\Api\Response;
    use Tygh\Enum\YesNo;
    use Tygh\Registry;
    use Tygh\Addons\Helper;
    class Products extends AEntity
        public function index()
            $redisCache = new Helper();
            // Check if data is cached in Redis
            $cacheKey = 'product_list';
            $cachedData = $redisCache->get($cacheKey);
            if ($cachedData !== false) {
                return array(
                    'status' => Response::STATUS_OK,
                    'data'   => $cachedData
            $lang_code = $this->getLanguageCode($params);
            // Set default values to input params
            $default_params = [
                'match'            => 'all',
                'subcats'          => YesNo::YES,
                'pcode_from_q'     => YesNo::YES,
                'pshort'           => YesNo::YES,
                'pfull'            => YesNo::YES,
                'pname'            => YesNo::YES,
                'pkeywords'        => YesNo::YES,
                'search_performed' => YesNo::YES,
            $params = array_merge($default_params, $params);
            $items_per_page = $this->safeGet($params, 'items_per_page', Registry::get('settings.Appearance.admin_elements_per_page'));
            list($products, $search) = fn_get_products($params, $items_per_page, $lang_code);
            $data = array(
                'products' => array_values($products),
                'params'   => $search,
            // Cache the product data
            $redisCache->set($cacheKey, $data, 3600);
            return array(
                'status' =>  Response::STATUS_OK,
                'data'   => $data
        // ... (other methods)

    In the above code, we have modified Products API entity in CS-Cart which retrieves and caches product list, utilizing Redis caching for improved performance.

    The retrieved product information is cached in Redis with a specified time-to-live (TTL) of 3600 seconds (1 hour) for subsequent faster access.

    Response Time Before using Redis


    Response Time After using Redis


    After Redis Response: The image illustrates the API response time with Redis caching implemented, showcasing significantly reduced response times, as cached data is swiftly retrieved, enhancing overall performance and user experience.

    Note: Before implementing any caching mechanism, make sure to carefully analyze your application’s requirements and choose which data to cache to avoid serving stale or outdated content to users.

    We hope this article has provided you with valuable insights into using Redis for API caching. Start implementing Redis caching in your applications and enjoy the benefits of enhanced performance and scalability.

    So, that was much about how to use Redis for Api caching in CS-Cart. If you need custom CS-Cart Development services then feel free to reach us and also explore our exclusive range of CS-Cart Addons.
    !!Have a Great Day Ahead!!

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*

    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home