diff --git a/README.md b/README.md index c4b571d..0b6fadd 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,81 @@ -# local_cache -local cache for deno / typescript +# Local Cache +Local runtime caching for deno / typescript with ttl availability + +# Usage +```typescript +import { Cache } from "https://deno.land/x/local_cache"; + +interface Person { + firstName: string, + lastName: string +} + +// Create a new cache with a global ttl of 30 seconds +const cache: Cache = new Cache(30000); + +// Listen for 'ttl' events +cache.on('ttl', (key: string, person: Person, ttl: number) => { + console.log(`'${person.firstName} ${person.lastName}' with ID '${key}' expired after ${ttl / 1000} seconds`); +}); + +// Listen for 'add' events +cache.on('add', (key: string, person: Person, ttl?: number) => { + console.log(`Cached '${person.firstName} ${person.lastName}' with ID '${key}' [Expires after ${(ttl || 0) / 1000} seconds]`) +}) + +// add a new person with its own ttl of 5 seconds (overrides global ttl) +cache.add('someUniqueID', { + firstName: 'John', + lastName: 'Doe' +}); +``` + +# API +```typescript +>.add(key: K, value: V) => void; +``` +```typescript +>.set(key: K, value: V) => void; +``` +```typescript +>.get(key: K) => V; +``` +```typescript +>.has(key: K) => boolean; +``` +```typescript +>.delete(key: K) => void; +``` +```typescript +>.clear() => void; +``` +```typescript +>.empty() => boolean; +``` +```typescript +>.clone() => Cache; +``` +##### Other methods not stated here for space sake + +# Events +```typescript +>.on('ttl', (key: K, value: V, ttl: number) => void); +``` +```typescript +>.on('add', (key: K, value: V, ttl?: number) => void); +``` +```typescript +>.on('set', (key: K, value: V, ttl?: number) => void); +``` +```typescript +>.on('delete', (key: K, value: V) => void); +``` +```typescript +>.on('claer', (map: Map) => void); +``` + +# Maintainer +###### - Somber ([GitHub](https://github.com/SomberTM)) + +# License +###### Copyright (c) 2020 SomberTM ([MIT License](https://github.com/SomberTM/local_cache/blob/main/LICENSE))