model_redis.nim 556 B

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