redis_cache.nim 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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(l: List): string = toLower("l:" & l.username & '/' & l.name)
  23. template to(s: string; typ: typedesc): untyped =
  24. var res: typ
  25. if s.len > 0:
  26. s.unpack(res)
  27. res
  28. proc get(query: string): Future[string] {.async.} =
  29. pool.withAcquire(r):
  30. result = await r.get(query)
  31. proc setex(key: string; time: int; data: string) {.async.} =
  32. pool.withAcquire(r):
  33. discard await r.setex(key, time, data)
  34. proc cache*(data: List) {.async.} =
  35. await setex(data.toKey, listCacheTime, data.pack)
  36. proc cache*(data: PhotoRail; id: string) {.async.} =
  37. await setex("pr:" & id, baseCacheTime, data.pack)
  38. proc cache*(data: Profile) {.async.} =
  39. if data.username.len == 0: return
  40. pool.withAcquire(r):
  41. r.startPipelining()
  42. discard await r.setex(data.toKey, baseCacheTime, pack(data))
  43. discard await r.hset("p:", toLower(data.username), data.id)
  44. discard await r.flushPipeline()
  45. proc cacheProfileId*(username, id: string) {.async.} =
  46. if username.len == 0 or id.len == 0: return
  47. pool.withAcquire(r):
  48. discard await r.hset("p:", toLower(username), id)
  49. proc cacheRss*(query, rss, cursor: string) {.async.} =
  50. let key = "rss:" & query
  51. pool.withAcquire(r):
  52. r.startPipelining()
  53. discard await r.hset(key, "rss", rss)
  54. discard await r.hset(key, "min", cursor)
  55. discard await r.expire(key, rssCacheTime)
  56. discard await r.flushPipeline()
  57. proc getProfileId*(username: string): Future[string] {.async.} =
  58. pool.withAcquire(r):
  59. result = await r.hget("p:", toLower(username))
  60. if result == redisNil:
  61. result.setLen(0)
  62. proc getCachedProfile*(username: string; fetch=true): Future[Profile] {.async.} =
  63. let prof = await get("p:" & toLower(username))
  64. if prof != redisNil:
  65. result = prof.to(Profile)
  66. elif fetch:
  67. result = await getProfile(username)
  68. proc getCachedPhotoRail*(id: string): Future[PhotoRail] {.async.} =
  69. if id.len == 0: return
  70. let rail = await get("pr:" & toLower(id))
  71. if rail != redisNil:
  72. result = rail.to(PhotoRail)
  73. else:
  74. result = await getPhotoRail(id)
  75. await cache(result, id)
  76. proc getCachedList*(username=""; name=""; id=""): Future[List] {.async.} =
  77. let list = if id.len > 0: redisNil
  78. else: await get(toLower("l:" & username & '/' & name))
  79. if list != redisNil:
  80. result = list.to(List)
  81. else:
  82. if id.len > 0:
  83. result = await getGraphListById(id)
  84. else:
  85. result = await getGraphList(username, name)
  86. await cache(result)
  87. proc getCachedRss*(key: string): Future[(string, string)] {.async.} =
  88. var res: Table[string, string]
  89. pool.withAcquire(r):
  90. res = await r.hgetall("rss:" & key)
  91. if "rss" in res:
  92. result = (res["rss"], res.getOrDefault("min"))