asyncredis.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from typing import Any
  4. import aioredis
  5. from core.config import settings
  6. # 同步Redis
  7. # class RedisCli(object):
  8. #
  9. # def __init__(self,
  10. # *,
  11. # host: str,
  12. # port: int = 6379,
  13. # password: str = "",
  14. # db: int = 0,
  15. # socket_timeout: int = 5):
  16. # self._redis_client = None
  17. # self.host = host
  18. # self.port = port
  19. # self.password = password
  20. # self.db = db
  21. # self.socket_timeout = socket_timeout
  22. #
  23. # def init_redis_connect(self) -> None:
  24. # """
  25. # 初始化连接
  26. # :return:
  27. # """
  28. # try:
  29. # self._redis_client = redis.Redis(host=self.host,
  30. # port=self.port,
  31. # password=self.password,
  32. # socket_timeout=self.socket_timeout,
  33. # decode_responses=True)
  34. # if not self._redis_client.ping():
  35. # print("Connect Redis Timeout!")
  36. # sys.exit()
  37. # except (redis.AuthenticationError, Exception) as ex:
  38. # print(f"Connect Redis Error: {str(ex)}")
  39. # sys.exit()
  40. #
  41. # def __getattr__(self, item):
  42. # return getattr(self._redis_client, item)
  43. #
  44. # def __getitem__(self, item):
  45. # return self._redis_client[item]
  46. #
  47. # def __setitem__(self, key, value):
  48. # self._redis_client[key] = value
  49. #
  50. # def __delitem__(self, key):
  51. # del self._redis_client[key]
  52. #
  53. #
  54. # redis_client = RedisCli(host=settings.REDIS_HOST,
  55. # port=settings.REDIS_PORT,
  56. # password=settings.REDIS_PASSWORD,
  57. # db=settings.REDIS_DB)
  58. # 异步Redis
  59. class AsyncRedis(object):
  60. def __init__(self,
  61. host: str,
  62. port: int = 6379,
  63. password: str = "",
  64. db: int = 0):
  65. self._redis = None
  66. self.host = host
  67. self.port = port
  68. self.password = password
  69. self.db = db
  70. async def get_redis_pool(self):
  71. if not self._redis:
  72. params = {
  73. "address": f"redis://{self.host}:{self.port}",
  74. "db": self.db
  75. }
  76. if self.password:
  77. params["password"] = self.password
  78. self._redis = await aioredis.create_redis_pool(**params)
  79. return self._redis
  80. async def close(self):
  81. if self._redis:
  82. print("Close Redis Pool!!")
  83. self._redis.close()
  84. await self._redis.wait_closed()
  85. async def set(self, key: str, value: Any, *, expire: int = 9):
  86. r = await self.get_redis_pool()
  87. await r.set(key, value, expire=expire)
  88. async def get(self, key: str):
  89. r = await self.get_redis_pool()
  90. val = await r.get(key)
  91. return val
  92. async def delete(self, key: str):
  93. r = await self.get_redis_pool()
  94. await r.delete(key)
  95. redis_client = AsyncRedis(host=settings.REDIS_HOST,
  96. port=settings.REDIS_PORT,
  97. password=settings.REDIS_PASSWORD,
  98. db=settings.REDIS_DB)
  99. __all__ = ["redis_client"]