generated from un-ts/lib-boilerplate
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: first blood, should just work (#2)
- Loading branch information
Showing
12 changed files
with
130 additions
and
63 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
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,5 @@ | ||
--- | ||
"yocto-ttl-cache": minor | ||
--- | ||
|
||
feat: first blood, should just work |
This file was deleted.
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
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,32 @@ | ||
name: Size Limit | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
size-limit: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Setup pnpm | ||
uses: pnpm/action-setup@v2 | ||
with: | ||
version: latest | ||
|
||
- name: Setup Node.js ${{ matrix.node }} | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: ${{ matrix.node }} | ||
cache: pnpm | ||
|
||
- name: Install Dependencies | ||
run: pnpm i | ||
|
||
- uses: andresz1/size-limit-action@v1 | ||
with: | ||
github_token: ${{ secrets.GITHUB_TOKEN }} | ||
skip_step: install | ||
script: pnpm size-limit --json |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "lib-boilerplate-docs", | ||
"name": "yocto-ttl-cache-docs", | ||
"type": "commonjs", | ||
"description": "A simple library boilerplate." | ||
"description": "The smallest and fastest TTL cache implementation in JavaScript" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,25 @@ | ||
export default () => 'Hello World!' | ||
const perf = typeof performance === 'undefined' ? Date : performance | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- it's fine | ||
export class TTLCache<K extends keyof any, V = unknown> { | ||
private readonly ttlMap = new Map<K, number>() | ||
private readonly valueMap = new Map<K, V | undefined>() | ||
|
||
constructor(private readonly ttl = 1000) {} | ||
|
||
get(key: K, setter?: (value?: V) => V) { | ||
const now = perf.now() | ||
const ttl = this.ttlMap.get(key) | ||
let value = this.valueMap.get(key) | ||
if (!ttl || value == null || now > ttl) { | ||
this.ttlMap.set(key, now + this.ttl) | ||
this.valueMap.set(key, (value = setter?.(value))) | ||
} | ||
return value | ||
} | ||
|
||
set(key: K, value: V) { | ||
this.ttlMap.set(key, perf.now() + this.ttl) | ||
this.valueMap.set(key, value) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,23 @@ | ||
import echo from 'lib-boilerplate' | ||
import { TTLCache } from 'yocto-ttl-cache' | ||
|
||
test('it should just work', () => { | ||
expect(echo()).toBe('Hello World!') | ||
const delay = (delay: number) => | ||
new Promise<void>(resolve => setTimeout(resolve, delay)) | ||
|
||
test('basic usage', async () => { | ||
const cache = new TTLCache<string, string>() | ||
expect(cache.get('key')).toBe(undefined) | ||
cache.set('key', 'value') | ||
expect(cache.get('key')).toBe('value') | ||
expect(cache.get('key')).toBe('value') | ||
await delay(3 * 500) | ||
expect(cache.get('key')).toBe(undefined) | ||
}) | ||
|
||
test('get with setter', async () => { | ||
const cache = new TTLCache<string, string>() | ||
expect(cache.get('key')).toBe(undefined) | ||
expect(cache.get('key', () => 'value')).toBe('value') | ||
expect(cache.get('key', () => 'unused')).toBe('value') | ||
await delay(3 * 500) | ||
expect(cache.get('key')).toBe(undefined) | ||
}) |
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