-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7e8113
commit 01d91e5
Showing
9 changed files
with
211 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
tools-nodejs/vighnesh153-astro/website/src/components/ImageWithCache.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { createEffect, createSignal, type JSX, Show } from "solid-js"; | ||
|
||
import { cacheImage } from "@/utils/image_caching.ts"; | ||
|
||
export type ImageWithCacheProps = { | ||
src: string; | ||
cacheKey: string; | ||
ttlMillis?: number; | ||
fallback?: JSX.Element; | ||
|
||
imageProps?: JSX.ImgHTMLAttributes<HTMLImageElement>; | ||
}; | ||
|
||
export function ImageWithCache(props: ImageWithCacheProps): JSX.Element { | ||
const [objUrl, setObjUrl] = createSignal<string | null>(null); | ||
|
||
createEffect(async () => { | ||
try { | ||
const cachedUrl = await cacheImage(props.src, props.cacheKey, { | ||
ttlMillis: props.ttlMillis, | ||
}); | ||
setObjUrl(cachedUrl); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
}); | ||
|
||
return ( | ||
<Show when={objUrl() !== null} fallback={props.fallback}> | ||
<img {...props.imageProps} src={objUrl()!} /> | ||
</Show> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
tools-nodejs/vighnesh153-astro/website/src/utils/image_caching.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { milliseconds } from "@vighnesh153/tools"; | ||
import localforage from "localforage"; | ||
|
||
const defaultCacheTtl = milliseconds({ years: 1 }); | ||
|
||
const imageStore = localforage.createInstance({ | ||
name: "vighnesh153-images", | ||
}); | ||
|
||
const imageTtlStore = localforage.createInstance({ | ||
name: "vighnesh153-images-ttl", | ||
}); | ||
|
||
export type CacheImageOptions = { | ||
ttlMillis?: number; | ||
}; | ||
|
||
export async function cacheImage( | ||
networkUri: string, | ||
cacheKey: string, | ||
{ ttlMillis = defaultCacheTtl }: CacheImageOptions = {}, | ||
): Promise<string> { | ||
return navigator.locks.request(`images-${cacheKey}`, async () => { | ||
const existingImage = await getImageObjUrl(cacheKey); | ||
if (existingImage !== null) { | ||
return existingImage; | ||
} | ||
|
||
return new Promise(async (resolve, reject) => { | ||
const blob = await fetch(networkUri).then((res) => res.blob()); | ||
|
||
const reader = new FileReader(); | ||
reader.onload = () => { | ||
const objectUrl = reader.result as string; | ||
|
||
imageStore.setItem(cacheKey, objectUrl).then(async () => { | ||
resolve(objectUrl); | ||
await imageTtlStore.setItem(cacheKey, Date.now() + ttlMillis); | ||
}); | ||
}; | ||
|
||
try { | ||
reader.readAsDataURL(blob); | ||
} catch (e) { | ||
reject(e); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
async function getImageObjUrl(cacheKey: string): Promise<string | null> { | ||
const ttl = await imageTtlStore.getItem(cacheKey); | ||
if (typeof ttl !== "number") { | ||
// either ttl doesn't exist or is corrupted for this cache key | ||
return null; | ||
} | ||
|
||
if (ttl < Date.now()) { | ||
// Cache expired | ||
return null; | ||
} | ||
|
||
const maybeImageObjUrl = await imageStore.getItem(cacheKey); | ||
if (typeof maybeImageObjUrl === "string") { | ||
return maybeImageObjUrl; | ||
} | ||
|
||
// imageObjUrl is corrupted | ||
return null; | ||
} | ||
|
||
export async function cleanupExpiredImages() { | ||
const imageKeys = await imageStore.keys(); | ||
const ttlKeys = await imageTtlStore.keys(); | ||
|
||
for (const key of new Set([...imageKeys, ...ttlKeys])) { | ||
const objUrl = await imageStore.getItem(key); | ||
const ttl = await imageTtlStore.getItem(key); | ||
if ( | ||
typeof objUrl !== "string" || typeof ttl !== "number" || ttl < Date.now() | ||
) { | ||
await imageStore.removeItem(key); | ||
await imageTtlStore.removeItem(key); | ||
} | ||
} | ||
} |