Skip to content

Commit

Permalink
fix: implement ttl + intialise cached values
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Oct 3, 2024
1 parent e743386 commit 6273d68
Show file tree
Hide file tree
Showing 5 changed files with 587 additions and 120 deletions.
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,43 @@ Install package:
```sh
# npm
npm install unifont

# pnpm
pnpm install unifont
```

```js
import {} from 'unifont'
import { createUnifont, providers } from 'unifont'

const unifont = await createUnifont([
providers.google(),
])

const fonts = await unifont.resolveFontFace('Poppins')

console.log(fonts)
```

In most environments, you will want to cache the results of font APIs to avoid unnecessary hits to them. By default `unifont` caches font data in memory.

For full control, `unifont` exposes a storage API which is compatible with `unstorage`. It simply needs to expose a `getItem` and `setItem` method.

```ts
import { createUnifont, providers } from 'unifont'

import { createStorage } from 'unstorage'
import fsDriver from 'unstorage/drivers/fs-lite'

const storage = createStorage({
driver: fsDriver({ base: 'node_modules/.cache/unifont' }),
})

const cachedUnifont = await createUnifont([providers.google()], { storage })

console.log(await cachedUnifont.resolveFontFace('Poppins'))

// cached data is stored in `node_modules/.cache/unifont`
```

For more about the storage drivers exposed from `unstorage`, check out https://unstorage.unjs.io.

## 💻 Development

- Clone this repository
Expand Down
15 changes: 15 additions & 0 deletions playground/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @ts-check
// custom caching support

import { createUnifont, providers } from 'unifont'

import { createStorage } from 'unstorage'
import fsDriver from 'unstorage/drivers/fs-lite'

const storage = createStorage({
driver: fsDriver({ base: 'node_modules/.cache/unifont' }),
})

const cachedUnifont = await createUnifont([providers.google()], { storage })

console.log(await cachedUnifont.resolveFontFace('Poppins'))
3 changes: 3 additions & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
},
"dependencies": {
"unifont": "latest"
},
"devDependencies": {
"unstorage": "^1.12.0"
}
}
Loading

0 comments on commit 6273d68

Please sign in to comment.