Blog>
Snippets

Caching Strategies with Redis

Implement caching for frequently retrieved RAG data using Redis to offload read operations from MongoDB and increase the performance.
const express = require('express');
const redis = require('redis');
const mongoose = require('mongoose');
const app = express();

const RAGSchema = new mongoose.Schema({/*...*/});
const RAGModel = mongoose.model('RAG', RAGSchema);

const REDIS_PORT = process.env.REDIS_PORT || 6379;
const client = redis.createClient(REDIS_PORT);

app.get('/rag/:id', (req, res) => {
  const { id } = req.params;

  // Try to fetch the data from Redis first
  client.get(id, async (err, data) => {
    if (err) throw err;

    if (data) {
      return res.json({ source: 'cache', data: JSON.parse(data) });
    } else {
      try {
        // Fetch the data from MongoDB if not found in Redis
        const ragData = await RAGModel.findById(id);
        // Save the data in Redis, set an expiration time (e.g., 3600 seconds)
        client.setex(id, 3600, JSON.stringify(ragData));
        return res.json({ source: 'db', data: ragData });
      } catch (error) {
        return res.status(500).send(error);
      }
    }
  });
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});
A Node.js/Express application example showing how to implement a basic caching layer using Redis for RAG (Red-Amber-Green) data retrieved from MongoDB. When a user requests data by ID, the app first checks if the data is available in the Redis cache. If it's cached, the data is served from Redis; if not, it retrieves data from MongoDB, caches it, and serves it.
const mongoose = require('mongoose');

// Replace with your MongoDB connection string
const MONGO_URI = 'mongodb://localhost:27017/your-database';

mongoose.connect(MONGO_URI, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
})
.then(() => console.log('MongoDB connected...'))
.catch(err => console.log(err));
This piece of code is responsible for establishing a connection to the MongoDB database using Mongoose. Make sure to replace the MONG_URI with your actual MongoDB connection string.
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>RAG Data</title>
</head>
<body>
  <h1>RAG Data Viewer</h1>
  <div id="ragData"></div>
  <script>
    fetch('/rag/:id')
      .then(response => response.json())
      .then(data => {
        const ragDataDiv = document.getElementById('ragData');
        ragDataDiv.innerHTML = JSON.stringify(data, null, 2);
      })
      .catch(error => console.error('Error:', error));
  </script>
</body>
</html>
This HTML document creates a basic page with a script that fetches RAG data from the server using the '/rag/:id' endpoint established in our Node.js/Express application and displays it on the page. Update ':id' with the actual ID of the RAG data.
body {
  font-family: Arial, sans-serif;
}

h1 {
  color: #333;
}

#ragData {
  white-space: pre-wrap;
  background-color: #f7f7f7;
  border: 1px solid #ddd;
  padding: 10px;
}
This CSS style is meant to be included in the HTML document head section to apply basic styling to the body, h1, and ragData div in the webpage.