Skip to main content

Configuration

Four exports drive configuration: an interface, an injection token, a standalone provider factory, and an NgModule for legacy projects.

import {
INcachedConfig,
NCACHED_CONFIG,
NcachedModule,
provideNcachedConfig,
} from 'ng-ncached';

INcachedConfig

interface INcachedConfig {
persistence?: {
enabled: boolean;
storageKey?: string;
compressor?: ICompressor;
};
}
FieldTypeDefaultNotes
persistenceobject?undefinedOmit for pure in-memory mode.
persistence.enabledbooleanrequiredtrue opts in to hydration + beforeunload save.
persistence.storageKeystring?'ncached_snapshot'The localStorage key. Bake a version into it for clean migrations.
persistence.compressorICompressor?new NoopCompressor()Plug in LzStringCompressor or your own. See Compressors.

NCACHED_CONFIG

export const NCACHED_CONFIG: InjectionToken<INcachedConfig>;

The Angular InjectionToken consumed by NcachedService. The service injects it as @Optional() @Inject(NCACHED_CONFIG) config: INcachedConfig | null — when the token isn't provided, the service runs in pure in-memory mode.

You'll usually wire this up via provideNcachedConfig(), but the raw token is exported for advanced cases like useFactory:

{
provide: NCACHED_CONFIG,
useFactory: (env: EnvService): INcachedConfig => ({
persistence: {
enabled: env.isProduction,
storageKey: `cache_${env.appVersion}`,
},
}),
deps: [EnvService],
}

provideNcachedConfig

function provideNcachedConfig(config: INcachedConfig): Provider;

A small convenience function that returns { provide: NCACHED_CONFIG, useValue: config }. Use it in the providers array of your ApplicationConfig, NgModule, or route config.

Example

import { ApplicationConfig } from '@angular/core';
import { LzStringCompressor, provideNcachedConfig } from 'ng-ncached';

export const appConfig: ApplicationConfig = {
providers: [
provideNcachedConfig({
persistence: {
enabled: true,
storageKey: 'app_cache_v1',
compressor: new LzStringCompressor(),
},
}),
],
};

NcachedModule

class NcachedModule {
static forRoot(config: INcachedConfig): ModuleWithProviders<NcachedModule>;
}

NgModule wrapper around provideNcachedConfig() for Angular 12-13 consumers (or any project still using the classic NgModule-based bootstrap). forRoot() delegates to provideNcachedConfig() under the hood, so both entry points produce identical runtime behaviour.

Example

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 Configuration guide for full setup walkthroughs and Persistence & compression for what each option actually does at runtime.