@joebobstevedave/ngx-cache-platform-server v8.0.0
@ngx-cache/platform-server

Server platform implementation of ngx-cache
Please support this project by simply putting a Github star. Share this library with friends on Twitter and everywhere else you can.
NOTICE
This 7.x.x branch is intented to work with
Angular v7.x.x. If you're developing on a later release of Angular thanv7.x.x, then you should probably choose the appropriate version of this library by visiting the master branch.
Table of contents:
Prerequisites
This library depends on Angular v6.0.0. Older versions contain outdated dependencies, might produce errors.
Also, please ensure that you are using Typescript v2.7.2 or higher.
Getting started
Installation
You can install @ngx-cache/platform-server using npm
npm install @ngx-cache/platform-server --saveNote: You should have already installed [@ngx-cache/core].
Note: You should have also installed [@ngx-cache/fs-storage] (which is currently the only available storage on the server platform).
Examples
- ng-seed/universal is an officially maintained seed project, showcasing common patterns and best practices for
@ngx-cache/platform-server.
Related packages
The following packages may be used in conjunction with @ngx-cache/platform-server:
Adding @ngx-cache/platform-server to your project (SystemJS)
Add map for @ngx-cache/platform-server in your systemjs.config
'@ngx-cache/platform-server': 'node_modules/@ngx-cache/platform-server/bundles/platform-server.umd.min.js'app.module configuration
- Import
ServerCacheModuleusing the mapping'@ngx-cache/platform-server'and appendServerCacheModule.forRoot({...})within the imports property of app.server.module (considering the app.server.module is the server module in Angular Universal application). - Import
CACHEinjection token using the mapping'@ngx-cache/core',FsCacheServiceusing the mapping'@ngx-cache/platform-server'. - Provide
CACHEusingFsCacheService, by calling the forRoot static method using theServerCacheModule. - Import
STORAGEinjection token using the mapping'@ngx-cache/core',FsStorageServiceusing the mapping'@ngx-cache/fs-storage'. - Provide
STORAGEusingFsStorageService, by calling the forRoot static method using theServerCacheModule. - Import
FsStorageLoaderandfsStorageFactoryusing the mapping'@ngx-cache/fs-storage'. - Provide
FsStorageLoaderusingfsStorageFactory, by calling the forRoot static method using theServerCacheModule. - Pass
CacheServicetogether withApplicationRefandTransferStateto the implementation ofbootstrapFactorymethod below.
app.server.module.ts
...
import { CacheService, CACHE, STORAGE } from '@ngx-cache/core';
import { ServerCacheModule, FsCacheService } from '@ngx-cache/platform-server';
import { fsStorageFactory, FsStorageLoader, FsStorageService } from '@ngx-cache/fs-storage';
...
export function bootstrapFactory(appRef: ApplicationRef,
transferState: TransferState,
cache: CacheService): () => Subscription {
return () => appRef.isStable
.filter(stable => stable)
.first()
.subscribe(() => {
transferState.set<any>(makeStateKey(cache.key), JSON.stringify(cache.dehydrate()));
});
}
@NgModule({
declarations: [
AppComponent,
...
],
...
imports: [
...
ServerCacheModule.forRoot([
{
provide: CACHE,
useClass: FsCacheService
},
{
provide: STORAGE,
useClass: FsStorageService
},
{
provide: FsStorageLoader,
useFactory: (fsStorageFactory)
}
]),
],
...
providers: [
{
provide: APP_BOOTSTRAP_LISTENER,
useFactory: (bootstrapFactory),
multi: true,
deps: [
ApplicationRef,
TransferState,
CacheService
]
}
],
bootstrap: [AppComponent]
})
export class AppServerModule {
}:+1: Yeah!
@ngx-cache/platform-serverwill now provide server platform implementation to @ngx-cache/core.
Credits
- universal-starter: Angular 2 Universal starter kit by @AngularClass
License
The MIT License (MIT)
Copyright (c) 2018 Burak Tasci
6 years ago