redis_cache.nim 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import asyncdispatch, times, strutils, options, tables
  2. import redis, redpool, msgpack4nim
  3. export redpool, msgpack4nim
  4. import types, api
  5. const redisNil = "\0\0"
  6. var
  7. pool: RedisPool
  8. baseCacheTime = 60 * 60
  9. rssCacheTime: int
  10. listCacheTime*: int
  11. proc setCacheTimes*(cfg: Config) =
  12. rssCacheTime = cfg.rssCacheTime * 60
  13. listCacheTime = cfg.listCacheTime * 60
  14. proc initRedisPool*(cfg: Config) =
  15. try:
  16. pool = waitFor newRedisPool(cfg.redisConns, maxConns=cfg.redisMaxConns,
  17. host=cfg.redisHost, port=cfg.redisPort)
  18. except OSError:
  19. echo "Failed to connect to Redis."
  20. quit(1)
  21. template toKey(p: Profile): string = "p:" & toLower(p.username)
  22. template toKey(v: Video): string = "v:" & v.videoId
  23. template toKey(c: Card): string = "c:" & c.id
  24. template toKey(l: List): string = toLower("l:" & l.username & '/' & l.name)
  25. template toKey(t: Token): string = "t:" & t.tok
  26. template to(s: string; typ: typedesc): untyped =
  27. var res: typ
  28. if s.len > 0:
  29. s.unpack(res)
  30. res
  31. proc get(query: string): Future[string] {.async.} =
  32. pool.withAcquire(r):
  33. result = await r.get(query)
  34. proc uncache*(id: int64) {.async.} =
  35. pool.withAcquire(r):
  36. discard await r.del("v:" & $id)
  37. proc cache*[T](data: T; time=baseCacheTime) {.async.} =
  38. pool.withAcquire(r):
  39. discard await r.setex(data.toKey, time, pack(data))
  40. proc cache*(data: PhotoRail; id: string) {.async.} =
  41. pool.withAcquire(r):
  42. discard await r.setex("pr:" & id, baseCacheTime, pack(data))
  43. proc cache*(data: Profile; time=baseCacheTime) {.async.} =
  44. pool.withAcquire(r):
  45. r.startPipelining()
  46. discard await r.setex(data.toKey, time, pack(data))
  47. discard await r.hset("p:", toLower(data.username), data.id)
  48. discard await r.flushPipeline()
  49. proc cacheRss*(query, rss, cursor: string) {.async.} =
  50. let key = "rss:" & query
  51. pool.withAcquire(r):
  52. r.startPipelining()
  53. await r.hmset(key, @[("rss", rss), ("min", cursor)])
  54. discard await r.expire(key, rssCacheTime)
  55. discard await r.flushPipeline()
  56. proc getProfileId*(username: string): Future[string] {.async.} =
  57. pool.withAcquire(r):
  58. result = await r.hget("p:", toLower(username))
  59. if result == redisNil:
  60. result.setLen(0)
  61. proc hasCachedProfile*(username: string): Future[Option[Profile]] {.async.} =
  62. let prof = await get("p:" & toLower(username))
  63. if prof != redisNil:
  64. result = some prof.to(Profile)
  65. proc getCachedProfile*(username: string; fetch=true): Future[Profile] {.async.} =
  66. let prof = await get("p:" & toLower(username))
  67. if prof != redisNil:
  68. result = prof.to(Profile)
  69. else:
  70. result = await getProfile(username)
  71. if result.id.len > 0:
  72. await cache(result)
  73. proc getCachedPhotoRail*(id: string): Future[PhotoRail] {.async.} =
  74. if id.len == 0: return
  75. let rail = await get("pr:" & toLower(id))
  76. if rail != redisNil:
  77. result = rail.to(PhotoRail)
  78. else:
  79. result = await getPhotoRail(id)
  80. await cache(result, id)
  81. proc getCachedList*(username=""; name=""; id=""): Future[List] {.async.} =
  82. let list = if id.len > 0: redisNil
  83. else: await get(toLower("l:" & username & '/' & name))
  84. if list != redisNil:
  85. result = list.to(List)
  86. else:
  87. if id.len > 0:
  88. result = await getGraphListById(id)
  89. else:
  90. result = await getGraphList(username, name)
  91. await cache(result, time=listCacheTime)
  92. proc getCachedRss*(key: string): Future[(string, string)] {.async.} =
  93. var res: Table[string, string]
  94. pool.withAcquire(r):
  95. res = await r.hgetall("rss:" & key)
  96. if "rss" in res:
  97. result = (res["rss"], res.getOrDefault("min"))