ng-ncached
v1.2.0
Published
A lightweight, hierarchical in-memory cache for Angular with TTL, Observable integration, request deduplication, and optional localStorage persistence
Maintainers
Readme
ng-ncached
A lightweight, hierarchical in-memory cache for Angular with TTL, Observable integration, request deduplication, and opt-in localStorage persistence with pluggable compression.
📚 Full documentation: ng-ncached.andreatantimonaco.me
Install
npm install ng-ncachedQuick start
NcachedService is provided in root — just inject it:
import { Component, inject } from '@angular/core';
import { NcachedService } from 'ng-ncached';
@Component({ /* ... */ })
export class MyComponent {
private readonly _cache = inject(NcachedService);
ngOnInit(): void {
this._cache.set({ name: 'Alice' }, 'users', 'current');
const user = this._cache.get<{ name: string }>('users', 'current');
// => { name: 'Alice' }
}
}Enable localStorage persistence (optional)
The cache is in-memory only by default. To make it survive page reloads, opt in — optionally with a compressor to shrink the snapshot.
Standalone (Angular 14+):
// app.config.ts
import { ApplicationConfig } from '@angular/core';
import { LzStringCompressor, provideNcachedConfig } from 'ng-ncached';
export const appConfig: ApplicationConfig = {
providers: [
provideNcachedConfig({
persistence: {
enabled: true,
compressor: new LzStringCompressor(), // optional, defaults to NoopCompressor
},
}),
],
};NgModule (Angular 12+):
// app.module.ts
import { NgModule } from '@angular/core';
import { LzStringCompressor, NcachedModule } from 'ng-ncached';
@NgModule({
imports: [
NcachedModule.forRoot({
persistence: {
enabled: true,
compressor: new LzStringCompressor(),
},
}),
],
})
export class AppModule {}→ See the docs for guides on TTL, Observable caching, persistence, compression, the full API reference, and more.
