# @karacas/kaysh

> In-memory caching with support for RxJs observables, native promises, functions & methods. Optional localStorage.

Latest version **0.2.2** (published 2024-07-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install @karacas/kaysh
pnpm add @karacas/kaysh
yarn add @karacas/kaysh
bun add @karacas/kaysh
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.2 |
| Published | 2024-07-04 |
| First published | 2022-01-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 169.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | karacas |
| Maintainers | karacas |
| Keywords | RxJS, Cache, Observables |

## Links

- npm: https://www.npmjs.com/package/@karacas/kaysh
- Repository: https://github.com/karacas/kaysh
- Issues: https://github.com/karacas/kaysh/issues
- npm.io page: https://npm.io/package/@karacas/kaysh

## Dependencies (1)

- [rxjs](https://npm.io/package/rxjs.md) ^6.6.0

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 0.2.2 (latest) — 2024-07-04
- 0.1.89 — 2022-02-02
- 0.1.76 — 2022-01-26
- 0.1.72 — 2022-01-26
- 0.1.60 — 2022-01-26
- 0.1.42 — 2022-01-25
- 0.1.40 — 2022-01-25

## README

# Kaysh

## In-memory caching with support for RxJs observables, native promises, functions & methods. Optional localStorage.

<br />
<br />

### <b>Config</b>

```typescript
const optional_config = {
  localStorage: true  /* Uses localStorage / Promises and RxJs automatically save a resolved value | dafault:false */,
  maxItems: 10        /* Maximum values cached per function/args | dafault:10 */,
  maxTime: 5 * 1000   /* Maximum time of the cached value in milliseconds | dafault:0(infinite) */,
};
```

<br />
<br />

### <b>RxJs cache operator</b>

```typescript
import { kayshOperator, kayshFlushOperator } from '@karacas/kaysh';

const getObservable = (payLoad?) => {
  return of(Math.random() + JSON.stringify(payLoad))
          .pipe(kayshOperator('getObservable', payLoad, optional_config));
};

const flushObservableValue = (payLoad?) => kayshFlushOperator('getObservable', payLoad);
const flushObservable = () => kayshFlushOperator('getObservable');
```

> Expects

```typescript
test('TestObserbable Operator', async function() {
  let value1 = await getObservable([1, 2]).toPromise();
  let value2 = await getObservable([1, 2]).toPromise();

  expect(value1).toBe(value2);

  flushObservableValue([1, 2]); // or flushObservable() for all values

  let value3 = await getObservable([1, 2]).toPromise();
  expect(value3).not.toBe(value1);
});
```

<br />
<br />

### <b>Memoize a function</b>

```typescript
function testFunction(val) {
  return String(Math.random()) + JSON.stringify(val);
}

const testFunctionMem = kayshMemFunction(testFunction, optional_config);
const flush = kayshFlushMemFunction(testFunction);
```

> Expects

```typescript
test('Test Simple function', function() {
  let value1 = testFunctionMem({ test: [1] });
  let value2 = testFunctionMem({ test: [1] });

  expect(value1).toBe(value2);

  flush({ test: [1] }); // or flush() for all values

  let value3 = testFunctionMem({ test: [1] });
  expect(value3).not.toBe(value1);
});
```

<br />
<br />

### <b>Memoize a Class method</b>

```typescript
import { kayshMemFunction, kayshFlushMemFunction, kayshDecorator } from '@karacas/kaysh';

class CacheTestClass {
  //Normal

  funcTest(v = 100) {
    return 1 + v;
  }

  funcTestMemo = kayshMemFunction(this.funcTest);
  flushFuncTestMemo = kayshFlushMemFunction(this.funcTest);
}
```

> Or

```typescript
class CacheClassExampleWithDecorator {
  //Decorator

  @kayshDecorator(kayshOptions)
  testDecorated(v = 100) {
    return 1 + v;
  }

  flushTestDecorated = kayshFlushMemFunction(this.testDecorated);
}
```

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