redis_cache.nim 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import asyncdispatch, times, strutils, 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) {.async.} =
  15. try:
  16. pool = await newRedisPool(cfg.redisConns, maxConns=cfg.redisMaxConns,
  17. host=cfg.redisHost, port=cfg.redisPort)
  18. pool.withAcquire(r):
  19. let snappyRss = await r.get("snappyRss")
  20. if snappyRss == redisNil:
  21. let list = await r.scan(newCursor(0), "rss:*", 10000)
  22. r.startPipelining()
  23. for rss in list:
  24. discard await r.del(rss)
  25. discard await r.flushPipeline()
  26. await r.setk("snappyRss", "true")
  27. except OSError:
  28. echo "Failed to connect to Redis."
  29. quit(1)
  30. template toKey(p: Profile): string = "p:" & toLower(p.username)
  31. template toKey(l: List): string = toLower("l:" & l.username & '/' & l.name)
  32. template to(s: string; typ: typedesc): untyped =
  33. var res: typ
  34. if s.len > 0:
  35. s.unpack(res)
  36. res
  37. proc get(query: string): Future[string] {.async.} =
  38. pool.withAcquire(r):
  39. result = await r.get(query)
  40. proc setex(key: string; time: int; data: string) {.async.} =
  41. pool.withAcquire(r):
  42. discard await r.setex(key, time, data)
  43. proc cache*(data: List) {.async.} =
  44. await setex(data.toKey, listCacheTime, data.pack)
  45. proc cache*(data: PhotoRail; id: string) {.async.} =
  46. await setex("pr:" & id, baseCacheTime, data.pack)
  47. proc cache*(data: Profile) {.async.} =
  48. if data.username.len == 0: return
  49. pool.withAcquire(r):
  50. r.startPipelining()
  51. discard await r.setex(data.toKey, baseCacheTime, pack(data))
  52. discard await r.hset("p:", toLower(data.username), data.id)
  53. discard await r.flushPipeline()
  54. proc cacheProfileId*(username, id: string) {.async.} =
  55. if username.len == 0 or id.len == 0: return
  56. pool.withAcquire(r):
  57. discard await r.hset("p:", toLower(username), id)
  58. proc cacheRss*(query: string; rss: Rss) {.async.} =
  59. let key = "rss:" & query
  60. pool.withAcquire(r):
  61. r.startPipelining()
  62. discard await r.hset(key, "rss", rss.feed)
  63. discard await r.hset(key, "min", rss.cursor)
  64. discard await r.expire(key, rssCacheTime)
  65. discard await r.flushPipeline()
  66. proc getProfileId*(username: string): Future[string] {.async.} =
  67. pool.withAcquire(r):
  68. result = await r.hget("p:", toLower(username))
  69. if result == redisNil:
  70. result.setLen(0)
  71. proc getCachedProfile*(username: string; fetch=true): Future[Profile] {.async.} =
  72. let prof = await get("p:" & toLower(username))
  73. if prof != redisNil:
  74. result = prof.to(Profile)
  75. elif fetch:
  76. result = await getProfile(username)
  77. proc getCachedPhotoRail*(id: string): Future[PhotoRail] {.async.} =
  78. if id.len == 0: return
  79. let rail = await get("pr:" & toLower(id))
  80. if rail != redisNil:
  81. result = rail.to(PhotoRail)
  82. else:
  83. result = await getPhotoRail(id)
  84. await cache(result, id)
  85. proc getCachedList*(username=""; name=""; id=""): Future[List] {.async.} =
  86. let list = if id.len > 0: redisNil
  87. else: await get(toLower("l:" & username & '/' & name))
  88. if list != redisNil:
  89. result = list.to(List)
  90. else:
  91. if id.len > 0:
  92. result = await getGraphListById(id)
  93. else:
  94. result = await getGraphList(username, name)
  95. await cache(result)
  96. proc getCachedRss*(key: string): Future[Rss] {.async.} =
  97. let k = "rss:" & key
  98. pool.withAcquire(r):
  99. result.cursor = await r.hget(k, "min")
  100. if result.cursor.len > 2:
  101. result.feed = await r.hget(k, "rss")
  102. else:
  103. result.cursor.setLen 0