nautilus_infrastructure/python/redis/
cache.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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 bytes::Bytes;
17use nautilus_core::{
18    python::{to_pyruntime_err, to_pyvalue_err},
19    UUID4,
20};
21use nautilus_model::identifiers::TraderId;
22use pyo3::{prelude::*, types::PyBytes};
23
24use crate::redis::cache::RedisCacheDatabase;
25
26#[pymethods]
27impl RedisCacheDatabase {
28    #[new]
29    fn py_new(trader_id: TraderId, instance_id: UUID4, config_json: Vec<u8>) -> PyResult<Self> {
30        let config = serde_json::from_slice(&config_json).map_err(to_pyvalue_err)?;
31        Self::new(trader_id, instance_id, config).map_err(to_pyvalue_err)
32    }
33
34    #[pyo3(name = "close")]
35    fn py_close(&mut self) {
36        self.close()
37    }
38
39    #[pyo3(name = "flushdb")]
40    fn py_flushdb(&mut self) {
41        self.flushdb()
42    }
43
44    #[pyo3(name = "keys")]
45    fn py_keys(&mut self, pattern: &str) -> PyResult<Vec<String>> {
46        match self.keys(pattern) {
47            Ok(keys) => Ok(keys),
48            Err(e) => Err(to_pyruntime_err(e)),
49        }
50    }
51
52    #[pyo3(name = "read")]
53    fn py_read(&mut self, py: Python, key: &str) -> PyResult<Vec<PyObject>> {
54        match self.read(key) {
55            Ok(result) => {
56                let vec_py_bytes = result
57                    .into_iter()
58                    .map(|r| PyBytes::new(py, r.as_ref()).into())
59                    .collect::<Vec<PyObject>>();
60                Ok(vec_py_bytes)
61            }
62            Err(e) => Err(to_pyruntime_err(e)),
63        }
64    }
65
66    #[pyo3(name = "insert")]
67    fn py_insert(&mut self, key: String, payload: Vec<Vec<u8>>) -> PyResult<()> {
68        let payload: Vec<Bytes> = payload.into_iter().map(Bytes::from).collect();
69        self.insert(key, Some(payload)).map_err(to_pyvalue_err)
70    }
71
72    #[pyo3(name = "update")]
73    fn py_update(&mut self, key: String, payload: Vec<Vec<u8>>) -> PyResult<()> {
74        let payload: Vec<Bytes> = payload.into_iter().map(Bytes::from).collect();
75        self.update(key, Some(payload)).map_err(to_pyvalue_err)
76    }
77
78    #[pyo3(name = "delete")]
79    #[pyo3(signature = (key, payload=None))]
80    fn py_delete(&mut self, key: String, payload: Option<Vec<Vec<u8>>>) -> PyResult<()> {
81        let payload: Option<Vec<Bytes>> =
82            payload.map(|vec| vec.into_iter().map(Bytes::from).collect());
83        self.delete(key, payload).map_err(to_pyvalue_err)
84    }
85}