Skip to content

Commit

Permalink
api: stream: add cache to /stream/playback (#2197)
Browse files Browse the repository at this point in the history
* api: stream: add cache to /stream/playback

* fix old typos
  • Loading branch information
gioelecerati authored Jun 4, 2024
1 parent 604775f commit 61613fb
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions packages/api/src/controllers/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
} from "./helpers";
import { toExternalSession } from "./session";
import wowzaHydrate from "./wowza-hydrate";
import { cache } from "../store/cache";

type Profile = DBStream["profiles"][number];
type MultistreamOptions = DBStream["multistream"];
Expand Down Expand Up @@ -839,14 +840,23 @@ app.get("/:id", authorizer({}), async (req, res) => {
res.json(db.stream.addDefaultFields(stream));
});

// returns stream by steamKey
// returns stream by playbackId
app.get("/playback/:playbackId", authorizer({}), async (req, res) => {
const {
data: [stream],
} = await req.store.queryObjects<DBStream>({
kind: "stream",
query: { playbackId: req.params.playbackId },
});
const stream = await cache.getOrSet(
`strm-stream-by-playback-${req.params.playbackId}`,
async () => {
const {
data: [stream],
} = await req.store.queryObjects<DBStream>({
kind: "stream",
query: { playbackId: req.params.playbackId },
});

return stream;
},
5
);

if (
!stream ||
((stream.userId !== req.user.id || stream.deleted) && !req.user.admin)
Expand All @@ -862,7 +872,7 @@ app.get("/playback/:playbackId", authorizer({}), async (req, res) => {
);
});

// returns stream by steamKey
// returns stream by streamKey
app.get("/key/:streamKey", authorizer({}), async (req, res) => {
const useReplica = req.query.main !== "true";
const [docs] = await db.stream.find(
Expand Down

0 comments on commit 61613fb

Please sign in to comment.