Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/templarian/promisecache
TypeScript Promise Cache Dectorator
https://github.com/templarian/promisecache
angular promise typescript
Last synced: 6 days 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 (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2018-01-28T20:37:32.000Z (almost 7 years ago)
- Last Synced: 2024-11-08T13:52:25.135Z (about 2 months ago)
- Topics: angular, promise, typescript
- Language: TypeScript
- Size: 2.93 KB
- Stars: 0
- Watchers: 3
- 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
}
```