Skip to content

Commit

Permalink
fix(@desktop/wallet): guarded accessing values by key of template cac…
Browse files Browse the repository at this point in the history
…he class
  • Loading branch information
saledjenic committed Apr 6, 2023
1 parent 14423df commit 2d3ff3e
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/app_service/common/cache.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ type TimedCache*[T] = Table[string, Value[T]]

proc newTimedCache*[T](): TimedCache[T] = initTable[string, Value[T]]()

proc init*[T](self: var TimedCache[T], values: Table[string, T]) =
self.clear()
for cacheKey, value in values:
self.set(cacheKey, value)

proc getTimestamp[T](self: TimedCache[T], cacheKey: string): DateTime = self[cacheKey].timestamp
proc getTimestamp[T](self: TimedCache[T], cacheKey: string): DateTime =
if self.hasKey(cacheKey):
return self[cacheKey].timestamp

proc isCached*[T](self: TimedCache[T], cacheKey: string, duration=initDuration(minutes = 5)): bool =
self.hasKey(cacheKey) and ((self.getTimestamp(cacheKey) + duration) >= now())

proc set*[T](self: var TimedCache[T], cacheKey: string, value: T) =
self[cacheKey] = Value[T](value: value, timestamp: now())

proc get*[T](self: TimedCache[T], cacheKey: string): T = self[cacheKey].value
proc get*[T](self: TimedCache[T], cacheKey: string): T =
if self.hasKey(cacheKey):
return self[cacheKey].value

0 comments on commit 2d3ff3e

Please sign in to comment.