Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

692 B
Raw Blame
import { LRUCache } from "lru-cache";
import { conn } from "./connection";
import { EdgeDomainProps } from "./types";

const domainLRUCache = new LRUCache<string, EdgeDomainProps>({
  max: 1000,
  ttl: 5 * 60 * 1000, // 5 minutes
});

export const getDomainViaEdge = async (domain: string) => {
  const cached = domainLRUCache.get(domain);
  if (cached) {
    return cached;
  }

  const { rows } =
    (await conn.execute<EdgeDomainProps>(
      "SELECT * FROM Domain WHERE slug = ?",
      [domain],
    )) || {};

  const result =
    rows && Array.isArray(rows) && rows.length > 0 ? rows[0] : null;
  if (result !== null) {
    domainLRUCache.set(domain, result);
  }
  return result;
};