nautilus_common/cache/
config.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 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")
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    /// If a 'trader-' prefix is used for keys.
37    pub use_trader_prefix: bool,
38    /// If the trader's instance ID is used for keys.
39    pub use_instance_id: bool,
40    /// If the database should be flushed on start.
41    pub flush_on_start: bool,
42    /// If instrument data should be dropped from the cache's memory on reset.
43    pub drop_instruments_on_reset: bool,
44    /// The maximum length for internal tick deques.
45    pub tick_capacity: usize,
46    /// The maximum length for internal bar deques.
47    pub bar_capacity: usize,
48    /// If market data should be persisted to disk.
49    pub save_market_data: bool,
50}
51
52impl Default for CacheConfig {
53    /// Creates a new default [`CacheConfig`] instance.
54    fn default() -> Self {
55        Self {
56            database: None,
57            encoding: SerializationEncoding::MsgPack,
58            timestamps_as_iso8601: false,
59            buffer_interval_ms: None,
60            use_trader_prefix: true,
61            use_instance_id: false,
62            flush_on_start: false,
63            drop_instruments_on_reset: true,
64            tick_capacity: 10_000,
65            bar_capacity: 10_000,
66            save_market_data: false,
67        }
68    }
69}
70
71impl CacheConfig {
72    /// Creates a new [`CacheConfig`] instance.
73    #[allow(clippy::too_many_arguments)]
74    #[must_use]
75    pub const fn new(
76        database: Option<DatabaseConfig>,
77        encoding: SerializationEncoding,
78        timestamps_as_iso8601: bool,
79        buffer_interval_ms: Option<usize>,
80        use_trader_prefix: bool,
81        use_instance_id: bool,
82        flush_on_start: bool,
83        drop_instruments_on_reset: bool,
84        tick_capacity: usize,
85        bar_capacity: usize,
86        save_market_data: bool,
87    ) -> Self {
88        Self {
89            database,
90            encoding,
91            timestamps_as_iso8601,
92            buffer_interval_ms,
93            use_trader_prefix,
94            use_instance_id,
95            flush_on_start,
96            drop_instruments_on_reset,
97            tick_capacity,
98            bar_capacity,
99            save_market_data,
100        }
101    }
102}