

const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.js');
let lru_map = require("lru_map");
lru_map = require_rolldown_runtime.__toESM(lru_map);

//#region src/modules/cache/InMemoryLruCache.ts
/**
* In memory LRU cache.
*
* This cache can be used with multiple agent context instances, however all instances will share the same cache.
* If you need the cache to be isolated per agent context instance, make sure to use a different cache implementation.
*/
var InMemoryLruCache = class {
	constructor({ limit }) {
		this.cache = new lru_map.default.LRUMap(limit);
	}
	async get(_agentContext, key) {
		this.removeExpiredItems();
		const item = this.cache.get(key);
		if (!item) return null;
		return item.value;
	}
	async set(_agentContext, key, value, expiresInSeconds) {
		this.removeExpiredItems();
		let expiresDate;
		if (expiresInSeconds) {
			expiresDate = /* @__PURE__ */ new Date();
			expiresDate.setSeconds(expiresDate.getSeconds() + expiresInSeconds);
		}
		this.cache.set(key, {
			expiresAt: expiresDate?.getTime(),
			value
		});
	}
	clear() {
		this.cache.clear();
	}
	async remove(_agentContext, key) {
		this.removeExpiredItems();
		this.cache.delete(key);
	}
	removeExpiredItems() {
		this.cache.forEach((value, key) => {
			if (value.expiresAt && Date.now() > value.expiresAt) this.cache.delete(key);
		});
	}
};

//#endregion
exports.InMemoryLruCache = InMemoryLruCache;