Просмотр исходного кода

Use redis to cache requested URLs

poeti8 7 лет назад
Родитель
Сommit
9b23aa814a
1 измененных файлов с 22 добавлено и 8 удалено
  1. 22 8
      server/controllers/urlController.js

+ 22 - 8
server/controllers/urlController.js

@@ -23,12 +23,12 @@ const {
   getBannedDomain,
   getBannedHost,
 } = require('../db/url');
-
-const dnsLookup = promisify(dns.lookup);
-
+const redis = require('../redis');
 const { addProtocol, generateShortUrl } = require('../utils');
 const config = require('../config');
 
+const dnsLookup = promisify(dns.lookup);
+
 const generateId = async () => {
   const id = generate('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', 6);
   const urls = await findUrl({ id });
@@ -122,14 +122,25 @@ exports.goToUrl = async (req, res, next) => {
   const referrer = req.header('Referer') && URL.parse(req.header('Referer')).hostname;
   const location = geoip.lookup(req.realIp);
   const country = location && location.country;
-  const urls = await findUrl({ id, domain });
   const isBot =
     botList.some(bot => agent.source.toLowerCase().includes(bot)) || agent.family === 'Other';
-  if (!urls && !urls.length) return next();
-  const url = urls.find(item => (domain ? item.domain === domain : !item.domain));
+
+  let url;
+
+  const cachedUrl = await redis.get(id + domain || '');
+
+  if (cachedUrl) {
+    url = JSON.parse(cachedUrl);
+  } else {
+    const urls = await findUrl({ id, domain });
+    if (!urls && !urls.length) return next();
+    url = urls.find(item => (domain ? item.domain === domain : !item.domain));
+  }
 
   if (!url) return next();
 
+  redis.set(id + domain || '', JSON.stringify(url), 'EX', 60 * 60 * 1);
+
   if (url.banned) {
     return res.redirect('/banned');
   }
@@ -152,7 +163,7 @@ exports.goToUrl = async (req, res, next) => {
       return res.status(401).json({ error: 'Password is not correct' });
     }
     if (url.user && !isBot) {
-      await createVisit({
+      createVisit({
         browser,
         country: country || 'Unknown',
         domain,
@@ -164,7 +175,7 @@ exports.goToUrl = async (req, res, next) => {
     return res.status(200).json({ target: url.target });
   }
   if (url.user && !isBot) {
-    await createVisit({
+    createVisit({
       browser,
       country: country || 'Unknown',
       domain,
@@ -213,6 +224,7 @@ exports.deleteUrl = async ({ body: { id, domain }, user }, res) => {
   const customDomain = domain !== config.DEFAULT_DOMAIN && domain;
   const urls = await findUrl({ id, domain: customDomain });
   if (!urls && !urls.length) return res.status(400).json({ error: "Couldn't find the short URL." });
+  redis.del(id + customDomain || '');
   const response = await deleteUrl({ id, domain: customDomain, user });
   if (response) return res.status(200).json({ message: 'Sort URL deleted successfully' });
   return res.status(400).json({ error: "Couldn't delete short URL." });
@@ -236,6 +248,8 @@ exports.ban = async ({ body }, res) => {
 
   if (url.banned) return res.status(200).json({ message: 'URL was banned already' });
 
+  redis.del(body.id);
+
   const domain = URL.parse(url.target).hostname;
 
   let host;