Skip to content

Commit

Permalink
feat: first blood, should just work (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin committed Jul 10, 2022
1 parent d6549f2 commit 3d3d340
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"changelog": [
"@changesets/changelog-github",
{
"repo": "un-ts/lib-boilerplate"
"repo": "un-ts/yocto-ttl-cache"
}
],
"commit": false,
Expand Down
5 changes: 5 additions & 0 deletions .changeset/loud-mails-design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"yocto-ttl-cache": minor
---

feat: first blood, should just work
25 changes: 0 additions & 25 deletions .github/workflows/pkg-size.yml

This file was deleted.

8 changes: 4 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ on:
jobs:
release:
name: Release
if: github.repository != 'rx-ts/lib-boilerplate'
if: github.repository != 'rx-ts/yocto-ttl-cache'
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
fetch-depth: 0
Expand All @@ -35,8 +35,8 @@ jobs:
uses: changesets/action@v1
with:
publish: pnpm release
commit: 'chore: release lib-boilerplate'
title: 'chore: release lib-boilerplate'
commit: 'chore: release yocto-ttl-cache'
title: 'chore: release yocto-ttl-cache'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
32 changes: 32 additions & 0 deletions .github/workflows/size-limit.yml
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
37 changes: 24 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# lib-boilerplate
# yocto-ttl-cache

[![GitHub Actions](https://github.com/un-ts/lib-boilerplate/workflows/CI/badge.svg)](https://github.com/un-ts/lib-boilerplate/actions/workflows/ci.yml)
[![Codecov](https://img.shields.io/codecov/c/github/un-ts/lib-boilerplate.svg)](https://codecov.io/gh/un-ts/lib-boilerplate)
[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/un-ts/lib-boilerplate.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/un-ts/lib-boilerplate/context:javascript)
[![type-coverage](https://img.shields.io/badge/dynamic/json.svg?label=type-coverage&prefix=%E2%89%A5&suffix=%&query=$.typeCoverage.atLeast&uri=https%3A%2F%2Fraw.githubusercontent.com%2Frx-ts%2Flib-boilerplate%2Fmain%2Fpackage.json)](https://github.com/plantain-00/type-coverage)
[![npm](https://img.shields.io/npm/v/lib-boilerplate.svg)](https://www.npmjs.com/package/lib-boilerplate)
[![GitHub Release](https://img.shields.io/github/release/un-ts/lib-boilerplate)](https://github.com/un-ts/lib-boilerplate/releases)
[![GitHub Actions](https://github.com/un-ts/yocto-ttl-cache/workflows/CI/badge.svg)](https://github.com/un-ts/yocto-ttl-cache/actions/workflows/ci.yml)
[![Codecov](https://img.shields.io/codecov/c/github/un-ts/yocto-ttl-cache.svg)](https://codecov.io/gh/un-ts/yocto-ttl-cache)
[![Language grade: JavaScript](https://img.shields.io/lgtm/grade/javascript/g/un-ts/yocto-ttl-cache.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/un-ts/yocto-ttl-cache/context:javascript)
[![type-coverage](https://img.shields.io/badge/dynamic/json.svg?label=type-coverage&prefix=%E2%89%A5&suffix=%&query=$.typeCoverage.atLeast&uri=https%3A%2F%2Fraw.githubusercontent.com%2Fun-ts%2Fyocto-ttl-cache%2Fmain%2Fpackage.json)](https://github.com/plantain-00/type-coverage)
[![npm](https://img.shields.io/npm/v/yocto-ttl-cache.svg)](https://www.npmjs.com/package/yocto-ttl-cache)
[![GitHub Release](https://img.shields.io/github/release/un-ts/yocto-ttl-cache)](https://github.com/un-ts/yocto-ttl-cache/releases)

[![Conventional Commits](https://img.shields.io/badge/conventional%20commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
[![Renovate enabled](https://img.shields.io/badge/renovate-enabled-brightgreen.svg)](https://renovatebot.com)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
[![Code Style: Prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
[![changesets](https://img.shields.io/badge/maintained%20with-changesets-176de3.svg)](https://github.com/atlassian/changesets)

A simple library boilerplate.
The smallest and fastest TTL cache implementation in JavaScript

## TOC <!-- omit in toc -->

Expand All @@ -31,21 +31,32 @@ A simple library boilerplate.

```sh
# pnpm
pnpm add lib-boilerplate
pnpm add yocto-ttl-cache

# yarn
yarn add lib-boilerplate
yarn add yocto-ttl-cache

# npm
npm i lib-boilerplate
npm i yocto-ttl-cache
```

### API

```js
import echo from 'lib-boilerplate'
import { TTLCache } from 'yocto-ttl-cache' // only named export is supported

echo()
const cache = new TTLCache(1000) // the `ttl` option of `constructor` is `1000` by default

cache.get('key') // `undefined`
cache.get('key', () => 'value') // get with `setter`, `'value'`
cache.get('key', () => 'unused') // `'value'` is not stale

cache.set('key', 'newValue')
cache.get('key') // `'newValue'`

setTimeout(() => {
cache.get('key') // `undefined`, stale after `ttl`
}, 1000)
```

## Sponsors
Expand Down
4 changes: 2 additions & 2 deletions docs/package.json
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"
}
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "lib-boilerplate",
"version": "0.0.2",
"name": "yocto-ttl-cache",
"version": "0.0.0",
"type": "module",
"description": "A simple library boilerplate.",
"repository": "git+https://github.com/rx-ts/lib-boilerplate.git",
"description": "The smallest and fastest TTL cache implementation in JavaScript",
"repository": "git+https://github.com/rx-ts/yocto-ttl-cache.git",
"author": "JounQin (https://www.1stG.me) <admin@1stg.me>",
"donate": {
"recipients": [
Expand Down Expand Up @@ -89,7 +89,7 @@
"sirv-cli": "^2.0.2",
"size-limit": "^7.0.8",
"ts-jest": "^28.0.5",
"type-coverage": "^2.21.2",
"type-coverage": "^2.22.0",
"typescript": "^4.7.4"
},
"publishConfig": {
Expand All @@ -105,7 +105,7 @@
"size-limit": [
{
"path": "lib/index.js",
"limit": "180b"
"limit": "200b"
}
],
"typeCoverage": {
Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 25 additions & 1 deletion src/index.ts
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)
}
}
24 changes: 21 additions & 3 deletions test/basic.spec.ts
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)
})
4 changes: 3 additions & 1 deletion vercel.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"version": 2,
"alias": [
"lib-boilerplate.vercel.app"
"ttlcache.vercel.app",
"ttl-cache.vercel.app",
"yocto-ttl-cache.vercel.app"
],
"github": {
"silent": true
Expand Down

0 comments on commit 3d3d340

Please sign in to comment.