cache.nim 705 B

12345678910111213141516171819202122232425262728
  1. import asyncdispatch, times
  2. import types, api
  3. withDb:
  4. try:
  5. createTables()
  6. except DbError:
  7. discard
  8. var profileCacheTime = initDuration(minutes=10)
  9. proc outdated(profile: Profile): bool =
  10. getTime() - profile.updated > profileCacheTime
  11. proc getCachedProfile*(username: string; force=false): Future[Profile] {.async.} =
  12. withDb:
  13. try:
  14. result.getOne("username = ?", username)
  15. doAssert not result.outdated()
  16. except AssertionError:
  17. var profile = await getProfile(username)
  18. profile.id = result.id
  19. result = profile
  20. result.update()
  21. except KeyError:
  22. result = await getProfile(username)
  23. if result.username.len > 0:
  24. result.insert()