ng-ncached v0.1.3
ng-ncached
Just a simple Angular multi layer cache service
ng-ncached has been thought as a multi layer cache for Angular applications. Its purpose is simple: allowing developers to create a layered (or multi-level) cache in a breeze.
Installation
Use npm (or yarn if you prefer)
npm install ng-ncached
Usage
import { NcachedService } from 'ng-ncached';
Inject the service in a proper injection context
class AppComponent {
private _ncachedService: NcachedService = inject(NcachedService);
}
Set a value
This will create a Map into an object like this: {key1: new Map()}. The 'value' will be placed into the 'key2' key of the Map instance
this._ncachedService.set('value', 'key1', 'key2');
Set a value in a deeper level
This will create a Map into an object like this: {root: {parent: new Map()}}. The 'value' will be placed into the 'child' key of the Map instance
this._ncachedService.set('value', 'root', 'parent', 'child');
Get a value
Once you have properly set a value, you can use the get method to read from the cache. Accordingly to the two previous examples:
this._ncachedService.get('key1', 'key2');
this._ncachedService.get('root', 'parent', 'child');
You may want to provide a generic type for defining the datatype of the retrieved item:
this._ncachedService.get<string>('key1', 'key2');
Errors
Both the get and the set methods shall throw errors if something is going wrong, so you may be interested in wrapping these methods into a try / catch statement. Errors are declared into the CacheServiceErrors namespace.
Get method
- InsufficientsKeysProvidedError - Less than 2 keywords have been provided
- ValueNotFound - The last key provided has not been found into the Map instance, so no value has been found for the lookup key
Set method
- InsufficientsKeysProvidedError - Less than 2 keywords have been provided
Running tests
npm run test
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.