Skip to content

Commit

Permalink
feat(core-kernel): dispatch cache events
Browse files Browse the repository at this point in the history
  • Loading branch information
faustbrian authored Nov 25, 2019
1 parent b3aab12 commit c4a8b70
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
15 changes: 14 additions & 1 deletion __tests__/unit/core-kernel/services/cache/drivers/memory.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import "jest-extended";

import { Application } from "@packages/core-kernel/src/application";
import { Container, Identifiers } from "@packages/core-kernel/src/ioc";
import { MemoryCacheStore } from "@packages/core-kernel/src/services/cache/drivers/memory";
import { NotImplemented } from "@packages/core-kernel/src/exceptions/runtime";
import { MemoryEventDispatcher } from "@packages/core-kernel/src/services/events/drivers/memory";

const items: Record<string, number> = {
"1": 1,
Expand All @@ -14,8 +18,17 @@ const itemsBool: boolean[] = new Array(5).fill(true);
const itemsTruthy: boolean[] = new Array(5).fill(true);
const itemsFalsey: boolean[] = new Array(5).fill(false);

let app: Application;
let store: MemoryCacheStore<string, number>;
beforeEach(() => (store = new MemoryCacheStore<string, number>()));
beforeEach(() => {
app = new Application(new Container());

app.bind(Identifiers.EventDispatcherService)
.to(MemoryEventDispatcher)
.inSingletonScope();

store = app.resolve(MemoryCacheStore);
});

describe("MemoryCacheStore", () => {
it("should make a new instance", async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ import { Application } from "@packages/core-kernel/src/application";
import { Container, Identifiers } from "@packages/core-kernel/src/ioc";
import { ServiceProvider } from "@packages/core-kernel/src/services/cache";
import { MemoryCacheStore } from "@packages/core-kernel/src/services/cache/drivers/memory";
import { MemoryEventDispatcher } from "@packages/core-kernel/src/services/events/drivers/memory";

let app: Application;
beforeEach(() => {
app = new Application(new Container());

beforeEach(() => (app = new Application(new Container())));
app.bind(Identifiers.EventDispatcherService)
.to(MemoryEventDispatcher)
.inSingletonScope();
});

describe("CacheServiceProvider", () => {
it(".register", async () => {
Expand Down
12 changes: 12 additions & 0 deletions packages/core-kernel/src/enums/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ export enum KernelEvent {
ServiceProviderRegistered = "kernel.serviceProvider.registered",
}

/**
* @export
* @enum {number}
*/
export enum CacheEvent {
Flushed = "cache.flushed",
Forgotten = "cache.forgotten",
Hit = "cache.hit",
Missed = "cache.missed",
Written = "cache.written",
}

/**
* @export
* @enum {number}
Expand Down
30 changes: 28 additions & 2 deletions packages/core-kernel/src/services/cache/drivers/memory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { EventDispatcher } from "../../../contracts/kernel";
import { CacheStore } from "../../../contracts/kernel/cache";
import { CacheEvent } from "../../../enums";
import { NotImplemented } from "../../../exceptions/runtime";
import { injectable } from "../../../ioc";
import { Identifiers, inject, injectable } from "../../../ioc";

/**
* @export
Expand All @@ -9,6 +11,14 @@ import { injectable } from "../../../ioc";
*/
@injectable()
export class MemoryCacheStore<K, T> implements CacheStore<K, T> {
/**
* @private
* @type {EventDispatcher}
* @memberof BlockJob
*/
@inject(Identifiers.EventDispatcherService)
private readonly eventDispatcher!: EventDispatcher;

/**
* @private
* @type {Map<K, T>}
Expand Down Expand Up @@ -65,7 +75,13 @@ export class MemoryCacheStore<K, T> implements CacheStore<K, T> {
* @memberof MemoryCacheStore
*/
public async get(key: K): Promise<T | undefined> {
return this.store.get(key);
const value: T | undefined = this.store.get(key);

value
? this.eventDispatcher.dispatch(CacheEvent.Hit, { key, value })
: this.eventDispatcher.dispatch(CacheEvent.Missed, { key });

return value;
}

/**
Expand All @@ -91,6 +107,10 @@ export class MemoryCacheStore<K, T> implements CacheStore<K, T> {
public async put(key: K, value: T, seconds?: number): Promise<boolean> {
this.store.set(key, value);

if (this.has(key)) {
this.eventDispatcher.dispatch(CacheEvent.Written, { key, value, seconds });
}

return this.has(key);
}

Expand Down Expand Up @@ -183,6 +203,10 @@ export class MemoryCacheStore<K, T> implements CacheStore<K, T> {
public async forget(key: K): Promise<boolean> {
this.store.delete(key);

if (this.missing(key)) {
this.eventDispatcher.dispatch(CacheEvent.Forgotten, { key });
}

return this.missing(key);
}

Expand All @@ -206,6 +230,8 @@ export class MemoryCacheStore<K, T> implements CacheStore<K, T> {
public async flush(): Promise<boolean> {
this.store.clear();

this.eventDispatcher.dispatch(CacheEvent.Flushed);

return this.store.size === 0;
}

Expand Down

0 comments on commit c4a8b70

Please sign in to comment.