@iota-big3/sdk-security
Version:
Advanced security features including zero trust, quantum-safe crypto, and ML threat detection
100 lines • 3.03 kB
JavaScript
"use strict";
/**
* Cache Adapter for SDK Security
* Adapts various cache implementations to match our SecurityCacheContract
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.CacheAdapter = void 0;
class CacheAdapter {
constructor(cache) {
this.cache = cache;
}
/**
* Get value from cache
*/
async get(key) {
// Handle different cache interface patterns
if (typeof this.cache.get === 'function') {
return this.cache.get(key);
}
// Fallback for Map-like caches
if (typeof this.cache.has === 'function' && typeof this.cache.get === 'function') {
return this.cache.get(key);
}
return null;
}
/**
* Set value in cache
*/
async set(key, value, ttl) {
if (typeof this.cache.set === 'function') {
// Some caches expect TTL as third parameter
if (ttl !== undefined) {
await this.cache.set(key, value, ttl);
}
else {
await this.cache.set(key, value);
}
}
}
/**
* Delete keys by pattern
*/
async del(pattern) {
// Handle pattern-based deletion
if (pattern.includes('*')) {
// For caches that support pattern deletion
if (typeof this.cache.del === 'function') {
return this.cache.del(pattern);
}
// Manual pattern matching for simple caches
if (typeof this.cache.keys === 'function') {
const regex = new RegExp(pattern.replace(/\*/g, '.*'));
const keys = await this.cache.keys();
let deleted = 0;
for (const key of keys) {
if (regex.test(key)) {
await this.cache.delete(key);
deleted++;
}
}
return deleted;
}
return 0;
}
else {
// Single key deletion
if (typeof this.cache.del === 'function') {
return this.cache.del(pattern);
}
if (typeof this.cache.delete === 'function') {
const success = await this.cache.delete(pattern);
return success ? 1 : 0;
}
return 0;
}
}
/**
* Check if key exists
*/
async has(key) {
if (typeof this.cache.has === 'function') {
return this.cache.has(key);
}
const value = await this.get(key);
return value !== null && value !== undefined;
}
/**
* Clear all cache entries
*/
async clear() {
if (typeof this.cache.clear === 'function') {
return this.cache.clear();
}
if (typeof this.cache.flushAll === 'function') {
return this.cache.flushAll();
}
}
}
exports.CacheAdapter = CacheAdapter;
//# sourceMappingURL=cache.adapter.js.map