Skip to content

Commit

Permalink
init release to npm (#759)
Browse files Browse the repository at this point in the history
* init release to npm

* adding in storage mode

* adding in fixes

* moving to 20 and 22
  • Loading branch information
jaredwray authored Sep 6, 2024
1 parent 5ab5e96 commit 2de1ba2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:

strategy:
matrix:
node-version: ['18', '20', '22']
node-version: ['20', '22']

steps:
- uses: actions/checkout@v4
Expand Down
10 changes: 10 additions & 0 deletions packages/cacheable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
npm install cacheable
```

## Basic Usage

```javascript
import { Cacheable } from 'cacheable';

const cacheable = new Cacheable();
cacheable.set('key', 'value', 1000);
const value = cacheable.get('key');
```

## Extending Your own Caching Engine

```javascript
Expand Down
11 changes: 6 additions & 5 deletions packages/cacheable/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
"private": false,
"scripts": {
"build": "tsc -p tsconfig.build.json",
"test": "xo",
"test": "xo --fix",
"test:ci": "xo",
"clean": "rm -rf dist && rm -rf node_modules"
},
"devDependencies": {
"eslint": "^8.56.0",
"ts-node": "^10.9.1",
"typescript": "^5.1.6",
"xo": "^0.55.0"
"eslint": "^9.9.1",
"ts-node": "^10.9.2",
"typescript": "^5.5.4",
"xo": "^0.59.3"
},
"dependencies": {
"eventemitter3": "^5.0.1",
"hookified": "^0.7.0",
"keyv": "^5.0.1"
},
"xo": {
Expand Down
93 changes: 43 additions & 50 deletions packages/cacheable/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {Keyv} from 'keyv';
import EventEmitter from 'eventemitter3';

type HookFunction = (...args: any[]) => void;
import {Hookified} from 'hookified';

type CacheableStatsItem = {
key: string;
Expand All @@ -24,26 +22,41 @@ type CacheableStats = {
};

export enum CacheableHooks {
PRE_SET = 'preSet',
POST_SET = 'postSet',
PRE_SET_MANY = 'preSetMany',
POST_SET_MANY = 'postSetMany',
PRE_GET = 'preGet',
POST_GET = 'postGet',
PRE_GET_MANY = 'preGetMany',
POST_GET_MANY = 'postGetMany',
BEFORE_SET = 'beforeSet',
AFTER_SET = 'afterSet',
BEFORE_SET_MANY = 'beforeSetMany',
AFTER_SET_MANY = 'afterSetMany',
BEFORE_GET = 'beforeGet',
AFTER_GET = 'afterGet',
BEFORE_GET_MANY = 'beforeGetMany',
AFTER_GET_MANY = 'afterGetMany',
}

export enum CacheableEvents {
ERROR = 'error',
}

export class Cacheable extends EventEmitter {
export enum CacheableTieringModes {
PRIMARY_WITH_FAILOVER = 'primarySecondary',
ACID = 'allPrimary',
PRIMARY_ALL_FAILOVER = 'primaryAllFailover',
}

export type CacheableOptions = {
store?: Keyv;
enableStats?: boolean;
enableOffline?: boolean;
nonBlocking?: boolean;
};

export class Cacheable extends Hookified {
private _store: Keyv = new Keyv();
private readonly _stats: CacheableStats = {currentSize: 0, cacheSize: 0, hits: 0, misses: 0, hitRate: 0, averageLoadPenalty: 0, loadSuccessCount: 0, loadExceptionCount: 0, totalLoadTime: 0, topHits: [], leastUsed: []};
private readonly _hooks = new Map<string, HookFunction>();
private _cacheSizeLimit = 0;
private _statsEnabled = false;
private readonly _stats: CacheableStats = {
currentSize: 0, cacheSize: 0, hits: 0, misses: 0, hitRate: 0, averageLoadPenalty: 0, loadSuccessCount: 0, loadExceptionCount: 0, totalLoadTime: 0, topHits: [], leastUsed: [],
};

private _enableStats = false;
private _enableOffline = false;

constructor(keyv?: Keyv) {
super();
Expand All @@ -53,20 +66,20 @@ export class Cacheable extends EventEmitter {
}
}

public get statsEnabled(): boolean {
return this._statsEnabled;
public get enableStats(): boolean {
return this._enableStats;
}

public set statsEnabled(enabled: boolean) {
this._statsEnabled = enabled;
public set enableStats(enabled: boolean) {
this._enableStats = enabled;
}

public get cacheSizeLimit(): number {
return this._cacheSizeLimit;
public get enableOffline(): boolean {
return this._enableOffline;
}

public set cacheSizeLimit(limit: number) {
this._cacheSizeLimit = limit;
public set enableOffline(enabled: boolean) {
this._enableOffline = enabled;
}

public get store(): Keyv {
Expand All @@ -81,34 +94,14 @@ export class Cacheable extends EventEmitter {
return this._stats;
}

public get hooks(): Map<string, HookFunction> {
return this._hooks;
}

public setHook(name: string, fn: HookFunction): void {
this._hooks.set(name, fn);
}

public deleteHook(name: string): void {
this._hooks.delete(name);
}

public triggerHook(name: string, ...args: any[]) {
const hook = this._hooks.get(name);
if (hook) {
/* eslint-disable-next-line @typescript-eslint/no-unsafe-argument */
hook(...args);
}
}

public async get<T>(key: string): Promise<T | undefined> {
let result;
try {
this.triggerHook(CacheableHooks.PRE_GET, key);
await this.hook(CacheableHooks.BEFORE_GET, key);
result = await this._store.get(key) as T;
this.triggerHook(CacheableHooks.POST_GET, key, result);
await this.hook(CacheableHooks.AFTER_GET, {key, result});
} catch (error: unknown) {
this.emit(CacheableEvents.ERROR, error);
await this.emit(CacheableEvents.ERROR, error);
}

return result;
Expand All @@ -117,11 +110,11 @@ export class Cacheable extends EventEmitter {
public async set<T>(key: string, value: T, ttl?: number): Promise<boolean> {
let result = false;
try {
this.triggerHook(CacheableHooks.PRE_SET, key, value, ttl);
await this.hook(CacheableHooks.BEFORE_SET, {key, value, ttl});
result = await this._store.set(key, value, ttl);
this.triggerHook(CacheableHooks.POST_SET, key, value, ttl);
await this.hook(CacheableHooks.AFTER_SET, {key, value, ttl});
} catch (error: unknown) {
this.emit(CacheableEvents.ERROR, error);
await this.emit(CacheableEvents.ERROR, error);
}

return result;
Expand Down

0 comments on commit 2de1ba2

Please sign in to comment.