# @grec0/ngx-cachemanager

Latest version **0.0.157** (published 2024-07-12) · 0 weekly downloads

## Install

```sh
npm install @grec0/ngx-cachemanager
pnpm add @grec0/ngx-cachemanager
yarn add @grec0/ngx-cachemanager
bun add @grec0/ngx-cachemanager
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.157 |
| Published | 2024-07-12 |
| First published | 2021-04-26 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 790.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | grec0 |

## Links

- npm: https://www.npmjs.com/package/@grec0/ngx-cachemanager
- npm.io page: https://npm.io/package/@grec0/ngx-cachemanager

## Dependencies (1)

- [tslib](https://npm.io/package/tslib.md) ^2.3.0

## Recent versions

- 0.0.157 (latest) — 2024-07-12
- 0.0.156 — 2024-07-12
- 0.0.155 — 2024-07-12
- 0.0.154 — 2024-07-12
- 0.0.153 — 2024-07-12
- 0.0.151 — 2024-07-12
- 0.0.149 — 2024-07-12
- 0.0.148 — 2024-07-12
- 0.0.146 — 2024-07-12
- 0.0.144 — 2024-07-11
- 0.0.142 — 2024-07-11
- 0.0.141 — 2024-07-11
- 0.0.140 — 2024-07-11
- 0.0.139 — 2023-07-08
- 0.0.138 — 2023-06-28
- … 38 more at https://npm.io/package/@grec0/ngx-cachemanager/versions

## README

# ngx-cachemanager

<!-- [![bower version](https://img.shields.io/bower/v/ngx-cachemanager.svg?style=flat)](https://www.npmjs.org/package/ngx-cachemanager)
[![npm version](https://img.shields.io/npm/v/ngx-cachemanager.svg?style=flat)](https://www.npmjs.org/package/ngx-cachemanager)
[![Circle CI](https://img.shields.io/circleci/project/jmdobry/ngx-cachemanager/master.svg?style=flat)](https://circleci.com/gh/jmdobry/ngx-cachemanager/tree/master)
[![npm downloads](https://img.shields.io/npm/dm/ngx-cachemanager.svg?style=flat)](https://www.npmjs.org/package/ngx-cachemanager)
[![codecov](https://img.shields.io/codecov/c/github/jmdobry/ngx-cachemanager.svg)](https://codecov.io/gh/jmdobry/ngx-cachemanager) -->

Líbrería Angular para la gestión de la cache implícita.

Con esta librería se pretende desacoplar la propia lógica de cache sobre el desarrollador, de tal manera que, le permite más control sobre ella sin necesidad de líneas de código solo de algunos Decorators.

Actualmente la comunidad en caso necesario, sulee delegar esta cache en un interceptor HTTP, lo cúal, no esta mal pero, para formatos de aplicación CRUD no suele ser válido además que implica saber que se realiza una llamada HTTP.

Con esta librería ignoraremos lo que hay por detras, ngx-cachemanager hace esto por ti, da igual si la petición es sincrona o no, Promise o Observable si el tipo está definido ngx-cachemanager sabrá que hacer.

En caso de que no se defina el tipo que devuelve la función ngx-cachemanager tomará esta cache como síncrona y puede llevar a errores en caso de que si lo séa.

Un ejemplo:

### MAL (RETURN TYPE UNDEFINED async = false)
```ts
@Cache({map: 'tasks'})
private getTasks(range) {

    return this.projectService.getProject(
      this.clientId
    );
  }

```

### BIEN (RETURN TYPE Observable async = true)
```ts
@Cache({map: 'tasks'})
private getTasks(range): Observable<ITaskDetail[]> {

    return this.projectService.getProject(
      this.clientId
    );
  }

```

### Table of Contents
- [Quick Start](#quick-start)
- [The Basics](#the-basics)
- [Working with a cache](#working-with-a-cache)
- [Configuration Options](#configuration-options)
- [Using ngx-cachemanager with $http](#using-ngx-cachemanager-with-http)
- [Using ngx-cachemanager with localStorage](#using-ngx-cachemanager-with-localstorage)
- [API Reference](#api-reference)

### Quick Start
`bower install --save @grec0/ngx-cachemanager` or `npm install --save @grec0/ngx-cachemanager`.

```app.module.ts
2 opciones para inicializar la configuración:
1) APP_INITIALIZER (recomendada carga en nivel superior) "loadGlobalConfig(config: ConfigCacheGlobal): Promise<boolean>;"

2) Root Module environment (carga en nivel inferior) NgxCachemanagerModule.forRoot(config);
```

### 1) APP_INITIALIZER app.module.ts
```ts
export function initializer(cacheConfig: CacheConfigService): () => Promise<any> {
	return (): Promise<any> => cacheConfig.loadGlobalConfig({});
}

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    NgxCachemanagerModule
  ],
  providers: [
    {
			provide: APP_INITIALIZER,
			useFactory: initializer,
			deps: [CacheConfigService],
			multi: true
    }
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

```

### 2) Root Module environment app.module.ts
```ts
import{ environment } from '@environment'; // environment contiene el objeto de configuración de cache descrito mas abajo.



@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    NgxCachemanagerModule.forRoot(environment)
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

```
<!-- 
#### Why would you want to replace $cacheFactory?

| Feature | $cacheFactory | ngx-cachemanager |
| ----------- | ------------------- | --------------- |
| core implementation usable outside of Angular | no | [__yes__](https://github.com/jmdobry/CacheFactory) |
| key/value store | __yes__ | __yes__ |
| in-memory support | __yes__ | __yes__ |
| localStorage support | no | __yes__ |
| sessionStorage support | no | __yes__ |
| custom storage support | no | __yes__ |
| can set maximum capacity | __yes__ | __yes__ |
| LRU support | __yes__ | __yes__ |
| support for time-based expiration of items | no | __yes__ |
| remove all expired items | n/a | __yes__ |
| "touch" an item | n/a | __yes__ |
| execute callback when an item expires | n/a | __yes__ |
| get info about cache | __yes__ | __yes__ |
| get info about single item | no | __yes__ |
| get item by key | __yes__ | __yes__ |
| remove item by key | __yes__ | __yes__ |
| remove all items | __yes__ | __yes__ |
| get keys of all caches | no | __yes__ |
| clear all caches | no | __yes__ |
| disable/enable a cache | no | __yes__ |
| "touch" all items in all caches | n/a | __yes__ |
| remove expired items from all caches | n/a | __yes__ |
| get all keys in a cache | no | __yes__ |
| clear a cache on a given interval | no | __yes__ | -->

### The Basics

Primero extendemos nuestro servicio de la clase `CacheableService` y aplicamos el decorator encima del @injectable con la estrategia requerida:

```ts
@Cacheable({defaultStrategy:'memory', logger: true})
@Injectable({providedIn:'root'})
export class UserService extends CacheableService {

  @Cache({timeout: 10000, map: 'users-search'})
  getUsers(clientId: string): Observable<Array<User>> {
    return this.userHttpService.getUsers(clientId);
  }
}
```

---
_Source: https://npm.io/package/@grec0/ngx-cachemanager · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
