File size: 1,011 Bytes
89eb9ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from redis import Redis
from valkey import Valkey
from typing import _ProtocolMeta
from restful.configs import settings, Settings


""" caching_connector """
def _caching_connector(db_type: str, env_var: Settings) -> _ProtocolMeta:
  if db_type == 'redis':
    return Redis(
      host     = env_var.CACHING_HOST,
      port     = env_var.CACHING_PORT,
      password = env_var.CACHING_PASS,
      ssl      = True
    )
  
  elif db_type == 'valkey':
    return Valkey(
      host     = env_var.CACHING_HOST,
      port     = env_var.CACHING_PORT,
      password = env_var.CACHING_PASS,
      ssl      = True
    )

  # default
  else:
    return Redis(
      host     = env_var.CACHING_HOST,
      port     = env_var.CACHING_PORT,
      password = env_var.CACHING_PASS,
      ssl      = True
    )

connector: _ProtocolMeta = _caching_connector(
  db_type = settings.CACHING_TYPE, env_var = settings)

async def close_caching_connector() -> None:
    global connector
    if connector: await connector.close()