## Nim-LibP2P ## Copyright (c) 2019 Status Research & Development GmbH ## Licensed under either of ## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) ## * MIT license ([LICENSE-MIT](LICENSE-MIT)) ## at your option. ## This file may not be copied, modified, or distributed except according to ## those terms. import std/[heapqueue, sets] import chronos/timer const Timeout* = 10.seconds # default timeout in ms type TimedEntry*[K] = ref object of RootObj key: K expiresAt: Moment TimedCache*[K] = object of RootObj expiries: HeapQueue[TimedEntry[K]] entries: HashSet[K] timeout: Duration func `<`*(a, b: TimedEntry): bool = a.expiresAt < b.expiresAt func expire*(t: var TimedCache, now: Moment = Moment.now()) = while t.expiries.len() > 0 and t.expiries[0].expiresAt < now: t.entries.excl(t.expiries.pop().key) func del*[K](t: var TimedCache[K], key: K): bool = # Removes existing key from cache, returning false if it was not present if not t.entries.missingOrExcl(key): for i in 0..