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