import asyncdispatch, times import types, api withDb: try: createTables() except DbError: discard var profileCacheTime = initDuration(minutes=10) proc outdated(profile: Profile): bool = getTime() - profile.updated > profileCacheTime proc getCachedProfile*(username, agent: string; force=false): Future[Profile] {.async.} = withDb: try: result.getOne("username = ?", username) doAssert not result.outdated() except AssertionError: var profile = await getProfile(username, agent) profile.id = result.id result = profile result.update() except KeyError: result = await getProfile(username, agent) if result.username.len > 0: result.insert() proc setProfileCacheTime*(minutes: int) = profileCacheTime = initDuration(minutes=minutes)