Add configurable cache request feature to your Angular project
npm install @devon4ng/cachebash
$ npm i object-hash @devon4ng/cache
`
Or you can use yarn:
`bash
$ 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:
`typescript
import { CacheModule } from '@devon4ng/cache';
`
Import and configure it in the @NgModule imports section:
`typescript
@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:
`typescript
@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 default 1800000 or 30 minutes.
- urlRegExp: Regular expression as string, string[] or RegExp object that defines which URLs are going to be cached. By default any URL that contains http`, that is all the URLs.