Spaces:
Runtime error
Runtime error
Thomas G. Lopes
commited on
Commit
·
73e0644
1
Parent(s):
42052ec
add caching to model avatar
Browse files
src/lib/components/avatar.svelte
CHANGED
|
@@ -16,7 +16,7 @@
|
|
| 16 |
let orgName = $derived(_orgName ?? (!isCustom ? model.id.split("/")[0] : undefined));
|
| 17 |
let avatarUrl = $state<string>();
|
| 18 |
|
| 19 |
-
$effect(() => {
|
| 20 |
avatarUrl = undefined;
|
| 21 |
getAvatarUrl(orgName).then(url => (avatarUrl = url));
|
| 22 |
});
|
|
|
|
| 16 |
let orgName = $derived(_orgName ?? (!isCustom ? model.id.split("/")[0] : undefined));
|
| 17 |
let avatarUrl = $state<string>();
|
| 18 |
|
| 19 |
+
$effect.pre(() => {
|
| 20 |
avatarUrl = undefined;
|
| 21 |
getAvatarUrl(orgName).then(url => (avatarUrl = url));
|
| 22 |
});
|
src/lib/remote/avatar.remote.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
import { query } from "$app/server";
|
|
|
|
| 2 |
import typia from "typia";
|
| 3 |
|
| 4 |
type AvatarJson = {
|
|
@@ -7,9 +8,10 @@ type AvatarJson = {
|
|
| 7 |
|
| 8 |
export const getAvatarUrl = query(
|
| 9 |
typia.createValidate<string | undefined>(),
|
| 10 |
-
async (orgName): Promise<string | undefined> => {
|
| 11 |
if (!orgName) return;
|
| 12 |
const url = `https://huggingface.co/api/organizations/${orgName}/avatar`;
|
|
|
|
| 13 |
const res = await fetch(url);
|
| 14 |
if (!res.ok) {
|
| 15 |
throw new Error(`Error getting avatar url for org: ${orgName}`);
|
|
@@ -19,5 +21,5 @@ export const getAvatarUrl = query(
|
|
| 19 |
typia.assert<AvatarJson>(json);
|
| 20 |
const { avatarUrl } = json;
|
| 21 |
return avatarUrl;
|
| 22 |
-
},
|
| 23 |
);
|
|
|
|
| 1 |
import { query } from "$app/server";
|
| 2 |
+
import { withCache } from "$lib/utils/cache.js";
|
| 3 |
import typia from "typia";
|
| 4 |
|
| 5 |
type AvatarJson = {
|
|
|
|
| 8 |
|
| 9 |
export const getAvatarUrl = query(
|
| 10 |
typia.createValidate<string | undefined>(),
|
| 11 |
+
withCache(async (orgName): Promise<string | undefined> => {
|
| 12 |
if (!orgName) return;
|
| 13 |
const url = `https://huggingface.co/api/organizations/${orgName}/avatar`;
|
| 14 |
+
|
| 15 |
const res = await fetch(url);
|
| 16 |
if (!res.ok) {
|
| 17 |
throw new Error(`Error getting avatar url for org: ${orgName}`);
|
|
|
|
| 21 |
typia.assert<AvatarJson>(json);
|
| 22 |
const { avatarUrl } = json;
|
| 23 |
return avatarUrl;
|
| 24 |
+
}),
|
| 25 |
);
|
src/lib/utils/cache.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
type WithCacheOptions = {
|
| 2 |
+
// Lifespan of the cache in milliseconds
|
| 3 |
+
// @default 10 minutes
|
| 4 |
+
lifespan?: number;
|
| 5 |
+
};
|
| 6 |
+
|
| 7 |
+
export function withCache<Args extends unknown[], Return>(
|
| 8 |
+
fn: (...args: Args) => Return,
|
| 9 |
+
options?: WithCacheOptions,
|
| 10 |
+
): (...args: Args) => Return {
|
| 11 |
+
const { lifespan = 1000 * 60 * 10 } = options ?? {};
|
| 12 |
+
|
| 13 |
+
const cache = new Map<string, Return>();
|
| 14 |
+
|
| 15 |
+
return (...args: Args) => {
|
| 16 |
+
const key = JSON.stringify(args);
|
| 17 |
+
if (cache.has(key)) {
|
| 18 |
+
return cache.get(key) as Return;
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
const value = fn(...args);
|
| 22 |
+
cache.set(key, value);
|
| 23 |
+
setTimeout(() => {
|
| 24 |
+
cache.delete(key);
|
| 25 |
+
}, lifespan);
|
| 26 |
+
|
| 27 |
+
return value;
|
| 28 |
+
};
|
| 29 |
+
}
|