Skip to main content

nautilus_common/cache/
config.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 serde::{Deserialize, Serialize};
17
18use crate::{enums::SerializationEncoding, msgbus::database::DatabaseConfig};
19
20/// Configuration for `Cache` instances.
21#[cfg_attr(
22    feature = "python",
23    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.common", from_py_object)
24)]
25#[cfg_attr(
26    feature = "python",
27    pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.common")
28)]
29#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, bon::Builder)]
30#[serde(default)]
31pub struct CacheConfig {
32    /// The configuration for the cache backing database.
33    pub database: Option<DatabaseConfig>,
34    /// The encoding for database operations, controls the type of serializer used.
35    #[builder(default = SerializationEncoding::MsgPack)]
36    pub encoding: SerializationEncoding,
37    /// If timestamps should be persisted as ISO 8601 strings.
38    #[builder(default)]
39    pub timestamps_as_iso8601: bool,
40    /// The buffer interval (milliseconds) between pipelined/batched transactions.
41    pub buffer_interval_ms: Option<usize>,
42    /// The batch size for bulk read operations (e.g., MGET).
43    /// If set, bulk reads will be batched into chunks of this size.
44    pub bulk_read_batch_size: Option<usize>,
45    /// If a 'trader-' prefix is used for keys.
46    #[builder(default = true)]
47    pub use_trader_prefix: bool,
48    /// If the trader's instance ID is used for keys.
49    #[builder(default)]
50    pub use_instance_id: bool,
51    /// If the database should be flushed on start.
52    #[builder(default)]
53    pub flush_on_start: bool,
54    /// If instrument data should be dropped from the cache's memory on reset.
55    #[builder(default = true)]
56    pub drop_instruments_on_reset: bool,
57    /// The maximum length for internal tick deques.
58    #[builder(default = 10_000)]
59    pub tick_capacity: usize,
60    /// The maximum length for internal bar deques.
61    #[builder(default = 10_000)]
62    pub bar_capacity: usize,
63    /// If market data should be persisted to disk.
64    #[builder(default)]
65    pub save_market_data: bool,
66}
67
68impl Default for CacheConfig {
69    fn default() -> Self {
70        Self::builder().build()
71    }
72}
73
74impl CacheConfig {
75    /// Creates a new [`CacheConfig`] instance.
76    #[allow(clippy::too_many_arguments)]
77    #[must_use]
78    pub const fn new(
79        database: Option<DatabaseConfig>,
80        encoding: SerializationEncoding,
81        timestamps_as_iso8601: bool,
82        buffer_interval_ms: Option<usize>,
83        bulk_read_batch_size: Option<usize>,
84        use_trader_prefix: bool,
85        use_instance_id: bool,
86        flush_on_start: bool,
87        drop_instruments_on_reset: bool,
88        tick_capacity: usize,
89        bar_capacity: usize,
90        save_market_data: bool,
91    ) -> Self {
92        Self {
93            database,
94            encoding,
95            timestamps_as_iso8601,
96            buffer_interval_ms,
97            bulk_read_batch_size,
98            use_trader_prefix,
99            use_instance_id,
100            flush_on_start,
101            drop_instruments_on_reset,
102            tick_capacity,
103            bar_capacity,
104            save_market_data,
105        }
106    }
107}