model_redis.nim 572 B

123456789101112131415161718192021222324
  1. import strutils, redis
  2. import model
  3. export model
  4. var db {.threadvar.}: TRedis
  5. proc init_db*() {.procvar.} =
  6. db = open(host="localhost")
  7. proc getWorld*(id: int): TWorld =
  8. let s = redis.get(db, "world:" & $id)
  9. if s == redisNil:
  10. raise newException(ERedis, "World Not Found")
  11. return (id, s.parseInt)
  12. proc updateWorld*(w: TWorld) =
  13. db.setk("world:" & $w.id, $w.randomNumber)
  14. proc getAllFortunes*(): seq[TFortune] =
  15. result = @[]
  16. var i = 1
  17. for s in db.lrange("fortunes", 0, -1):
  18. result.add((id: i, message: s))
  19. inc(i)