Skip to content

Commit

Permalink
Merge pull request #29 from medishen/dev/v2
Browse files Browse the repository at this point in the history
The packages are synced according to the new version of @medishn/qiks.
  • Loading branch information
m-mdy-m authored Feb 7, 2025
2 parents 6726e03 + cc734f6 commit cce56e5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 33 deletions.
13 changes: 4 additions & 9 deletions packages/cache/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { Qiks } from '@medishn/qiks';
import { CacheConfigQiks } from '@medishn/qiks/dist/types/CacheTypes';

/**
* A caching system built on top of @medishn/qiks.
*/
export class MemoryCacheStore<K, V> extends Qiks<K, V> {
constructor(options?: CacheConfigQiks<K>) {
super(options);
import { CacheConfig, Qiks } from '@medishn/qiks';
export class InMemoryCacheStore<K, V> extends Qiks<K, V> {
constructor(option: CacheConfig<K, V>) {
super(option);
}
}
3 changes: 3 additions & 0 deletions packages/cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
},
"devDependencies": {
"typescript": "^5.5.4"
},
"dependencies": {
"@medishn/qiks": "2.0.0"
}
}
19 changes: 13 additions & 6 deletions packages/metadata/interface/metadata.interface.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { MetadataKey, MetadataTarget, MetadataValue } from '../types/metadata.types';

export interface MetadataStorage {
get<K extends MetadataKey, V>(target: MetadataTarget): Map<MetadataKey<K>, MetadataValue<V>>;
set<K extends MetadataKey, V>(target: MetadataTarget, key: MetadataKey<K>, value: MetadataValue<V>): void;
has(target: MetadataTarget, key: MetadataKey): boolean;
delete(target: MetadataTarget, key: MetadataKey): boolean;
// Define the MetadataStorage interface
export interface MetadataStorage<K extends MetadataKey, V extends MetadataValue> {
get(target: MetadataTarget): Map<K, V>;

set(target: MetadataTarget, key: K, value: V): void;

has(target: MetadataTarget, key: K): boolean;

delete(target: MetadataTarget, key: K): boolean;

clear(target: MetadataTarget): void;

keys(): string[];
list<K extends MetadataKey, V>(target: MetadataTarget): Map<MetadataKey<K>, MetadataValue<V>> | null;

list(target: MetadataTarget): Map<K, V> | null;
}
37 changes: 20 additions & 17 deletions packages/metadata/storage/storage-metadata.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { isClass } from '@gland/common/utils';
import { MemoryCacheStore } from '@gland/cache';
import { InMemoryCacheStore } from '@gland/cache';
import { MetadataStorage } from '../interface/metadata.interface';
import { MetadataKey, MetadataTarget, MetadataValue } from '../types/metadata.types';
import { MAXIMUM_CACHE_SIZE } from '@gland/common';

class ReflectStorage implements MetadataStorage {
private storage = new MemoryCacheStore<string, Map<MetadataKey, MetadataValue<any>>>();
class ReflectStorage<K extends MetadataKey = MetadataKey, V extends MetadataValue = MetadataValue> implements MetadataStorage<K, V> {
private storage: InMemoryCacheStore<string, Map<K, V>>;
constructor() {
this.storage = new MemoryCacheStore({
policy: 'LRU',
this.storage = new InMemoryCacheStore({
maxSize: MAXIMUM_CACHE_SIZE,
evictionPolicy: 'LRU',
storage: 'map',
});
}
private getTargetIdentifier(target: MetadataTarget): string {
Expand All @@ -20,34 +23,34 @@ class ReflectStorage implements MetadataStorage {
return `instance:${target.constructor.name}`;
}

private ensureMetadataMap<K extends MetadataKey, V>(target: MetadataTarget): Map<MetadataKey<K>, MetadataValue<V>> {
private ensureMetadataMap(target: MetadataTarget): Map<K, V> {
const targetKey = this.getTargetIdentifier(target);
let metadataMap = this.storage.get(targetKey);
if (!(metadataMap instanceof Map)) {
metadataMap = new Map<MetadataKey, MetadataValue<V>>();
if (!metadataMap) {
metadataMap = new Map<K, V>();
this.storage.set(targetKey, metadataMap);
}
return metadataMap as Map<K, V>;
return metadataMap;
}
get<K extends MetadataKey, V>(target: MetadataTarget): Map<MetadataKey<K>, MetadataValue<V>> {
get(target: MetadataTarget): Map<K, V> {
const targetKey = this.getTargetIdentifier(target);
const metadataMap = this.storage.get(targetKey);
return metadataMap instanceof Map ? (metadataMap as Map<K, V>) : new Map<MetadataKey<K>, MetadataValue<V>>();
return metadataMap instanceof Map ? metadataMap : new Map<K, V>();
}

set<K extends MetadataKey, V>(target: MetadataTarget, key: MetadataKey<K>, value: MetadataValue<V>): void {
set(target: MetadataTarget, key: K, value: V): void {
const metadataMap = this.ensureMetadataMap(target);
metadataMap.set(key, value);
const targetKey = this.getTargetIdentifier(target);
this.storage.set(targetKey, metadataMap);
}

has(target: MetadataTarget, key: MetadataKey): boolean {
has(target: MetadataTarget, key: K): boolean {
const metadataMap = this.get(target);
return metadataMap.has(key);
}

delete(target: MetadataTarget, key: MetadataKey): boolean {
delete(target: MetadataTarget, key: K): boolean {
const targetKey = this.getTargetIdentifier(target);
const metadataMap = this.get(target);
const result = metadataMap.delete(key);
Expand All @@ -64,11 +67,11 @@ class ReflectStorage implements MetadataStorage {
this.storage.delete(targetKey);
}

list<K extends MetadataKey, V>(target: MetadataTarget): Map<MetadataKey<K>, MetadataValue<V>> | null {
list(target: MetadataTarget): Map<K, V> | null {
const targetKey = this.getTargetIdentifier(target);
const metadataMap = this.storage.get(targetKey);
if (metadataMap instanceof Map) {
return metadataMap as Map<K, V>;
if (metadataMap) {
return metadataMap;
}
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/metadata/types/metadata.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MetadataScope } from '../constant';
export type MetadataKey<K = string | symbol> = K;
export type MetadataValue<T extends unknown> = T;
export type MetadataValue<T = unknown> = T;
export type MetadataTarget = object | Function;
export type MetadataPropertyKey = string | symbol;
export type MetadataParameterIndex = number;
Expand Down

0 comments on commit cce56e5

Please sign in to comment.