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