Skip to main content

nautilus_infrastructure/python/redis/
cache.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use nautilus_common::{
17    cache::database::CacheDatabaseFactory,
18    python::cache::get_global_cache_database_factory_registry,
19};
20use nautilus_core::python::to_pyruntime_err;
21use pyo3::prelude::*;
22
23use crate::redis::cache::RedisCacheConfig;
24
25#[pymethods]
26#[pyo3_stub_gen::derive::gen_stub_pymethods]
27impl RedisCacheConfig {
28    /// Configuration for a Redis-backed cache database.
29    ///
30    /// Redis 6.2 or higher is required for correct operation.
31    #[new]
32    #[expect(clippy::too_many_arguments)]
33    #[pyo3(signature = (host=None, port=None, username=None, password=None, ssl=None, connection_timeout=None, response_timeout=None, number_of_retries=None, exponent_base=None, max_delay=None, factor=None))]
34    fn py_new(
35        host: Option<String>,
36        port: Option<u16>,
37        username: Option<String>,
38        password: Option<String>,
39        ssl: Option<bool>,
40        connection_timeout: Option<u16>,
41        response_timeout: Option<u16>,
42        number_of_retries: Option<usize>,
43        exponent_base: Option<u64>,
44        max_delay: Option<u64>,
45        factor: Option<u64>,
46    ) -> Self {
47        let default = Self::default();
48        Self {
49            host,
50            port,
51            username,
52            password,
53            ssl: ssl.unwrap_or(default.ssl),
54            connection_timeout: connection_timeout.unwrap_or(default.connection_timeout),
55            response_timeout: response_timeout.unwrap_or(default.response_timeout),
56            number_of_retries: number_of_retries.unwrap_or(default.number_of_retries),
57            exponent_base: exponent_base.unwrap_or(default.exponent_base),
58            max_delay: max_delay.unwrap_or(default.max_delay),
59            factor: factor.unwrap_or(default.factor),
60        }
61    }
62
63    #[getter]
64    fn host(&self) -> Option<&str> {
65        self.host.as_deref()
66    }
67
68    #[getter]
69    const fn port(&self) -> Option<u16> {
70        self.port
71    }
72
73    #[getter]
74    fn username(&self) -> Option<&str> {
75        self.username.as_deref()
76    }
77
78    #[getter]
79    fn password(&self) -> Option<&str> {
80        self.password.as_deref()
81    }
82
83    #[getter]
84    const fn ssl(&self) -> bool {
85        self.ssl
86    }
87
88    #[getter]
89    const fn connection_timeout(&self) -> u16 {
90        self.connection_timeout
91    }
92
93    #[getter]
94    const fn response_timeout(&self) -> u16 {
95        self.response_timeout
96    }
97
98    #[getter]
99    const fn number_of_retries(&self) -> usize {
100        self.number_of_retries
101    }
102
103    #[getter]
104    const fn exponent_base(&self) -> u64 {
105        self.exponent_base
106    }
107
108    #[getter]
109    const fn max_delay(&self) -> u64 {
110        self.max_delay
111    }
112
113    #[getter]
114    const fn factor(&self) -> u64 {
115        self.factor
116    }
117}
118
119#[expect(clippy::needless_pass_by_value)]
120fn extract_redis_cache_database_factory(
121    py: Python<'_>,
122    factory: Py<PyAny>,
123) -> PyResult<Box<dyn CacheDatabaseFactory>> {
124    Ok(Box::new(factory.extract::<RedisCacheConfig>(py)?))
125}
126
127pub(in crate::python) fn register_redis_cache_database_factory() -> PyResult<()> {
128    get_global_cache_database_factory_registry()
129        .register(
130            stringify!(RedisCacheConfig).to_string(),
131            extract_redis_cache_database_factory,
132        )
133        .map_err(to_pyruntime_err)
134}