2019-10-03 21:34:12 +00:00
|
|
|
## 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.
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
import tables
|
2019-10-03 21:34:12 +00:00
|
|
|
import chronos, chronicles
|
|
|
|
|
|
|
|
logScope:
|
2020-06-10 08:48:01 +00:00
|
|
|
topics = "timedcache"
|
2019-10-03 21:34:12 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
const Timeout* = 10.seconds # default timeout in ms
|
2019-10-03 21:34:12 +00:00
|
|
|
|
|
|
|
type
|
2019-12-06 02:16:18 +00:00
|
|
|
ExpireHandler*[V] = proc(key: string, val: V) {.gcsafe.}
|
2019-10-03 21:34:12 +00:00
|
|
|
TimedEntry*[V] = object of RootObj
|
|
|
|
val: V
|
|
|
|
handler: ExpireHandler[V]
|
|
|
|
|
|
|
|
TimedCache*[V] = ref object of RootObj
|
|
|
|
cache*: Table[string, TimedEntry[V]]
|
|
|
|
onExpire*: ExpireHandler[V]
|
2019-12-06 02:16:18 +00:00
|
|
|
timeout*: Duration
|
2019-10-03 21:34:12 +00:00
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
# TODO: This belong in chronos, temporary left here until chronos is updated
|
|
|
|
proc addTimer*(at: Duration, cb: CallbackFunc, udata: pointer = nil) =
|
|
|
|
## Arrange for the callback ``cb`` to be called at the given absolute
|
|
|
|
## timestamp ``at``. You can also pass ``udata`` to callback.
|
|
|
|
addTimer(Moment.fromNow(at), cb, udata)
|
2019-10-03 21:34:12 +00:00
|
|
|
|
|
|
|
proc put*[V](t: TimedCache[V],
|
|
|
|
key: string,
|
|
|
|
val: V = "",
|
2019-12-06 02:16:18 +00:00
|
|
|
timeout: Duration,
|
2020-02-04 14:17:24 +00:00
|
|
|
handler: ExpireHandler[V] = nil) =
|
2019-12-06 02:16:18 +00:00
|
|
|
trace "adding entry to timed cache", key = key
|
2019-10-03 21:34:12 +00:00
|
|
|
t.cache[key] = TimedEntry[V](val: val, handler: handler)
|
|
|
|
|
|
|
|
addTimer(
|
|
|
|
timeout,
|
|
|
|
proc (arg: pointer = nil) {.gcsafe.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
trace "deleting expired entry from timed cache", key = key
|
|
|
|
if key in t.cache:
|
|
|
|
let entry = t.cache[key]
|
|
|
|
t.cache.del(key)
|
|
|
|
if not isNil(entry.handler):
|
|
|
|
entry.handler(key, entry.val)
|
2019-10-03 21:34:12 +00:00
|
|
|
)
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
proc put*[V](t: TimedCache[V],
|
|
|
|
key: string,
|
|
|
|
val: V = "",
|
|
|
|
handler: ExpireHandler[V] = nil) =
|
|
|
|
t.put(key, val, t.timeout, handler)
|
|
|
|
|
2020-02-04 14:17:24 +00:00
|
|
|
proc contains*[V](t: TimedCache[V], key: string): bool =
|
2019-10-03 21:34:12 +00:00
|
|
|
t.cache.contains(key)
|
|
|
|
|
|
|
|
proc del*[V](t: TimedCache[V], key: string) =
|
|
|
|
trace "deleting entry from timed cache", key = key
|
|
|
|
t.cache.del(key)
|
2019-12-06 02:16:18 +00:00
|
|
|
|
|
|
|
proc get*[V](t: TimedCache[V], key: string): V =
|
|
|
|
t.cache[key].val
|
|
|
|
|
|
|
|
proc `[]`*[V](t: TimedCache[V], key: string): V =
|
|
|
|
t.get(key)
|
|
|
|
|
|
|
|
proc `[]=`*[V](t: TimedCache[V], key: string, val: V): V =
|
|
|
|
t.put(key, val)
|
|
|
|
|
|
|
|
proc newTimedCache*[V](timeout: Duration = Timeout): TimedCache[V] =
|
|
|
|
new result
|
|
|
|
result.cache = initTable[string, TimedEntry[V]]()
|
|
|
|
result.timeout = timeout
|