https://github.com/adamjosefus/allo_caching
🦕 Simple cache for Deno.
https://github.com/adamjosefus/allo_caching
cache caching deno server typescript
Last synced: 10 months ago
JSON representation
🦕 Simple cache for Deno.
- Host: GitHub
- URL: https://github.com/adamjosefus/allo_caching
- Owner: adamjosefus
- Created: 2022-02-08T10:36:25.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-11-30T12:48:56.000Z (about 3 years ago)
- Last Synced: 2025-03-27T19:43:09.409Z (10 months ago)
- Topics: cache, caching, deno, server, typescript
- Language: TypeScript
- Homepage:
- Size: 30.3 KB
- Stars: 5
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# **Allo Caching** for Deno 🦕
Simple caching solution in Typescript.
## `Cache`
### Load or Generate
```ts
const cache = new Cache();
// Load from cache.
// If not found, value will be generated and stored in cache.
cache.load('key_1', () => {
// Some expensive operation
return 'Lorem ipsum';
}); // Return type is string
cache.has('key_1'); // true
cache.load('key_1'); // 'Lorem ipsum'
// Load from cache without generating.
cache.load('key_2'); // Return type is string | undefined
// Save to cache.
cache.save('key_2', 'Lorem ipsum');
// Check if cache exists.
cache.has('key_2'); // true
cache.load('key_2'); // 'Lorem ipsum'
```
### Invalidation
#### Expiration
```ts
const cache = new Cache();
cache.save('key', 42, {
expire: 1000 // Expire after 1 second.
});
// Works with load generator too.
cache.load('key', () => 42, { expire: 1000 });
```
```ts
const cache = new Cache();
cache.save('key', 42, {
expire: 1000,
sliding: true // Update expiration after each load
});
```
#### Files
```ts
const cache = new Cache();
cache.save('key', "My text data", {
files: [
'file_1.txt',
'file_2.txt'
] // Expired when some file is modified or deleted.
});
```
#### Callbacks
```ts
const cache = new Cache();
function isValid(): boolean {
//...
return true;
}
cache.save('key', "My text data", {
callbacks: [
isValid,
() => false,
] // Expired when some callback returns false.
});
```
## Documentation 📖
Description of all classes and methods with **examples** will found in the [documentation](https://doc.deno.land/https://deno.land/x/allo_caching/mod.ts).
---
Check out other [ours packages 📦](https://deno.land/x?query=allo_)!