https://github.com/templarian/promisecache
TypeScript Promise Cache Dectorator
https://github.com/templarian/promisecache
angular promise typescript
Last synced: about 1 month ago
JSON representation
TypeScript Promise Cache Dectorator
- Host: GitHub
- URL: https://github.com/templarian/promisecache
- Owner: Templarian
- License: mit
- Created: 2018-01-28T20:03:37.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-01-28T20:37:32.000Z (over 8 years ago)
- Last Synced: 2025-06-02T03:04:08.083Z (about 1 year ago)
- Topics: angular, promise, typescript
- Language: TypeScript
- Size: 2.93 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TypeScript promiseCache
The `promiseCache` dectorator and service allow an easy ability to apply caching for a method returning an async return.
Caching promises can be useful when similar components are calling the same endpoint which can be common in larger forms or dropdown lists.
## Overview
- `promiseCache.decorator.ts` - The `@promiseCache()` dectorator allows caching of the entire method or based on exact parameters.
- `promiseCache.service.ts` - Stores a cache dictionary that allows clearing of data. This could be extended easily to cache across browser sessions if needed.
I will take pull requests on this repo. Mostly putting the code out there for others and for myself since I use it in a few locations.
## Examples
### Without Paramters
The easiest example is caching a basic `getItems` request.
```TypeScript
@PromiseCache()
async getItems(): Promise {
let res = await this.http.get('/api/items')
.toPromise();
return res.map(i => new Item().from(i)); // .from is a dto mapper
}
```
### With Parameters
Extending the example above with a basic search filter for a `term` paramters.
```TypeScript
@PromiseCache()
async getItems(@CacheParam term: string): Promise {
let res = await this.http.get('/api/items', {
params: {
term: term
}
}).toPromise();
return res.map(i => new Item().from(i)); // .from is a dto mapper
}
```