Skip to content

Commit

Permalink
feat: implementing cache (not tested yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
yUnreal committed May 22, 2024
1 parent 5188d72 commit 27e5e3e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 5 deletions.
10 changes: 10 additions & 0 deletions src/managers/BaseCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Cache } from '../structs/Cache';
import { CacheOptions } from '../typings/model';

export abstract class BaseCache<K, V> {
public cache: Cache<K, V>;

public constructor(options: CacheOptions) {
this.cache = new Cache<K, V>(options);
}
}
15 changes: 15 additions & 0 deletions src/structs/Cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { CacheOptions } from '../typings/model';

export class Cache<K, V> extends Map<K, V> {
public constructor(public options: CacheOptions) {
super();
}

public set(key: K, value: V) {
if (this.size === this.options.limit) return this;

setTimeout(() => this.delete(key), this.options.lifetime);

return super.set(key, value);
}
}
39 changes: 34 additions & 5 deletions src/structs/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,19 @@ import uuid from 'uuid-random';
import { inspect } from 'util';
import { QueryManager } from '../managers/QueryManager';
import { ModelOptions } from '../typings/model';
import { BaseCache } from '../managers/BaseCache';

export class Model<S extends Schema<AnyObject>> extends BaseCache<
string,
Doc<Infer<S>>
> {
public constructor(public options: ModelOptions<S>) {
super(options.cache ?? { lifetime: Infinity });
}

export class Model<S extends Schema<AnyObject>> {
public constructor(public options: ModelOptions<S>) {}
protected isUsingCache() {
return Boolean(this.options.cache);
}

public get name() {
return this.options.name;
Expand Down Expand Up @@ -62,9 +72,19 @@ export class Model<S extends Schema<AnyObject>> {
});

if (!doc) return null;
if (projection) return new Doc(project(doc, projection), this);
if (projection) {
const createdDoc = new Doc(project(doc, projection), this);

if (this.isUsingCache()) this.cache.set(createdDoc.id, createdDoc);

return createdDoc;
}

const createdDoc = new Doc(doc, this);

return new Doc(doc, this);
if (this.isUsingCache()) this.cache.set(createdDoc.id, createdDoc);

return createdDoc;
}

public deleteOne(options: DeleteOneOptions<Infer<S>> | string) {
Expand All @@ -78,6 +98,8 @@ export class Model<S extends Schema<AnyObject>> {
return null;
}

this.cache.delete(doc.id);

return doc;
}

Expand All @@ -95,6 +117,8 @@ export class Model<S extends Schema<AnyObject>> {
return data;
});

// TODO: if (this.isUsingCache()) this.cache.set;

return;
}

Expand All @@ -111,7 +135,12 @@ export class Model<S extends Schema<AnyObject>> {
return crrData;
});

return new Doc(data, this);
// @ts-expect-error Fix this soon
const createdDoc = new Doc<Infer<S>>(data, this);

this.cache.set(createdDoc.id, createdDoc);

return createdDoc;
}

public createMany(...data: Infer<S>[]) {
Expand Down

0 comments on commit 27e5e3e

Please sign in to comment.