-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
debfef5
commit 6c46231
Showing
13 changed files
with
600 additions
and
542 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.