|
1 | 1 | from valkey.cluster import ValkeyCluster
|
2 | 2 | from valkey.typing import KeyT, EncodableT
|
3 | 3 |
|
4 |
| -from django_valkey.base_client import BaseClient, _main_exceptions |
| 4 | +from django_valkey.base_client import _main_exceptions |
| 5 | +from django_valkey.client.default import SyncClientMethod |
5 | 6 | from django_valkey.exceptions import ConnectionInterrupted
|
6 | 7 |
|
7 | 8 |
|
8 |
| -class DefaultClusterClient(BaseClient): |
| 9 | +class DefaultClusterClient(SyncClientMethod): |
9 | 10 | CONNECTION_FACTORY_PATH = (
|
10 | 11 | "django_valkey.cluster_cache.pool.ClusterConnectionFactory"
|
11 | 12 | )
|
12 | 13 |
|
| 14 | + def _get_client(self, write=True, tried=None, client=None) -> ValkeyCluster: |
| 15 | + if client: |
| 16 | + return client |
| 17 | + return self.get_client(write=write, tried=tried) |
| 18 | + |
| 19 | + def get_client(self, write=True, tried=None) -> ValkeyCluster: |
| 20 | + index = self.get_next_client_index(write=write, tried=tried) |
| 21 | + |
| 22 | + if self._clients[index] is None: |
| 23 | + self._clients[index] = self.connect(index) |
| 24 | + |
| 25 | + return self._clients[index] |
| 26 | + |
| 27 | + def get_client_with_index( |
| 28 | + self, write=True, tried=None |
| 29 | + ) -> tuple[ValkeyCluster, int]: |
| 30 | + index = self.get_next_client_index(write=write, tried=tried) |
| 31 | + if self._clients[index] is None: |
| 32 | + self._clients[index] = self.connect(index) |
| 33 | + |
| 34 | + return self._clients[index], index |
| 35 | + |
| 36 | + def connect(self, index: int = 0) -> ValkeyCluster: |
| 37 | + return self.connection_factory.connect(self._server[index]) |
| 38 | + |
| 39 | + def disconnect(self, index: int = 0, client: ValkeyCluster | None = None) -> None: |
| 40 | + """ |
| 41 | + delegates the connection factory to disconnect the client |
| 42 | + """ |
| 43 | + if client is None: |
| 44 | + client = self._clients[index] |
| 45 | + |
| 46 | + if client is not None: |
| 47 | + self.connection_factory.disconnect(client) |
| 48 | + |
13 | 49 | def readonly(self, target_nodes=None, client=None):
|
14 | 50 | client = self._get_client(write=True, client=client)
|
15 | 51 | return client.readonly(target_nodes)
|
|
0 commit comments