|
18 | 18 | """Renku service cache management.""" |
19 | 19 | import json |
20 | 20 | import os |
| 21 | +from typing import Any, Dict |
21 | 22 |
|
22 | 23 | import redis |
23 | 24 | from redis import RedisError |
24 | 25 | from walrus import Database |
25 | 26 |
|
26 | | -from renku.ui.service.cache.config import REDIS_DATABASE, REDIS_HOST, REDIS_NAMESPACE, REDIS_PASSWORD, REDIS_PORT |
| 27 | +from renku.ui.service.cache.config import ( |
| 28 | + REDIS_DATABASE, |
| 29 | + REDIS_HOST, |
| 30 | + REDIS_IS_SENTINEL, |
| 31 | + REDIS_MASTER_SET, |
| 32 | + REDIS_NAMESPACE, |
| 33 | + REDIS_PASSWORD, |
| 34 | + REDIS_PORT, |
| 35 | +) |
| 36 | + |
| 37 | +_config: Dict[str, Any] = { |
| 38 | + "db": REDIS_DATABASE, |
| 39 | + "password": REDIS_PASSWORD, |
| 40 | + "retry_on_timeout": True, |
| 41 | + "health_check_interval": int(os.getenv("CACHE_HEALTH_CHECK_INTERVAL", 60)), |
| 42 | +} |
| 43 | + |
| 44 | +if REDIS_IS_SENTINEL: |
| 45 | + _sentinel = redis.Sentinel([(REDIS_HOST, REDIS_PORT)], sentinel_kwargs={"password": REDIS_PASSWORD}) |
| 46 | + _cache = _sentinel.master_for(REDIS_MASTER_SET, **_config) |
| 47 | + _model_db = Database(connection_pool=_cache.connection_pool, **_config) |
| 48 | +else: |
| 49 | + _cache = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, **_config) # type: ignore |
| 50 | + _model_db = Database(host=REDIS_HOST, port=REDIS_PORT, **_config) |
27 | 51 |
|
28 | 52 |
|
29 | 53 | class BaseCache: |
30 | 54 | """Cache management.""" |
31 | 55 |
|
32 | | - config_ = { |
33 | | - "host": REDIS_HOST, |
34 | | - "port": REDIS_PORT, |
35 | | - "db": REDIS_DATABASE, |
36 | | - "password": REDIS_PASSWORD, |
37 | | - "retry_on_timeout": True, |
38 | | - "health_check_interval": int(os.getenv("CACHE_HEALTH_CHECK_INTERVAL", 60)), |
39 | | - } |
40 | | - |
41 | | - cache = redis.Redis(**config_) # type: ignore |
42 | | - model_db = Database(**config_) |
| 56 | + cache: redis.Redis = _cache |
| 57 | + model_db: Database = _model_db |
43 | 58 | namespace = REDIS_NAMESPACE |
44 | 59 |
|
45 | 60 | def set_record(self, name, key, value): |
|
0 commit comments