Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
shimizudev committed Nov 15, 2024
1 parent debfef5 commit 6c46231
Show file tree
Hide file tree
Showing 13 changed files with 600 additions and 542 deletions.
4 changes: 3 additions & 1 deletion src/cache/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export abstract class CacheAdapter extends EventEmitter {
abstract entries<T>(): Promise<[string, T][]>;

protected isExpired(entry: CacheEntry<unknown>): boolean {
if (!entry.expires) { return false; }
if (!entry.expires) {
return false;
}
return Date.now() > entry.expires;
}

Expand Down
234 changes: 121 additions & 113 deletions src/cache/default/array.ts
Original file line number Diff line number Diff line change
@@ -1,136 +1,144 @@
import { CacheAdapter, type CacheEntry, type CacheOptions } from "../adapter";
import { CacheAdapter, type CacheEntry, type CacheOptions } from '../adapter';

export class ArrayAdapter extends CacheAdapter {
private cache: CacheEntry<unknown>[];
private Keys: Map<string, number>;

constructor(options?: CacheOptions) {
super(options);
this.cache = [];
this.Keys = new Map();
private cache: CacheEntry<unknown>[];
private Keys: Map<string, number>;

constructor(options?: CacheOptions) {
super(options);
this.cache = [];
this.Keys = new Map();
}

public async init(): Promise<void> {
this.emit('cacheInitialized');
}

public async get<T>(key: string): Promise<T | undefined> {
const index = this.Keys.get(key);
if (index === undefined) {
return undefined;
}

const entry = this.cache[index] as CacheEntry<T>;
if (!entry) {
return undefined;
}

if (this.isExpired(entry)) {
this.cache.splice(index, 1);
this.Keys.delete(key);
this.updateIndices(index);
this.emit('cacheExpired', key);
return undefined;
}

return entry.value;
}

public async set<T>(
key: string,
value: T,
options?: CacheOptions
): Promise<void> {
const entry = this.createEntry(value, options);
const existingIndex = this.Keys.get(key);

if (existingIndex !== undefined) {
this.cache[existingIndex] = entry;
} else {
this.Keys.set(key, this.cache.length);
this.cache.push(entry);
}

this.emit('cacheSet', key, value);
}

public async has(key: string): Promise<boolean> {
const index = this.Keys.get(key);
if (index === undefined) {
return false;
}

const entry = this.cache[index];
if (!entry) {
return false;
}

public async init(): Promise<void> {
this.emit('cacheInitialized');

if (this.isExpired(entry)) {
this.cache.splice(index, 1);
this.Keys.delete(key);
this.updateIndices(index);
this.emit('cacheExpired', key);
return false;
}

return true;
}

public async delete(key: string): Promise<boolean> {
const index = this.Keys.get(key);
if (index !== undefined) {
this.cache.splice(index, 1);
this.Keys.delete(key);
this.updateIndices(index);
this.emit('cacheDelete', key);
return true;
}

public async get<T>(key: string): Promise<T | undefined> {
const index = this.Keys.get(key);
if (index === undefined) { return undefined; }

return false;
}

public async clear(): Promise<void> {
this.cache = [];
this.Keys.clear();
this.emit('cacheClear');
}

public async size(): Promise<number> {
return this.cache.length;
}

public async keys(): Promise<string[]> {
return Array.from(this.Keys.keys());
}

public async values<T>(): Promise<T[]> {
const values: T[] = [];
for (const [key, index] of this.Keys.entries()) {
const entry = this.cache[index] as CacheEntry<T>;
if (!entry) { return undefined; }

if (this.isExpired(entry)) {
this.cache.splice(index, 1);
this.Keys.delete(key);
this.updateIndices(index);
this.emit('cacheExpired', key);
return undefined;
}

return entry.value;
}

public async set<T>(
key: string,
value: T,
options?: CacheOptions
): Promise<void> {
const entry = this.createEntry(value, options);
const existingIndex = this.Keys.get(key);

if (existingIndex !== undefined) {
this.cache[existingIndex] = entry;
} else {
this.Keys.set(key, this.cache.length);
this.cache.push(entry);
values.push(entry.value);
}

this.emit('cacheSet', key, value);
}

public async has(key: string): Promise<boolean> {
const index = this.Keys.get(key);
if (index === undefined) { return false; }

const entry = this.cache[index];
if (!entry) { return false; }

return values;
}

public async entries<T>(): Promise<[string, T][]> {
const entries: [string, T][] = [];
for (const [key, index] of this.Keys.entries()) {
const entry = this.cache[index] as CacheEntry<T>;
if (this.isExpired(entry)) {
this.cache.splice(index, 1);
this.Keys.delete(key);
this.updateIndices(index);
this.emit('cacheExpired', key);
return false;
}

return true;
}

public async delete(key: string): Promise<boolean> {
const index = this.Keys.get(key);
if (index !== undefined) {
this.cache.splice(index, 1);
this.Keys.delete(key);
this.updateIndices(index);
this.emit('cacheDelete', key);
return true;
}
return false;
}

public async clear(): Promise<void> {
this.cache = [];
this.Keys.clear();
this.emit('cacheClear');
}

public async size(): Promise<number> {
return this.cache.length;
}

public async keys(): Promise<string[]> {
return Array.from(this.Keys.keys());
}

public async values<T>(): Promise<T[]> {
const values: T[] = [];
for (const [key, index] of this.Keys.entries()) {
const entry = this.cache[index] as CacheEntry<T>;
if (this.isExpired(entry)) {
this.cache.splice(index, 1);
this.Keys.delete(key);
this.updateIndices(index);
this.emit('cacheExpired', key);
} else {
values.push(entry.value);
}
}
return values;
}

public async entries<T>(): Promise<[string, T][]> {
const entries: [string, T][] = [];
for (const [key, index] of this.Keys.entries()) {
const entry = this.cache[index] as CacheEntry<T>;
if (this.isExpired(entry)) {
this.cache.splice(index, 1);
this.Keys.delete(key);
this.updateIndices(index);
this.emit('cacheExpired', key);
} else {
entries.push([key, entry.value]);
}
} else {
entries.push([key, entry.value]);
}
return entries;
}
return entries;
}

private updateIndices(deletedIndex: number): void {
for (const [key, index] of this.Keys.entries()) {
if (index > deletedIndex) {
this.Keys.set(key, index - 1);
}
private updateIndices(deletedIndex: number): void {
for (const [key, index] of this.Keys.entries()) {
if (index > deletedIndex) {
this.Keys.set(key, index - 1);
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/cache/default/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from './map';
export * from './object';
export * from './weak-map';
export * from './set';
export * from './array';
export * from './array';
Loading

0 comments on commit 6c46231

Please sign in to comment.