redis_cache.nim 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. pool = waitFor newRedisPool(cfg.redisConns, maxConns=cfg.redisMaxConns,
  16. host=cfg.redisHost, port=cfg.redisPort)
  17. template toKey(p: Profile): string = "p:" & toLower(p.username)
  18. template toKey(v: Video): string = "v:" & v.videoId
  19. template toKey(c: Card): string = "c:" & c.id
  20. template toKey(l: List): string = toLower("l:" & l.username & '/' & l.name)
  21. template toKey(t: Token): string = "t:" & t.tok
  22. template to(s: string; typ: typedesc): untyped =
  23. var res: typ
  24. if s.len > 0:
  25. s.unpack(res)
  26. res
  27. proc get(query: string): Future[string] {.async.} =
  28. pool.withAcquire(r):
  29. result = await r.get(query)
  30. proc uncache*(id: int64) {.async.} =
  31. pool.withAcquire(r):
  32. discard await r.del("v:" & $id)
  33. proc cache*[T](data: T; time=baseCacheTime) {.async.} =
  34. pool.withAcquire(r):
  35. discard await r.setex(data.toKey, time, pack(data))
  36. proc cache*(data: PhotoRail; id: string) {.async.} =
  37. pool.withAcquire(r):
  38. discard await r.setex("pr:" & id, baseCacheTime, pack(data))
  39. proc cache*(data: Profile; time=baseCacheTime) {.async.} =
  40. pool.withAcquire(r):
  41. r.startPipelining()
  42. discard await r.setex(data.toKey, time, pack(data))
  43. discard await r.hset("p:", toLower(data.username), data.id)
  44. discard await r.flushPipeline()
  45. proc cacheRss*(query, rss, cursor: string) {.async.} =
  46. let key = "rss:" & query
  47. pool.withAcquire(r):
  48. r.startPipelining()
  49. await r.hmset(key, @[("rss", rss), ("min", cursor)])
  50. discard await r.expire(key, rssCacheTime)
  51. discard await r.flushPipeline()
  52. proc getProfileId*(username: string): Future[string] {.async.} =
  53. pool.withAcquire(r):
  54. result = await r.hget("p:", toLower(username))
  55. if result == redisNil:
  56. result.setLen(0)
  57. proc hasCachedProfile*(username: string): Future[Option[Profile]] {.async.} =
  58. let prof = await get("p:" & toLower(username))
  59. if prof != redisNil:
  60. result = some prof.to(Profile)
  61. proc getCachedProfile*(username: string; fetch=true): Future[Profile] {.async.} =
  62. let prof = await get("p:" & toLower(username))
  63. if prof != redisNil:
  64. result = prof.to(Profile)
  65. else:
  66. result = await getProfile(username)
  67. if result.id.len > 0:
  68. await cache(result)
  69. proc getCachedPhotoRail*(id: string): Future[PhotoRail] {.async.} =
  70. if id.len == 0: return
  71. let rail = await get("pr:" & toLower(id))
  72. if rail != redisNil:
  73. result = rail.to(PhotoRail)
  74. else:
  75. result = await getPhotoRail(id)
  76. await cache(result, id)
  77. proc getCachedList*(username=""; name=""; id=""): Future[List] {.async.} =
  78. let list = if id.len > 0: redisNil
  79. else: await get(toLower("l:" & username & '/' & name))
  80. if list != redisNil:
  81. result = list.to(List)
  82. else:
  83. if id.len > 0:
  84. result = await getGraphListById(id)
  85. else:
  86. result = await getGraphList(username, name)
  87. await cache(result, time=listCacheTime)
  88. proc getCachedRss*(key: string): Future[(string, string)] {.async.} =
  89. var res: Table[string, string]
  90. pool.withAcquire(r):
  91. res = await r.hgetall("rss:" & key)
  92. if "rss" in res:
  93. result = (res["rss"], res["min"])