@dbs-portal/core-api
Version:
HTTP client and API utilities for DBS Portal
199 lines • 4.77 kB
JavaScript
/**
* In-memory cache storage
*/
/**
* In-memory cache storage implementation
*/
export class MemoryCache {
cache = new Map();
/**
* Gets a cache entry
*/
get(key) {
const entry = this.cache.get(key);
return entry ? entry : null;
}
/**
* Sets a cache entry
*/
set(key, entry) {
this.cache.set(key, entry);
}
/**
* Deletes a cache entry
*/
delete(key) {
this.cache.delete(key);
}
/**
* Clears all cache entries
*/
clear() {
this.cache.clear();
}
/**
* Gets the number of cached entries
*/
size() {
return this.cache.size;
}
/**
* Gets all cache keys
*/
keys() {
return Array.from(this.cache.keys());
}
/**
* Gets all cache values
*/
values() {
return Array.from(this.cache.values());
}
/**
* Gets all cache entries
*/
entries() {
return Array.from(this.cache.entries());
}
/**
* Checks if a key exists in cache
*/
has(key) {
return this.cache.has(key);
}
/**
* Iterates over cache entries
*/
forEach(callback) {
this.cache.forEach(callback);
}
/**
* Gets cache statistics
*/
getStats() {
let memoryUsage = 0;
// Estimate memory usage (rough calculation)
for (const [key, entry] of this.cache.entries()) {
memoryUsage += key.length * 2; // UTF-16 characters
memoryUsage += this.estimateObjectSize(entry);
}
return {
size: this.cache.size,
memoryUsage,
};
}
/**
* Estimates the size of an object in bytes
*/
estimateObjectSize(obj) {
let size = 0;
if (obj === null || obj === undefined) {
return 0;
}
if (typeof obj === 'string') {
return obj.length * 2; // UTF-16
}
if (typeof obj === 'number') {
return 8; // 64-bit number
}
if (typeof obj === 'boolean') {
return 4;
}
if (Array.isArray(obj)) {
for (const item of obj) {
size += this.estimateObjectSize(item);
}
return size;
}
if (typeof obj === 'object') {
for (const [key, value] of Object.entries(obj)) {
size += key.length * 2; // Key size
size += this.estimateObjectSize(value); // Value size
}
return size;
}
return 0;
}
/**
* Cleans up expired entries
*/
cleanup() {
let cleaned = 0;
const now = Date.now();
for (const [key, entry] of this.cache.entries()) {
if (now - entry.timestamp > entry.ttl) {
this.cache.delete(key);
cleaned++;
}
}
return cleaned;
}
/**
* Gets entries sorted by timestamp (oldest first)
*/
getEntriesByAge() {
return Array.from(this.cache.entries()).sort(([, a], [, b]) => a.timestamp - b.timestamp);
}
/**
* Removes oldest entries to free up space
*/
evictOldest(count) {
const evicted = [];
const sortedEntries = this.getEntriesByAge();
for (let i = 0; i < count && i < sortedEntries.length; i++) {
const [key] = sortedEntries[i];
this.cache.delete(key);
evicted.push(key);
}
return evicted;
}
/**
* Sets maximum cache size with LRU eviction
*/
setMaxSize(maxSize) {
if (this.cache.size > maxSize) {
const toEvict = this.cache.size - maxSize;
this.evictOldest(toEvict);
}
}
/**
* Gets cache hit ratio (requires tracking)
*/
getHitRatio() {
// This would require additional tracking of hits/misses
// For now, return 0 as placeholder
return 0;
}
/**
* Exports cache data for serialization
*/
export() {
const exported = {};
for (const [key, entry] of this.cache.entries()) {
exported[key] = entry;
}
return exported;
}
/**
* Imports cache data from serialized format
*/
import(data) {
this.cache.clear();
for (const [key, entry] of Object.entries(data)) {
this.cache.set(key, entry);
}
}
/**
* Creates a snapshot of current cache state
*/
snapshot() {
return new Map(this.cache);
}
/**
* Restores cache from a snapshot
*/
restore(snapshot) {
this.cache = new Map(snapshot);
}
}
//# sourceMappingURL=memory-cache.js.map