Cache Module
Use this devon4ng Angular module when you want to cache requests to server. You may configure it to store in cache only the requests you need and to set the duration you want.
Installation
$ npm i object-hash @devon4ng/cache
Or you can use yarn:
$ yarn add object-hash @devon4ng/cache
To install the correct vesion for your Angular project please refer to the @devon4ng/cache versions available.
Usage
Import the dependency in your app.module.ts
:
import { CacheModule } from '@devon4ng/cache';
Import and configure it in the @NgModule
imports section:
@NgModule({
...
imports: [
...otherModules,
CacheModule.forRoot({
maxCacheAge: 1800000, // number
urlRegExp: new RegExp('http.*', 'g').toString(); // RegExp object or string
}),
]
})
In case you need to define a list of strings instead, you can do the following:
@NgModule({
...
imports: [
...otherModules,
CacheModule.forRoot({
maxCacheAge: 1800000, // number
urlRegExp: ['data', 'users']; // strings array
}),
]
})
The configuration object defines the following two parameters:
-
maxCacheAge
: Age in milliseconds. After that the cache entry will be developed. By default1800000
or 30 minutes. -
urlRegExp
: Regular expression asstring
,string[]
orRegExp
object that defines which URLs are going to be cached. By default any URL that containshttp
, that is all the URLs.
Both parameters are optional, so you could set up only one.