Skip to main content

nautilus_common/cache/
api.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
16//! User-facing read API over the platform cache.
17
18use std::{
19    cell::{Ref, RefCell},
20    fmt::Display,
21};
22
23use ahash::AHashSet;
24use bytes::Bytes;
25#[cfg(feature = "defi")]
26use nautilus_model::defi::{Pool, PoolProfiler};
27use nautilus_model::{
28    accounts::AccountAny,
29    data::{
30        Bar, BarType, CustomData, DataType, FundingRateUpdate, GreeksData, IndexPriceUpdate,
31        InstrumentClose, InstrumentStatus, MarkPriceUpdate, QuoteTick, TradeTick,
32        option_chain::OptionGreeks,
33    },
34    enums::{AggregationSource, InstrumentClass, OmsType, OrderSide, PositionSide, PriceType},
35    identifiers::{
36        AccountId, ClientId, ClientOrderId, ExecAlgorithmId, InstrumentId, OrderListId, PositionId,
37        StrategyId, Venue, VenueOrderId,
38    },
39    instruments::{InstrumentAny, SyntheticInstrument},
40    orderbook::{OrderBook, own::OwnOrderBook},
41    orders::{OrderAny, OrderList},
42    position::Position,
43    types::{Currency, Money, Price, Quantity},
44};
45use rust_decimal::Decimal;
46use ustr::Ustr;
47
48use super::{
49    Cache,
50    error::{
51        AccountLookupError, CurrencyLookupError, InstrumentLookupError, OrderBookLookupError,
52        OrderListLookupError, OrderLookupError, OwnOrderBookLookupError, PositionLookupError,
53        SyntheticInstrumentLookupError,
54    },
55};
56use crate::component::ComponentAccessError;
57
58/// User-facing cache API.
59///
60/// Point reads return owned snapshots where possible, so actor code does not retain a `Ref` into
61/// the live [`Cache`]. Plural collection reads return owned snapshots of all matching values and
62/// are intentionally named as bulk reads. Prefer the count, ID, or `has_*` methods in hot paths
63/// when a full snapshot is not needed.
64#[derive(Debug)]
65pub struct CacheApi<'a> {
66    cache: &'a RefCell<Cache>,
67}
68
69impl<'a> CacheApi<'a> {
70    pub(crate) fn new(cache: &'a RefCell<Cache>) -> Self {
71        Self { cache }
72    }
73
74    /// Returns the unrealized PnL for the `position` using cached market data.
75    ///
76    /// # Panics
77    ///
78    /// Panics if the cache is already mutably borrowed.
79    #[must_use]
80    pub fn calculate_unrealized_pnl(&self, position: &Position) -> Option<Money> {
81        self.cache().calculate_unrealized_pnl(position)
82    }
83
84    /// Returns the OMS type for the `position_id` (if known).
85    ///
86    /// # Panics
87    ///
88    /// Panics if the cache is already mutably borrowed.
89    #[must_use]
90    pub fn oms_type(&self, position_id: &PositionId) -> Option<OmsType> {
91        self.cache().oms_type(position_id)
92    }
93
94    /// Returns serialized position snapshot frames for the `position_id`.
95    ///
96    /// # Panics
97    ///
98    /// Panics if the cache is already mutably borrowed.
99    #[must_use]
100    pub fn position_snapshot_bytes(&self, position_id: &PositionId) -> Option<Vec<Vec<u8>>> {
101        self.cache().position_snapshot_bytes(position_id)
102    }
103
104    /// Returns the number of stored position snapshots for the `position_id`.
105    ///
106    /// # Panics
107    ///
108    /// Panics if the cache is already mutably borrowed.
109    #[must_use]
110    pub fn position_snapshot_count(&self, position_id: &PositionId) -> usize {
111        self.cache().position_snapshot_count(position_id)
112    }
113
114    /// Returns position snapshots matching the optional filters.
115    ///
116    /// # Panics
117    ///
118    /// Panics if the cache is already mutably borrowed.
119    #[must_use]
120    pub fn position_snapshots(
121        &self,
122        position_id: Option<&PositionId>,
123        account_id: Option<&AccountId>,
124    ) -> Vec<Position> {
125        self.cache().position_snapshots(position_id, account_id)
126    }
127
128    /// Returns position snapshots for `position_id` starting from `skip`.
129    ///
130    /// # Panics
131    ///
132    /// Panics if the cache is already mutably borrowed.
133    #[must_use]
134    pub fn position_snapshots_from(&self, position_id: &PositionId, skip: usize) -> Vec<Position> {
135        self.cache().position_snapshots_from(position_id, skip)
136    }
137
138    /// Returns position snapshot IDs for the `instrument_id`.
139    ///
140    /// # Panics
141    ///
142    /// Panics if the cache is already mutably borrowed.
143    #[must_use]
144    pub fn position_snapshot_ids(&self, instrument_id: &InstrumentId) -> AHashSet<PositionId> {
145        self.cache().position_snapshot_ids(instrument_id)
146    }
147
148    /// Returns the client order IDs of all orders matching the optional filter parameters.
149    ///
150    /// # Panics
151    ///
152    /// Panics if the cache is already mutably borrowed.
153    #[must_use]
154    pub fn client_order_ids(
155        &self,
156        venue: Option<&Venue>,
157        instrument_id: Option<&InstrumentId>,
158        strategy_id: Option<&StrategyId>,
159        account_id: Option<&AccountId>,
160    ) -> AHashSet<ClientOrderId> {
161        self.cache()
162            .client_order_ids(venue, instrument_id, strategy_id, account_id)
163    }
164
165    /// Returns the client order IDs of all open orders matching the optional filter parameters.
166    ///
167    /// # Panics
168    ///
169    /// Panics if the cache is already mutably borrowed.
170    #[must_use]
171    pub fn client_order_ids_open(
172        &self,
173        venue: Option<&Venue>,
174        instrument_id: Option<&InstrumentId>,
175        strategy_id: Option<&StrategyId>,
176        account_id: Option<&AccountId>,
177    ) -> AHashSet<ClientOrderId> {
178        self.cache()
179            .client_order_ids_open(venue, instrument_id, strategy_id, account_id)
180    }
181
182    /// Returns the client order IDs of all closed orders matching the optional filter parameters.
183    ///
184    /// # Panics
185    ///
186    /// Panics if the cache is already mutably borrowed.
187    #[must_use]
188    pub fn client_order_ids_closed(
189        &self,
190        venue: Option<&Venue>,
191        instrument_id: Option<&InstrumentId>,
192        strategy_id: Option<&StrategyId>,
193        account_id: Option<&AccountId>,
194    ) -> AHashSet<ClientOrderId> {
195        self.cache()
196            .client_order_ids_closed(venue, instrument_id, strategy_id, account_id)
197    }
198
199    /// Returns the client order IDs of all locally active orders matching the optional filter parameters.
200    ///
201    /// # Panics
202    ///
203    /// Panics if the cache is already mutably borrowed.
204    #[must_use]
205    pub fn client_order_ids_active_local(
206        &self,
207        venue: Option<&Venue>,
208        instrument_id: Option<&InstrumentId>,
209        strategy_id: Option<&StrategyId>,
210        account_id: Option<&AccountId>,
211    ) -> AHashSet<ClientOrderId> {
212        self.cache()
213            .client_order_ids_active_local(venue, instrument_id, strategy_id, account_id)
214    }
215
216    /// Returns the client order IDs of all emulated orders matching the optional filter parameters.
217    ///
218    /// # Panics
219    ///
220    /// Panics if the cache is already mutably borrowed.
221    #[must_use]
222    pub fn client_order_ids_emulated(
223        &self,
224        venue: Option<&Venue>,
225        instrument_id: Option<&InstrumentId>,
226        strategy_id: Option<&StrategyId>,
227        account_id: Option<&AccountId>,
228    ) -> AHashSet<ClientOrderId> {
229        self.cache()
230            .client_order_ids_emulated(venue, instrument_id, strategy_id, account_id)
231    }
232
233    /// Returns the client order IDs of all in-flight orders matching the optional filter parameters.
234    ///
235    /// # Panics
236    ///
237    /// Panics if the cache is already mutably borrowed.
238    #[must_use]
239    pub fn client_order_ids_inflight(
240        &self,
241        venue: Option<&Venue>,
242        instrument_id: Option<&InstrumentId>,
243        strategy_id: Option<&StrategyId>,
244        account_id: Option<&AccountId>,
245    ) -> AHashSet<ClientOrderId> {
246        self.cache()
247            .client_order_ids_inflight(venue, instrument_id, strategy_id, account_id)
248    }
249
250    /// Returns the position IDs of all positions matching the optional filter parameters.
251    ///
252    /// # Panics
253    ///
254    /// Panics if the cache is already mutably borrowed.
255    #[must_use]
256    pub fn position_ids(
257        &self,
258        venue: Option<&Venue>,
259        instrument_id: Option<&InstrumentId>,
260        strategy_id: Option<&StrategyId>,
261        account_id: Option<&AccountId>,
262    ) -> AHashSet<PositionId> {
263        self.cache()
264            .position_ids(venue, instrument_id, strategy_id, account_id)
265    }
266
267    /// Returns the position IDs of all open positions matching the optional filter parameters.
268    ///
269    /// # Panics
270    ///
271    /// Panics if the cache is already mutably borrowed.
272    #[must_use]
273    pub fn position_open_ids(
274        &self,
275        venue: Option<&Venue>,
276        instrument_id: Option<&InstrumentId>,
277        strategy_id: Option<&StrategyId>,
278        account_id: Option<&AccountId>,
279    ) -> AHashSet<PositionId> {
280        self.cache()
281            .position_open_ids(venue, instrument_id, strategy_id, account_id)
282    }
283
284    /// Returns the position IDs of all closed positions matching the optional filter parameters.
285    ///
286    /// # Panics
287    ///
288    /// Panics if the cache is already mutably borrowed.
289    #[must_use]
290    pub fn position_closed_ids(
291        &self,
292        venue: Option<&Venue>,
293        instrument_id: Option<&InstrumentId>,
294        strategy_id: Option<&StrategyId>,
295        account_id: Option<&AccountId>,
296    ) -> AHashSet<PositionId> {
297        self.cache()
298            .position_closed_ids(venue, instrument_id, strategy_id, account_id)
299    }
300
301    /// Returns the strategy IDs in the cache.
302    ///
303    /// # Panics
304    ///
305    /// Panics if the cache is already mutably borrowed.
306    #[must_use]
307    pub fn strategy_ids(&self) -> AHashSet<StrategyId> {
308        self.cache().strategy_ids()
309    }
310
311    /// Returns the execution algorithm IDs in the cache.
312    ///
313    /// # Panics
314    ///
315    /// Panics if the cache is already mutably borrowed.
316    #[must_use]
317    pub fn exec_algorithm_ids(&self) -> AHashSet<ExecAlgorithmId> {
318        self.cache().exec_algorithm_ids()
319    }
320
321    /// Returns an owned copy of the order for the `client_order_id` (if found).
322    ///
323    /// # Panics
324    ///
325    /// Panics if the cache is already mutably borrowed.
326    #[must_use]
327    pub fn order(&self, client_order_id: &ClientOrderId) -> Option<OrderAny> {
328        self.cache().order_owned(client_order_id)
329    }
330
331    /// Returns an owned copy of the order for the `client_order_id`.
332    ///
333    /// # Errors
334    ///
335    /// Returns an error if:
336    /// - The order is not present in the cache.
337    /// - The cache is already mutably borrowed.
338    pub fn try_order(&self, client_order_id: &ClientOrderId) -> Result<OrderAny, OrderLookupError> {
339        self.try_cache("try_order")?
340            .try_order_owned(client_order_id)
341    }
342
343    /// Returns owned copies of the orders for `client_order_ids`.
344    ///
345    /// # Panics
346    ///
347    /// Panics if the cache is already mutably borrowed.
348    #[must_use]
349    pub fn orders_for_ids(
350        &self,
351        client_order_ids: &[ClientOrderId],
352        context: &dyn Display,
353    ) -> Vec<OrderAny> {
354        self.cache().orders_for_ids(client_order_ids, context)
355    }
356
357    /// Returns the client order ID for the `venue_order_id` (if found).
358    ///
359    /// # Panics
360    ///
361    /// Panics if the cache is already mutably borrowed.
362    #[must_use]
363    pub fn client_order_id(&self, venue_order_id: &VenueOrderId) -> Option<ClientOrderId> {
364        self.cache().client_order_id(venue_order_id).copied()
365    }
366
367    /// Returns the venue order ID for the `client_order_id` (if found).
368    ///
369    /// # Panics
370    ///
371    /// Panics if the cache is already mutably borrowed.
372    #[must_use]
373    pub fn venue_order_id(&self, client_order_id: &ClientOrderId) -> Option<VenueOrderId> {
374        self.cache().venue_order_id(client_order_id).copied()
375    }
376
377    /// Returns the client ID indexed for the `client_order_id` (if found).
378    ///
379    /// # Panics
380    ///
381    /// Panics if the cache is already mutably borrowed.
382    #[must_use]
383    pub fn client_id(&self, client_order_id: &ClientOrderId) -> Option<ClientId> {
384        self.cache().client_id(client_order_id).copied()
385    }
386
387    /// Returns owned copies of all orders matching the optional filter parameters.
388    ///
389    /// # Panics
390    ///
391    /// Panics if the cache is already mutably borrowed.
392    #[must_use]
393    pub fn orders(
394        &self,
395        venue: Option<&Venue>,
396        instrument_id: Option<&InstrumentId>,
397        strategy_id: Option<&StrategyId>,
398        account_id: Option<&AccountId>,
399        side: Option<OrderSide>,
400    ) -> Vec<OrderAny> {
401        self.cache()
402            .orders_refs(venue, instrument_id, strategy_id, account_id, side)
403            .into_iter()
404            .map(|order| order.cloned())
405            .collect()
406    }
407
408    /// Returns owned copies of all open orders matching the optional filter parameters.
409    ///
410    /// # Panics
411    ///
412    /// Panics if the cache is already mutably borrowed.
413    #[must_use]
414    pub fn orders_open(
415        &self,
416        venue: Option<&Venue>,
417        instrument_id: Option<&InstrumentId>,
418        strategy_id: Option<&StrategyId>,
419        account_id: Option<&AccountId>,
420        side: Option<OrderSide>,
421    ) -> Vec<OrderAny> {
422        self.cache()
423            .orders_open_refs(venue, instrument_id, strategy_id, account_id, side)
424            .into_iter()
425            .map(|order| order.cloned())
426            .collect()
427    }
428
429    /// Returns owned copies of all closed orders matching the optional filter parameters.
430    ///
431    /// # Panics
432    ///
433    /// Panics if the cache is already mutably borrowed.
434    #[must_use]
435    pub fn orders_closed(
436        &self,
437        venue: Option<&Venue>,
438        instrument_id: Option<&InstrumentId>,
439        strategy_id: Option<&StrategyId>,
440        account_id: Option<&AccountId>,
441        side: Option<OrderSide>,
442    ) -> Vec<OrderAny> {
443        self.cache()
444            .orders_closed_refs(venue, instrument_id, strategy_id, account_id, side)
445            .into_iter()
446            .map(|order| order.cloned())
447            .collect()
448    }
449
450    /// Returns owned copies of all locally active orders matching the optional filter parameters.
451    ///
452    /// # Panics
453    ///
454    /// Panics if the cache is already mutably borrowed.
455    #[must_use]
456    pub fn orders_active_local(
457        &self,
458        venue: Option<&Venue>,
459        instrument_id: Option<&InstrumentId>,
460        strategy_id: Option<&StrategyId>,
461        account_id: Option<&AccountId>,
462        side: Option<OrderSide>,
463    ) -> Vec<OrderAny> {
464        self.cache()
465            .orders_active_local_refs(venue, instrument_id, strategy_id, account_id, side)
466            .into_iter()
467            .map(|order| order.cloned())
468            .collect()
469    }
470
471    /// Returns owned copies of all emulated orders matching the optional filter parameters.
472    ///
473    /// # Panics
474    ///
475    /// Panics if the cache is already mutably borrowed.
476    #[must_use]
477    pub fn orders_emulated(
478        &self,
479        venue: Option<&Venue>,
480        instrument_id: Option<&InstrumentId>,
481        strategy_id: Option<&StrategyId>,
482        account_id: Option<&AccountId>,
483        side: Option<OrderSide>,
484    ) -> Vec<OrderAny> {
485        self.cache()
486            .orders_emulated_refs(venue, instrument_id, strategy_id, account_id, side)
487            .into_iter()
488            .map(|order| order.cloned())
489            .collect()
490    }
491
492    /// Returns owned copies of all in-flight orders matching the optional filter parameters.
493    ///
494    /// # Panics
495    ///
496    /// Panics if the cache is already mutably borrowed.
497    #[must_use]
498    pub fn orders_inflight(
499        &self,
500        venue: Option<&Venue>,
501        instrument_id: Option<&InstrumentId>,
502        strategy_id: Option<&StrategyId>,
503        account_id: Option<&AccountId>,
504        side: Option<OrderSide>,
505    ) -> Vec<OrderAny> {
506        self.cache()
507            .orders_inflight_refs(venue, instrument_id, strategy_id, account_id, side)
508            .into_iter()
509            .map(|order| order.cloned())
510            .collect()
511    }
512
513    /// Returns owned copies of all orders for the `position_id`.
514    ///
515    /// # Panics
516    ///
517    /// Panics if the cache is already mutably borrowed.
518    #[must_use]
519    pub fn orders_for_position(&self, position_id: &PositionId) -> Vec<OrderAny> {
520        self.cache()
521            .orders_for_position(position_id)
522            .into_iter()
523            .map(|order| order.cloned())
524            .collect()
525    }
526
527    /// Returns whether an order with the `client_order_id` exists.
528    ///
529    /// # Panics
530    ///
531    /// Panics if the cache is already mutably borrowed.
532    #[must_use]
533    pub fn order_exists(&self, client_order_id: &ClientOrderId) -> bool {
534        self.cache().order_exists(client_order_id)
535    }
536
537    /// Returns whether an order with the `client_order_id` is open.
538    ///
539    /// # Panics
540    ///
541    /// Panics if the cache is already mutably borrowed.
542    #[must_use]
543    pub fn is_order_open(&self, client_order_id: &ClientOrderId) -> bool {
544        self.cache().is_order_open(client_order_id)
545    }
546
547    /// Returns whether an order with the `client_order_id` is closed.
548    ///
549    /// # Panics
550    ///
551    /// Panics if the cache is already mutably borrowed.
552    #[must_use]
553    pub fn is_order_closed(&self, client_order_id: &ClientOrderId) -> bool {
554        self.cache().is_order_closed(client_order_id)
555    }
556
557    /// Returns whether an order with the `client_order_id` is locally active.
558    ///
559    /// # Panics
560    ///
561    /// Panics if the cache is already mutably borrowed.
562    #[must_use]
563    pub fn is_order_active_local(&self, client_order_id: &ClientOrderId) -> bool {
564        self.cache().is_order_active_local(client_order_id)
565    }
566
567    /// Returns whether an order with the `client_order_id` is emulated.
568    ///
569    /// # Panics
570    ///
571    /// Panics if the cache is already mutably borrowed.
572    #[must_use]
573    pub fn is_order_emulated(&self, client_order_id: &ClientOrderId) -> bool {
574        self.cache().is_order_emulated(client_order_id)
575    }
576
577    /// Returns whether an order with the `client_order_id` is in-flight.
578    ///
579    /// # Panics
580    ///
581    /// Panics if the cache is already mutably borrowed.
582    #[must_use]
583    pub fn is_order_inflight(&self, client_order_id: &ClientOrderId) -> bool {
584        self.cache().is_order_inflight(client_order_id)
585    }
586
587    /// Returns whether an order with the `client_order_id` is `PENDING_CANCEL` locally.
588    ///
589    /// # Panics
590    ///
591    /// Panics if the cache is already mutably borrowed.
592    #[must_use]
593    pub fn is_order_pending_cancel_local(&self, client_order_id: &ClientOrderId) -> bool {
594        self.cache().is_order_pending_cancel_local(client_order_id)
595    }
596
597    /// Returns the count of all open orders matching the optional filter parameters.
598    ///
599    /// # Panics
600    ///
601    /// Panics if the cache is already mutably borrowed.
602    #[must_use]
603    pub fn orders_open_count(
604        &self,
605        venue: Option<&Venue>,
606        instrument_id: Option<&InstrumentId>,
607        strategy_id: Option<&StrategyId>,
608        account_id: Option<&AccountId>,
609        side: Option<OrderSide>,
610    ) -> usize {
611        self.cache()
612            .orders_open_count(venue, instrument_id, strategy_id, account_id, side)
613    }
614
615    /// Returns the count of all closed orders matching the optional filter parameters.
616    ///
617    /// # Panics
618    ///
619    /// Panics if the cache is already mutably borrowed.
620    #[must_use]
621    pub fn orders_closed_count(
622        &self,
623        venue: Option<&Venue>,
624        instrument_id: Option<&InstrumentId>,
625        strategy_id: Option<&StrategyId>,
626        account_id: Option<&AccountId>,
627        side: Option<OrderSide>,
628    ) -> usize {
629        self.cache()
630            .orders_closed_count(venue, instrument_id, strategy_id, account_id, side)
631    }
632
633    /// Returns the count of all locally active orders matching the optional filter parameters.
634    ///
635    /// # Panics
636    ///
637    /// Panics if the cache is already mutably borrowed.
638    #[must_use]
639    pub fn orders_active_local_count(
640        &self,
641        venue: Option<&Venue>,
642        instrument_id: Option<&InstrumentId>,
643        strategy_id: Option<&StrategyId>,
644        account_id: Option<&AccountId>,
645        side: Option<OrderSide>,
646    ) -> usize {
647        self.cache()
648            .orders_active_local_count(venue, instrument_id, strategy_id, account_id, side)
649    }
650
651    /// Returns the count of all emulated orders matching the optional filter parameters.
652    ///
653    /// # Panics
654    ///
655    /// Panics if the cache is already mutably borrowed.
656    #[must_use]
657    pub fn orders_emulated_count(
658        &self,
659        venue: Option<&Venue>,
660        instrument_id: Option<&InstrumentId>,
661        strategy_id: Option<&StrategyId>,
662        account_id: Option<&AccountId>,
663        side: Option<OrderSide>,
664    ) -> usize {
665        self.cache()
666            .orders_emulated_count(venue, instrument_id, strategy_id, account_id, side)
667    }
668
669    /// Returns the count of all in-flight orders matching the optional filter parameters.
670    ///
671    /// # Panics
672    ///
673    /// Panics if the cache is already mutably borrowed.
674    #[must_use]
675    pub fn orders_inflight_count(
676        &self,
677        venue: Option<&Venue>,
678        instrument_id: Option<&InstrumentId>,
679        strategy_id: Option<&StrategyId>,
680        account_id: Option<&AccountId>,
681        side: Option<OrderSide>,
682    ) -> usize {
683        self.cache()
684            .orders_inflight_count(venue, instrument_id, strategy_id, account_id, side)
685    }
686
687    /// Returns the count of all orders matching the optional filter parameters.
688    ///
689    /// # Panics
690    ///
691    /// Panics if the cache is already mutably borrowed.
692    #[must_use]
693    pub fn orders_total_count(
694        &self,
695        venue: Option<&Venue>,
696        instrument_id: Option<&InstrumentId>,
697        strategy_id: Option<&StrategyId>,
698        account_id: Option<&AccountId>,
699        side: Option<OrderSide>,
700    ) -> usize {
701        self.cache()
702            .orders_total_count(venue, instrument_id, strategy_id, account_id, side)
703    }
704
705    /// Returns whether any open order matches the optional filter parameters.
706    ///
707    /// # Panics
708    ///
709    /// Panics if the cache is already mutably borrowed.
710    #[must_use]
711    pub fn has_orders_open(
712        &self,
713        venue: Option<&Venue>,
714        instrument_id: Option<&InstrumentId>,
715        strategy_id: Option<&StrategyId>,
716        account_id: Option<&AccountId>,
717        side: Option<OrderSide>,
718    ) -> bool {
719        self.cache()
720            .has_orders_open(venue, instrument_id, strategy_id, account_id, side)
721    }
722
723    /// Returns whether any closed order matches the optional filter parameters.
724    ///
725    /// # Panics
726    ///
727    /// Panics if the cache is already mutably borrowed.
728    #[must_use]
729    pub fn has_orders_closed(
730        &self,
731        venue: Option<&Venue>,
732        instrument_id: Option<&InstrumentId>,
733        strategy_id: Option<&StrategyId>,
734        account_id: Option<&AccountId>,
735        side: Option<OrderSide>,
736    ) -> bool {
737        self.cache()
738            .has_orders_closed(venue, instrument_id, strategy_id, account_id, side)
739    }
740
741    /// Returns whether any locally active order matches the optional filter parameters.
742    ///
743    /// # Panics
744    ///
745    /// Panics if the cache is already mutably borrowed.
746    #[must_use]
747    pub fn has_orders_active_local(
748        &self,
749        venue: Option<&Venue>,
750        instrument_id: Option<&InstrumentId>,
751        strategy_id: Option<&StrategyId>,
752        account_id: Option<&AccountId>,
753        side: Option<OrderSide>,
754    ) -> bool {
755        self.cache()
756            .has_orders_active_local(venue, instrument_id, strategy_id, account_id, side)
757    }
758
759    /// Returns whether any emulated order matches the optional filter parameters.
760    ///
761    /// # Panics
762    ///
763    /// Panics if the cache is already mutably borrowed.
764    #[must_use]
765    pub fn has_orders_emulated(
766        &self,
767        venue: Option<&Venue>,
768        instrument_id: Option<&InstrumentId>,
769        strategy_id: Option<&StrategyId>,
770        account_id: Option<&AccountId>,
771        side: Option<OrderSide>,
772    ) -> bool {
773        self.cache()
774            .has_orders_emulated(venue, instrument_id, strategy_id, account_id, side)
775    }
776
777    /// Returns whether any in-flight order matches the optional filter parameters.
778    ///
779    /// # Panics
780    ///
781    /// Panics if the cache is already mutably borrowed.
782    #[must_use]
783    pub fn has_orders_inflight(
784        &self,
785        venue: Option<&Venue>,
786        instrument_id: Option<&InstrumentId>,
787        strategy_id: Option<&StrategyId>,
788        account_id: Option<&AccountId>,
789        side: Option<OrderSide>,
790    ) -> bool {
791        self.cache()
792            .has_orders_inflight(venue, instrument_id, strategy_id, account_id, side)
793    }
794
795    /// Returns whether any order matches the optional filter parameters.
796    ///
797    /// # Panics
798    ///
799    /// Panics if the cache is already mutably borrowed.
800    #[must_use]
801    pub fn has_orders(
802        &self,
803        venue: Option<&Venue>,
804        instrument_id: Option<&InstrumentId>,
805        strategy_id: Option<&StrategyId>,
806        account_id: Option<&AccountId>,
807        side: Option<OrderSide>,
808    ) -> bool {
809        self.cache()
810            .has_orders(venue, instrument_id, strategy_id, account_id, side)
811    }
812
813    /// Returns an owned copy of the order list for the `order_list_id` (if found).
814    ///
815    /// # Panics
816    ///
817    /// Panics if the cache is already mutably borrowed.
818    #[must_use]
819    pub fn order_list(&self, order_list_id: &OrderListId) -> Option<OrderList> {
820        self.cache().order_list(order_list_id).cloned()
821    }
822
823    /// Returns an owned copy of the order list for the `order_list_id`.
824    ///
825    /// # Errors
826    ///
827    /// Returns an error if:
828    /// - The order list is not present in the cache.
829    /// - The cache is already mutably borrowed.
830    pub fn try_order_list(
831        &self,
832        order_list_id: &OrderListId,
833    ) -> Result<OrderList, OrderListLookupError> {
834        self.try_cache("try_order_list")?
835            .try_order_list(order_list_id)
836            .cloned()
837    }
838
839    /// Returns owned copies of all order lists matching the optional filter parameters.
840    ///
841    /// # Panics
842    ///
843    /// Panics if the cache is already mutably borrowed.
844    #[must_use]
845    pub fn order_lists(
846        &self,
847        venue: Option<&Venue>,
848        instrument_id: Option<&InstrumentId>,
849        strategy_id: Option<&StrategyId>,
850        account_id: Option<&AccountId>,
851    ) -> Vec<OrderList> {
852        self.cache()
853            .order_lists(venue, instrument_id, strategy_id, account_id)
854            .into_iter()
855            .cloned()
856            .collect()
857    }
858
859    /// Returns whether an order list with the `order_list_id` exists.
860    ///
861    /// # Panics
862    ///
863    /// Panics if the cache is already mutably borrowed.
864    #[must_use]
865    pub fn order_list_exists(&self, order_list_id: &OrderListId) -> bool {
866        self.cache().order_list_exists(order_list_id)
867    }
868
869    /// Returns owned copies of all orders associated with the `exec_algorithm_id`.
870    ///
871    /// # Panics
872    ///
873    /// Panics if the cache is already mutably borrowed.
874    #[must_use]
875    pub fn orders_for_exec_algorithm(
876        &self,
877        exec_algorithm_id: &ExecAlgorithmId,
878        venue: Option<&Venue>,
879        instrument_id: Option<&InstrumentId>,
880        strategy_id: Option<&StrategyId>,
881        account_id: Option<&AccountId>,
882        side: Option<OrderSide>,
883    ) -> Vec<OrderAny> {
884        self.cache()
885            .orders_for_exec_algorithm(
886                exec_algorithm_id,
887                venue,
888                instrument_id,
889                strategy_id,
890                account_id,
891                side,
892            )
893            .into_iter()
894            .map(|order| order.cloned())
895            .collect()
896    }
897
898    /// Returns owned copies of all orders with the `exec_spawn_id`.
899    ///
900    /// # Panics
901    ///
902    /// Panics if the cache is already mutably borrowed.
903    #[must_use]
904    pub fn orders_for_exec_spawn(&self, exec_spawn_id: &ClientOrderId) -> Vec<OrderAny> {
905        self.cache()
906            .orders_for_exec_spawn(exec_spawn_id)
907            .into_iter()
908            .map(|order| order.cloned())
909            .collect()
910    }
911
912    /// Returns the total order quantity for the `exec_spawn_id`.
913    ///
914    /// # Panics
915    ///
916    /// Panics if the cache is already mutably borrowed.
917    #[must_use]
918    pub fn exec_spawn_total_quantity(
919        &self,
920        exec_spawn_id: &ClientOrderId,
921        active_only: bool,
922    ) -> Option<Quantity> {
923        self.cache()
924            .exec_spawn_total_quantity(exec_spawn_id, active_only)
925    }
926
927    /// Returns the total filled quantity for all orders with the `exec_spawn_id`.
928    ///
929    /// # Panics
930    ///
931    /// Panics if the cache is already mutably borrowed.
932    #[must_use]
933    pub fn exec_spawn_total_filled_qty(
934        &self,
935        exec_spawn_id: &ClientOrderId,
936        active_only: bool,
937    ) -> Option<Quantity> {
938        self.cache()
939            .exec_spawn_total_filled_qty(exec_spawn_id, active_only)
940    }
941
942    /// Returns the total leaves quantity for all orders with the `exec_spawn_id`.
943    ///
944    /// # Panics
945    ///
946    /// Panics if the cache is already mutably borrowed.
947    #[must_use]
948    pub fn exec_spawn_total_leaves_qty(
949        &self,
950        exec_spawn_id: &ClientOrderId,
951        active_only: bool,
952    ) -> Option<Quantity> {
953        self.cache()
954            .exec_spawn_total_leaves_qty(exec_spawn_id, active_only)
955    }
956
957    /// Returns an owned copy of the position for the `position_id` (if found).
958    ///
959    /// # Panics
960    ///
961    /// Panics if the cache is already mutably borrowed.
962    #[must_use]
963    pub fn position(&self, position_id: &PositionId) -> Option<Position> {
964        self.cache()
965            .position_ref(position_id)
966            .map(|position| position.cloned())
967    }
968
969    /// Returns an owned copy of the position for the `position_id`.
970    ///
971    /// # Errors
972    ///
973    /// Returns an error if:
974    /// - The position is not present in the cache.
975    /// - The cache is already mutably borrowed.
976    pub fn try_position(&self, position_id: &PositionId) -> Result<Position, PositionLookupError> {
977        self.try_cache("try_position")?
978            .try_position_ref(position_id)
979            .map(|position| position.cloned())
980    }
981
982    /// Returns an owned copy of the position for the `client_order_id` (if found).
983    ///
984    /// # Panics
985    ///
986    /// Panics if the cache is already mutably borrowed.
987    #[must_use]
988    pub fn position_for_order(&self, client_order_id: &ClientOrderId) -> Option<Position> {
989        self.cache()
990            .position_for_order_ref(client_order_id)
991            .map(|position| position.cloned())
992    }
993
994    /// Returns the position ID for the `client_order_id` (if found).
995    ///
996    /// # Panics
997    ///
998    /// Panics if the cache is already mutably borrowed.
999    #[must_use]
1000    pub fn position_id(&self, client_order_id: &ClientOrderId) -> Option<PositionId> {
1001        self.cache().position_id(client_order_id).copied()
1002    }
1003
1004    /// Returns owned copies of all positions matching the optional filter parameters.
1005    ///
1006    /// # Panics
1007    ///
1008    /// Panics if the cache is already mutably borrowed.
1009    #[must_use]
1010    pub fn positions(
1011        &self,
1012        venue: Option<&Venue>,
1013        instrument_id: Option<&InstrumentId>,
1014        strategy_id: Option<&StrategyId>,
1015        account_id: Option<&AccountId>,
1016        side: Option<PositionSide>,
1017    ) -> Vec<Position> {
1018        self.cache()
1019            .positions_refs(venue, instrument_id, strategy_id, account_id, side)
1020            .into_iter()
1021            .map(|position| position.cloned())
1022            .collect()
1023    }
1024
1025    /// Returns owned copies of all open positions matching the optional filter parameters.
1026    ///
1027    /// # Panics
1028    ///
1029    /// Panics if the cache is already mutably borrowed.
1030    #[must_use]
1031    pub fn positions_open(
1032        &self,
1033        venue: Option<&Venue>,
1034        instrument_id: Option<&InstrumentId>,
1035        strategy_id: Option<&StrategyId>,
1036        account_id: Option<&AccountId>,
1037        side: Option<PositionSide>,
1038    ) -> Vec<Position> {
1039        self.cache()
1040            .positions_open_refs(venue, instrument_id, strategy_id, account_id, side)
1041            .into_iter()
1042            .map(|position| position.cloned())
1043            .collect()
1044    }
1045
1046    /// Returns owned copies of all closed positions matching the optional filter parameters.
1047    ///
1048    /// # Panics
1049    ///
1050    /// Panics if the cache is already mutably borrowed.
1051    #[must_use]
1052    pub fn positions_closed(
1053        &self,
1054        venue: Option<&Venue>,
1055        instrument_id: Option<&InstrumentId>,
1056        strategy_id: Option<&StrategyId>,
1057        account_id: Option<&AccountId>,
1058        side: Option<PositionSide>,
1059    ) -> Vec<Position> {
1060        self.cache()
1061            .positions_closed_refs(venue, instrument_id, strategy_id, account_id, side)
1062            .into_iter()
1063            .map(|position| position.cloned())
1064            .collect()
1065    }
1066
1067    /// Returns whether a position with the `position_id` exists.
1068    ///
1069    /// # Panics
1070    ///
1071    /// Panics if the cache is already mutably borrowed.
1072    #[must_use]
1073    pub fn position_exists(&self, position_id: &PositionId) -> bool {
1074        self.cache().position_exists(position_id)
1075    }
1076
1077    /// Returns whether a position with the `position_id` is open.
1078    ///
1079    /// # Panics
1080    ///
1081    /// Panics if the cache is already mutably borrowed.
1082    #[must_use]
1083    pub fn is_position_open(&self, position_id: &PositionId) -> bool {
1084        self.cache().is_position_open(position_id)
1085    }
1086
1087    /// Returns whether a position with the `position_id` is closed.
1088    ///
1089    /// # Panics
1090    ///
1091    /// Panics if the cache is already mutably borrowed.
1092    #[must_use]
1093    pub fn is_position_closed(&self, position_id: &PositionId) -> bool {
1094        self.cache().is_position_closed(position_id)
1095    }
1096
1097    /// Returns the count of all open positions matching the optional filter parameters.
1098    ///
1099    /// # Panics
1100    ///
1101    /// Panics if the cache is already mutably borrowed.
1102    #[must_use]
1103    pub fn positions_open_count(
1104        &self,
1105        venue: Option<&Venue>,
1106        instrument_id: Option<&InstrumentId>,
1107        strategy_id: Option<&StrategyId>,
1108        account_id: Option<&AccountId>,
1109        side: Option<PositionSide>,
1110    ) -> usize {
1111        self.cache()
1112            .positions_open_count(venue, instrument_id, strategy_id, account_id, side)
1113    }
1114
1115    /// Returns the count of all closed positions matching the optional filter parameters.
1116    ///
1117    /// # Panics
1118    ///
1119    /// Panics if the cache is already mutably borrowed.
1120    #[must_use]
1121    pub fn positions_closed_count(
1122        &self,
1123        venue: Option<&Venue>,
1124        instrument_id: Option<&InstrumentId>,
1125        strategy_id: Option<&StrategyId>,
1126        account_id: Option<&AccountId>,
1127        side: Option<PositionSide>,
1128    ) -> usize {
1129        self.cache()
1130            .positions_closed_count(venue, instrument_id, strategy_id, account_id, side)
1131    }
1132
1133    /// Returns the count of all positions matching the optional filter parameters.
1134    ///
1135    /// # Panics
1136    ///
1137    /// Panics if the cache is already mutably borrowed.
1138    #[must_use]
1139    pub fn positions_total_count(
1140        &self,
1141        venue: Option<&Venue>,
1142        instrument_id: Option<&InstrumentId>,
1143        strategy_id: Option<&StrategyId>,
1144        account_id: Option<&AccountId>,
1145        side: Option<PositionSide>,
1146    ) -> usize {
1147        self.cache()
1148            .positions_total_count(venue, instrument_id, strategy_id, account_id, side)
1149    }
1150
1151    /// Returns whether any open position matches the optional filter parameters.
1152    ///
1153    /// # Panics
1154    ///
1155    /// Panics if the cache is already mutably borrowed.
1156    #[must_use]
1157    pub fn has_positions_open(
1158        &self,
1159        venue: Option<&Venue>,
1160        instrument_id: Option<&InstrumentId>,
1161        strategy_id: Option<&StrategyId>,
1162        account_id: Option<&AccountId>,
1163        side: Option<PositionSide>,
1164    ) -> bool {
1165        self.cache()
1166            .has_positions_open(venue, instrument_id, strategy_id, account_id, side)
1167    }
1168
1169    /// Returns whether any closed position matches the optional filter parameters.
1170    ///
1171    /// # Panics
1172    ///
1173    /// Panics if the cache is already mutably borrowed.
1174    #[must_use]
1175    pub fn has_positions_closed(
1176        &self,
1177        venue: Option<&Venue>,
1178        instrument_id: Option<&InstrumentId>,
1179        strategy_id: Option<&StrategyId>,
1180        account_id: Option<&AccountId>,
1181        side: Option<PositionSide>,
1182    ) -> bool {
1183        self.cache()
1184            .has_positions_closed(venue, instrument_id, strategy_id, account_id, side)
1185    }
1186
1187    /// Returns whether any position matches the optional filter parameters.
1188    ///
1189    /// # Panics
1190    ///
1191    /// Panics if the cache is already mutably borrowed.
1192    #[must_use]
1193    pub fn has_positions(
1194        &self,
1195        venue: Option<&Venue>,
1196        instrument_id: Option<&InstrumentId>,
1197        strategy_id: Option<&StrategyId>,
1198        account_id: Option<&AccountId>,
1199        side: Option<PositionSide>,
1200    ) -> bool {
1201        self.cache()
1202            .has_positions(venue, instrument_id, strategy_id, account_id, side)
1203    }
1204
1205    /// Returns the strategy ID for the `client_order_id` (if found).
1206    ///
1207    /// # Panics
1208    ///
1209    /// Panics if the cache is already mutably borrowed.
1210    #[must_use]
1211    pub fn strategy_id_for_order(&self, client_order_id: &ClientOrderId) -> Option<StrategyId> {
1212        self.cache().strategy_id_for_order(client_order_id).copied()
1213    }
1214
1215    /// Returns the strategy ID for the `position_id` (if found).
1216    ///
1217    /// # Panics
1218    ///
1219    /// Panics if the cache is already mutably borrowed.
1220    #[must_use]
1221    pub fn strategy_id_for_position(&self, position_id: &PositionId) -> Option<StrategyId> {
1222        self.cache().strategy_id_for_position(position_id).copied()
1223    }
1224
1225    /// Returns the general cache value for the `key` (if found).
1226    ///
1227    /// # Errors
1228    ///
1229    /// Returns an error if:
1230    /// - The `key` is invalid.
1231    /// - The cache is already mutably borrowed.
1232    pub fn get(&self, key: &str) -> anyhow::Result<Option<Bytes>> {
1233        let cache = self.try_cache("get")?;
1234        let value = cache.get(key)?;
1235        Ok(value.cloned())
1236    }
1237
1238    /// Returns the price for the `instrument_id` and `price_type` (if found).
1239    ///
1240    /// For `Mid`, returns `None` if no quote is cached or either price is a sentinel.
1241    /// For quote precision `p`, the midpoint has precision `p + 1` when exactly representable,
1242    /// otherwise `p`, rounded half-even if necessary. The fallback applies when the precision
1243    /// limit or raw range rules out `p + 1`, and when `p` is the maximum float precision (16),
1244    /// so a midpoint of a float-convertible quote stays float-convertible.
1245    ///
1246    /// # Panics
1247    ///
1248    /// Panics if the cache is already mutably borrowed.
1249    #[must_use]
1250    pub fn price(&self, instrument_id: &InstrumentId, price_type: PriceType) -> Option<Price> {
1251        self.cache().price(instrument_id, price_type)
1252    }
1253
1254    /// Returns cached quotes for the `instrument_id`, most recently added first.
1255    ///
1256    /// See [`Cache::quotes`] for ordering and missing-data behavior.
1257    ///
1258    /// # Panics
1259    ///
1260    /// Panics if the cache is already mutably borrowed.
1261    #[must_use]
1262    pub fn quotes(&self, instrument_id: &InstrumentId) -> Option<Vec<QuoteTick>> {
1263        self.cache().quotes(instrument_id)
1264    }
1265
1266    /// Returns cached trades for the `instrument_id`, most recently added first.
1267    ///
1268    /// See [`Cache::trades`] for ordering and missing-data behavior.
1269    ///
1270    /// # Panics
1271    ///
1272    /// Panics if the cache is already mutably borrowed.
1273    #[must_use]
1274    pub fn trades(&self, instrument_id: &InstrumentId) -> Option<Vec<TradeTick>> {
1275        self.cache().trades(instrument_id)
1276    }
1277
1278    /// Returns cached mark price updates for the `instrument_id`, most recently added first.
1279    ///
1280    /// See [`Cache::mark_prices`] for ordering and missing-data behavior.
1281    ///
1282    /// # Panics
1283    ///
1284    /// Panics if the cache is already mutably borrowed.
1285    #[must_use]
1286    pub fn mark_prices(&self, instrument_id: &InstrumentId) -> Option<Vec<MarkPriceUpdate>> {
1287        self.cache().mark_prices(instrument_id)
1288    }
1289
1290    /// Returns cached index price updates for the `instrument_id`, most recently added first.
1291    ///
1292    /// See [`Cache::index_prices`] for ordering and missing-data behavior.
1293    ///
1294    /// # Panics
1295    ///
1296    /// Panics if the cache is already mutably borrowed.
1297    #[must_use]
1298    pub fn index_prices(&self, instrument_id: &InstrumentId) -> Option<Vec<IndexPriceUpdate>> {
1299        self.cache().index_prices(instrument_id)
1300    }
1301
1302    /// Returns cached funding rate updates for the `instrument_id`, most recently added first.
1303    ///
1304    /// See [`Cache::funding_rates`] for ordering and missing-data behavior.
1305    ///
1306    /// # Panics
1307    ///
1308    /// Panics if the cache is already mutably borrowed.
1309    #[must_use]
1310    pub fn funding_rates(&self, instrument_id: &InstrumentId) -> Option<Vec<FundingRateUpdate>> {
1311        self.cache().funding_rates(instrument_id)
1312    }
1313
1314    /// Returns cached instrument status updates for the `instrument_id`, most recently added first.
1315    ///
1316    /// See [`Cache::instrument_statuses`] for ordering and missing-data behavior.
1317    ///
1318    /// # Panics
1319    ///
1320    /// Panics if the cache is already mutably borrowed.
1321    #[must_use]
1322    pub fn instrument_statuses(
1323        &self,
1324        instrument_id: &InstrumentId,
1325    ) -> Option<Vec<InstrumentStatus>> {
1326        self.cache().instrument_statuses(instrument_id)
1327    }
1328
1329    /// Returns cached bars for the `bar_type`, newest `ts_event` first.
1330    ///
1331    /// See [`Cache::bars`] for ordering and missing-data behavior.
1332    ///
1333    /// # Panics
1334    ///
1335    /// Panics if the cache is already mutably borrowed.
1336    #[must_use]
1337    pub fn bars(&self, bar_type: &BarType) -> Option<Vec<Bar>> {
1338        self.cache().bars(bar_type)
1339    }
1340
1341    /// Returns custom data for the `data_type` in reverse insertion order.
1342    ///
1343    /// See [`Cache::custom_data_history`] for ordering and missing-data behavior.
1344    ///
1345    /// # Panics
1346    ///
1347    /// Panics if the cache is already mutably borrowed.
1348    #[must_use]
1349    pub fn custom_data_history(&self, data_type: &DataType) -> Vec<CustomData> {
1350        self.cache().custom_data_history(data_type)
1351    }
1352
1353    /// Returns the most recently added custom data for the `data_type`, or `None` if absent.
1354    ///
1355    /// # Panics
1356    ///
1357    /// Panics if the cache is already mutably borrowed.
1358    #[must_use]
1359    pub fn custom_data(&self, data_type: &DataType) -> Option<CustomData> {
1360        self.custom_data_at_index(data_type, 0)
1361    }
1362
1363    /// Returns custom data at `index` for the `data_type`, or `None` if absent.
1364    ///
1365    /// See [`Cache::custom_data_at_index`] for ordering and out-of-range behavior.
1366    ///
1367    /// # Panics
1368    ///
1369    /// Panics if the cache is already mutably borrowed.
1370    #[must_use]
1371    pub fn custom_data_at_index(&self, data_type: &DataType, index: usize) -> Option<CustomData> {
1372        self.cache().custom_data_at_index(data_type, index).cloned()
1373    }
1374
1375    /// Returns an owned copy of the order book for the `instrument_id` (if found).
1376    ///
1377    /// See [`Cache::order_book`] for the current-state contract.
1378    ///
1379    /// # Panics
1380    ///
1381    /// Panics if the cache is already mutably borrowed.
1382    #[must_use]
1383    pub fn order_book(&self, instrument_id: &InstrumentId) -> Option<OrderBook> {
1384        self.cache().order_book(instrument_id).cloned()
1385    }
1386
1387    /// Returns an owned copy of the order book for the `instrument_id`.
1388    ///
1389    /// # Errors
1390    ///
1391    /// Returns an error if:
1392    /// - The order book is not present in the cache.
1393    /// - The cache is already mutably borrowed.
1394    pub fn try_order_book(
1395        &self,
1396        instrument_id: &InstrumentId,
1397    ) -> Result<OrderBook, OrderBookLookupError> {
1398        self.try_cache("try_order_book")?
1399            .try_order_book(instrument_id)
1400            .cloned()
1401    }
1402
1403    /// Returns an owned copy of the own order book for the `instrument_id` (if found).
1404    ///
1405    /// # Panics
1406    ///
1407    /// Panics if the cache is already mutably borrowed.
1408    #[must_use]
1409    pub fn own_order_book(&self, instrument_id: &InstrumentId) -> Option<OwnOrderBook> {
1410        self.cache().own_order_book(instrument_id).cloned()
1411    }
1412
1413    /// Returns an owned copy of the own order book for the `instrument_id`.
1414    ///
1415    /// # Errors
1416    ///
1417    /// Returns an error if:
1418    /// - The own order book is not present in the cache.
1419    /// - The cache is already mutably borrowed.
1420    pub fn try_own_order_book(
1421        &self,
1422        instrument_id: &InstrumentId,
1423    ) -> Result<OwnOrderBook, OwnOrderBookLookupError> {
1424        self.try_cache("try_own_order_book")?
1425            .try_own_order_book(instrument_id)
1426            .cloned()
1427    }
1428
1429    /// Returns the most recently added quote for the `instrument_id` (if found).
1430    ///
1431    /// # Panics
1432    ///
1433    /// Panics if the cache is already mutably borrowed.
1434    #[must_use]
1435    pub fn quote(&self, instrument_id: &InstrumentId) -> Option<QuoteTick> {
1436        self.cache().quote(instrument_id).copied()
1437    }
1438
1439    /// Returns the quote at `index` for the `instrument_id` (if found).
1440    ///
1441    /// Index 0 is the most recently added quote. See [`Cache::quote_at_index`] for ordering
1442    /// and out-of-range behavior.
1443    ///
1444    /// # Panics
1445    ///
1446    /// Panics if the cache is already mutably borrowed.
1447    #[must_use]
1448    pub fn quote_at_index(&self, instrument_id: &InstrumentId, index: usize) -> Option<QuoteTick> {
1449        self.cache().quote_at_index(instrument_id, index).copied()
1450    }
1451
1452    /// Returns the most recently added trade for the `instrument_id` (if found).
1453    ///
1454    /// # Panics
1455    ///
1456    /// Panics if the cache is already mutably borrowed.
1457    #[must_use]
1458    pub fn trade(&self, instrument_id: &InstrumentId) -> Option<TradeTick> {
1459        self.cache().trade(instrument_id).copied()
1460    }
1461
1462    /// Returns the trade at `index` for the `instrument_id` (if found).
1463    ///
1464    /// Index 0 is the most recently added trade. See [`Cache::trade_at_index`] for ordering
1465    /// and out-of-range behavior.
1466    ///
1467    /// # Panics
1468    ///
1469    /// Panics if the cache is already mutably borrowed.
1470    #[must_use]
1471    pub fn trade_at_index(&self, instrument_id: &InstrumentId, index: usize) -> Option<TradeTick> {
1472        self.cache().trade_at_index(instrument_id, index).copied()
1473    }
1474
1475    /// Returns the most recently added mark price update for the `instrument_id` (if found).
1476    ///
1477    /// # Panics
1478    ///
1479    /// Panics if the cache is already mutably borrowed.
1480    #[must_use]
1481    pub fn mark_price(&self, instrument_id: &InstrumentId) -> Option<MarkPriceUpdate> {
1482        self.cache().mark_price(instrument_id).copied()
1483    }
1484
1485    /// Returns the mark price update at `index` for the `instrument_id` (if found).
1486    ///
1487    /// Index 0 is the most recently added update. See [`Cache::mark_price_at_index`] for
1488    /// ordering and out-of-range behavior.
1489    ///
1490    /// # Panics
1491    ///
1492    /// Panics if the cache is already mutably borrowed.
1493    #[must_use]
1494    pub fn mark_price_at_index(
1495        &self,
1496        instrument_id: &InstrumentId,
1497        index: usize,
1498    ) -> Option<MarkPriceUpdate> {
1499        self.cache()
1500            .mark_price_at_index(instrument_id, index)
1501            .copied()
1502    }
1503
1504    /// Returns the most recently added index price update for the `instrument_id` (if found).
1505    ///
1506    /// # Panics
1507    ///
1508    /// Panics if the cache is already mutably borrowed.
1509    #[must_use]
1510    pub fn index_price(&self, instrument_id: &InstrumentId) -> Option<IndexPriceUpdate> {
1511        self.cache().index_price(instrument_id).copied()
1512    }
1513
1514    /// Returns the index price update at `index` for the `instrument_id` (if found).
1515    ///
1516    /// Index 0 is the most recently added update. See [`Cache::index_price_at_index`] for
1517    /// ordering and out-of-range behavior.
1518    ///
1519    /// # Panics
1520    ///
1521    /// Panics if the cache is already mutably borrowed.
1522    #[must_use]
1523    pub fn index_price_at_index(
1524        &self,
1525        instrument_id: &InstrumentId,
1526        index: usize,
1527    ) -> Option<IndexPriceUpdate> {
1528        self.cache()
1529            .index_price_at_index(instrument_id, index)
1530            .copied()
1531    }
1532
1533    /// Returns the most recently added funding rate update for the `instrument_id` (if found).
1534    ///
1535    /// # Panics
1536    ///
1537    /// Panics if the cache is already mutably borrowed.
1538    #[must_use]
1539    pub fn funding_rate(&self, instrument_id: &InstrumentId) -> Option<FundingRateUpdate> {
1540        self.cache().funding_rate(instrument_id).copied()
1541    }
1542
1543    /// Returns the funding rate update at `index` for the `instrument_id` (if found).
1544    ///
1545    /// Index 0 is the most recently added update. See [`Cache::funding_rate_at_index`] for
1546    /// ordering and out-of-range behavior.
1547    ///
1548    /// # Panics
1549    ///
1550    /// Panics if the cache is already mutably borrowed.
1551    #[must_use]
1552    pub fn funding_rate_at_index(
1553        &self,
1554        instrument_id: &InstrumentId,
1555        index: usize,
1556    ) -> Option<FundingRateUpdate> {
1557        self.cache()
1558            .funding_rate_at_index(instrument_id, index)
1559            .copied()
1560    }
1561
1562    /// Returns the most recently added instrument status update for the `instrument_id` (if found).
1563    ///
1564    /// # Panics
1565    ///
1566    /// Panics if the cache is already mutably borrowed.
1567    #[must_use]
1568    pub fn instrument_status(&self, instrument_id: &InstrumentId) -> Option<InstrumentStatus> {
1569        self.cache().instrument_status(instrument_id).copied()
1570    }
1571
1572    /// Returns the cached close for the `instrument_id` (if found).
1573    ///
1574    /// See [`Cache::instrument_close`] for replacement behavior.
1575    ///
1576    /// # Panics
1577    ///
1578    /// Panics if the cache is already mutably borrowed.
1579    #[must_use]
1580    pub fn instrument_close(&self, instrument_id: &InstrumentId) -> Option<InstrumentClose> {
1581        self.cache().instrument_close(instrument_id).copied()
1582    }
1583
1584    /// Returns the most recent bar by `ts_event` for the `bar_type` (if found).
1585    ///
1586    /// # Panics
1587    ///
1588    /// Panics if the cache is already mutably borrowed.
1589    #[must_use]
1590    pub fn bar(&self, bar_type: &BarType) -> Option<Bar> {
1591        self.cache().bar(bar_type).copied()
1592    }
1593
1594    /// Returns the bar at `index` for the `bar_type` (if found).
1595    ///
1596    /// Index 0 is the most recent bar by `ts_event`. See [`Cache::bar_at_index`] for ordering
1597    /// and out-of-range behavior.
1598    ///
1599    /// # Panics
1600    ///
1601    /// Panics if the cache is already mutably borrowed.
1602    #[must_use]
1603    pub fn bar_at_index(&self, bar_type: &BarType, index: usize) -> Option<Bar> {
1604        self.cache().bar_at_index(bar_type, index).copied()
1605    }
1606
1607    /// Returns the order book update count for the `instrument_id`.
1608    ///
1609    /// # Panics
1610    ///
1611    /// Panics if the cache is already mutably borrowed.
1612    #[must_use]
1613    pub fn book_update_count(&self, instrument_id: &InstrumentId) -> usize {
1614        self.cache().book_update_count(instrument_id)
1615    }
1616
1617    /// Returns the quote tick count for the `instrument_id`.
1618    ///
1619    /// # Panics
1620    ///
1621    /// Panics if the cache is already mutably borrowed.
1622    #[must_use]
1623    pub fn quote_count(&self, instrument_id: &InstrumentId) -> usize {
1624        self.cache().quote_count(instrument_id)
1625    }
1626
1627    /// Returns the trade tick count for the `instrument_id`.
1628    ///
1629    /// # Panics
1630    ///
1631    /// Panics if the cache is already mutably borrowed.
1632    #[must_use]
1633    pub fn trade_count(&self, instrument_id: &InstrumentId) -> usize {
1634        self.cache().trade_count(instrument_id)
1635    }
1636
1637    /// Returns the mark price update count for the `instrument_id`.
1638    ///
1639    /// # Panics
1640    ///
1641    /// Panics if the cache is already mutably borrowed.
1642    #[must_use]
1643    pub fn mark_price_count(&self, instrument_id: &InstrumentId) -> usize {
1644        self.cache().mark_price_count(instrument_id)
1645    }
1646
1647    /// Returns the index price update count for the `instrument_id`.
1648    ///
1649    /// # Panics
1650    ///
1651    /// Panics if the cache is already mutably borrowed.
1652    #[must_use]
1653    pub fn index_price_count(&self, instrument_id: &InstrumentId) -> usize {
1654        self.cache().index_price_count(instrument_id)
1655    }
1656
1657    /// Returns the funding rate update count for the `instrument_id`.
1658    ///
1659    /// # Panics
1660    ///
1661    /// Panics if the cache is already mutably borrowed.
1662    #[must_use]
1663    pub fn funding_rate_count(&self, instrument_id: &InstrumentId) -> usize {
1664        self.cache().funding_rate_count(instrument_id)
1665    }
1666
1667    /// Returns the instrument status update count for the `instrument_id`.
1668    ///
1669    /// # Panics
1670    ///
1671    /// Panics if the cache is already mutably borrowed.
1672    #[must_use]
1673    pub fn instrument_status_count(&self, instrument_id: &InstrumentId) -> usize {
1674        self.cache().instrument_status_count(instrument_id)
1675    }
1676
1677    /// Returns the bar count for the `bar_type`.
1678    ///
1679    /// # Panics
1680    ///
1681    /// Panics if the cache is already mutably borrowed.
1682    #[must_use]
1683    pub fn bar_count(&self, bar_type: &BarType) -> usize {
1684        self.cache().bar_count(bar_type)
1685    }
1686
1687    /// Returns whether the cache contains an order book for the `instrument_id`.
1688    ///
1689    /// # Panics
1690    ///
1691    /// Panics if the cache is already mutably borrowed.
1692    #[must_use]
1693    pub fn has_order_book(&self, instrument_id: &InstrumentId) -> bool {
1694        self.cache().has_order_book(instrument_id)
1695    }
1696
1697    /// Returns whether the cache contains quotes for the `instrument_id`.
1698    ///
1699    /// # Panics
1700    ///
1701    /// Panics if the cache is already mutably borrowed.
1702    #[must_use]
1703    pub fn has_quote_ticks(&self, instrument_id: &InstrumentId) -> bool {
1704        self.cache().has_quote_ticks(instrument_id)
1705    }
1706
1707    /// Returns whether the cache contains trades for the `instrument_id`.
1708    ///
1709    /// # Panics
1710    ///
1711    /// Panics if the cache is already mutably borrowed.
1712    #[must_use]
1713    pub fn has_trade_ticks(&self, instrument_id: &InstrumentId) -> bool {
1714        self.cache().has_trade_ticks(instrument_id)
1715    }
1716
1717    /// Returns whether the cache contains mark price updates for the `instrument_id`.
1718    ///
1719    /// # Panics
1720    ///
1721    /// Panics if the cache is already mutably borrowed.
1722    #[must_use]
1723    pub fn has_mark_prices(&self, instrument_id: &InstrumentId) -> bool {
1724        self.cache().has_mark_prices(instrument_id)
1725    }
1726
1727    /// Returns whether the cache contains index price updates for the `instrument_id`.
1728    ///
1729    /// # Panics
1730    ///
1731    /// Panics if the cache is already mutably borrowed.
1732    #[must_use]
1733    pub fn has_index_prices(&self, instrument_id: &InstrumentId) -> bool {
1734        self.cache().has_index_prices(instrument_id)
1735    }
1736
1737    /// Returns whether the cache contains funding rate updates for the `instrument_id`.
1738    ///
1739    /// # Panics
1740    ///
1741    /// Panics if the cache is already mutably borrowed.
1742    #[must_use]
1743    pub fn has_funding_rates(&self, instrument_id: &InstrumentId) -> bool {
1744        self.cache().has_funding_rates(instrument_id)
1745    }
1746
1747    /// Returns whether the cache contains instrument status updates for the `instrument_id`.
1748    ///
1749    /// # Panics
1750    ///
1751    /// Panics if the cache is already mutably borrowed.
1752    #[must_use]
1753    pub fn has_instrument_statuses(&self, instrument_id: &InstrumentId) -> bool {
1754        self.cache().has_instrument_statuses(instrument_id)
1755    }
1756
1757    /// Returns whether the cache contains a close for the `instrument_id`.
1758    ///
1759    /// # Panics
1760    ///
1761    /// Panics if the cache is already mutably borrowed.
1762    #[must_use]
1763    pub fn has_instrument_close(&self, instrument_id: &InstrumentId) -> bool {
1764        self.cache().has_instrument_close(instrument_id)
1765    }
1766
1767    /// Returns whether the cache contains bars for the `bar_type`.
1768    ///
1769    /// # Panics
1770    ///
1771    /// Panics if the cache is already mutably borrowed.
1772    #[must_use]
1773    pub fn has_bars(&self, bar_type: &BarType) -> bool {
1774        self.cache().has_bars(bar_type)
1775    }
1776
1777    /// Returns the exchange rate for the given currencies and price type (if available).
1778    ///
1779    /// # Panics
1780    ///
1781    /// Panics if the cache is already mutably borrowed.
1782    #[must_use]
1783    pub fn get_xrate(
1784        &self,
1785        venue: Venue,
1786        from_currency: Currency,
1787        to_currency: Currency,
1788        price_type: PriceType,
1789    ) -> Option<Decimal> {
1790        self.cache()
1791            .get_xrate(venue, from_currency, to_currency, price_type)
1792    }
1793
1794    /// Returns the mark exchange rate for the currency pair (if set).
1795    ///
1796    /// # Panics
1797    ///
1798    /// Panics if the cache is already mutably borrowed.
1799    #[must_use]
1800    pub fn get_mark_xrate(&self, from_currency: Currency, to_currency: Currency) -> Option<f64> {
1801        self.cache().get_mark_xrate(from_currency, to_currency)
1802    }
1803
1804    /// Returns the yield curve for the `key` (if found).
1805    ///
1806    /// # Panics
1807    ///
1808    /// Panics if the cache is already mutably borrowed.
1809    #[must_use]
1810    pub fn yield_curve(&self, key: &str) -> Option<Box<dyn Fn(f64) -> f64>> {
1811        self.cache().yield_curve(key)
1812    }
1813
1814    /// Returns an owned copy of the greeks data for the `instrument_id` (if found).
1815    ///
1816    /// # Panics
1817    ///
1818    /// Panics if the cache is already mutably borrowed.
1819    #[must_use]
1820    pub fn greeks(&self, instrument_id: &InstrumentId) -> Option<GreeksData> {
1821        self.cache().greeks(instrument_id)
1822    }
1823
1824    /// Returns exchange-provided option greeks for the `instrument_id` (if found).
1825    ///
1826    /// # Panics
1827    ///
1828    /// Panics if the cache is already mutably borrowed.
1829    #[must_use]
1830    pub fn option_greeks(&self, instrument_id: &InstrumentId) -> Option<OptionGreeks> {
1831        self.cache().option_greeks(instrument_id).copied()
1832    }
1833
1834    /// Returns the currency for the `code` (if found).
1835    ///
1836    /// # Panics
1837    ///
1838    /// Panics if the cache is already mutably borrowed.
1839    #[must_use]
1840    pub fn currency(&self, code: &Ustr) -> Option<Currency> {
1841        self.cache().currency(code).copied()
1842    }
1843
1844    /// Returns the currency for the `code`.
1845    ///
1846    /// # Errors
1847    ///
1848    /// Returns an error if:
1849    /// - The currency is not present in the cache.
1850    /// - The cache is already mutably borrowed.
1851    pub fn try_currency(&self, code: &Ustr) -> Result<Currency, CurrencyLookupError> {
1852        self.try_cache("try_currency")?.try_currency(code).copied()
1853    }
1854
1855    /// Returns an owned copy of the instrument for the `instrument_id` (if found).
1856    ///
1857    /// # Panics
1858    ///
1859    /// Panics if the cache is already mutably borrowed.
1860    #[must_use]
1861    pub fn instrument(&self, instrument_id: &InstrumentId) -> Option<InstrumentAny> {
1862        self.cache().instrument(instrument_id).cloned()
1863    }
1864
1865    /// Returns an owned copy of the instrument for the `instrument_id`.
1866    ///
1867    /// # Errors
1868    ///
1869    /// Returns an error if:
1870    /// - The instrument is not present in the cache.
1871    /// - The cache is already mutably borrowed.
1872    pub fn try_instrument(
1873        &self,
1874        instrument_id: &InstrumentId,
1875    ) -> Result<InstrumentAny, InstrumentLookupError> {
1876        self.try_cache("try_instrument")?
1877            .try_instrument(instrument_id)
1878            .cloned()
1879    }
1880
1881    /// Returns the instrument IDs in the cache, optionally filtered by `venue`.
1882    ///
1883    /// # Panics
1884    ///
1885    /// Panics if the cache is already mutably borrowed.
1886    #[must_use]
1887    pub fn instrument_ids(&self, venue: Option<&Venue>) -> Vec<InstrumentId> {
1888        self.cache()
1889            .instrument_ids(venue)
1890            .into_iter()
1891            .copied()
1892            .collect()
1893    }
1894
1895    /// Returns owned copies of all instruments for the `venue`.
1896    ///
1897    /// # Panics
1898    ///
1899    /// Panics if the cache is already mutably borrowed.
1900    #[must_use]
1901    pub fn instruments(&self, venue: &Venue, underlying: Option<&Ustr>) -> Vec<InstrumentAny> {
1902        self.cache()
1903            .instruments(venue, underlying)
1904            .into_iter()
1905            .cloned()
1906            .collect()
1907    }
1908
1909    /// Returns owned copies of all instruments for the `venue`, parent `root`, and instrument
1910    /// `class`.
1911    ///
1912    /// # Panics
1913    ///
1914    /// Panics if the cache is already mutably borrowed.
1915    #[must_use]
1916    pub fn instruments_by_parent(
1917        &self,
1918        venue: &Venue,
1919        root: &Ustr,
1920        class: InstrumentClass,
1921    ) -> Vec<InstrumentAny> {
1922        self.cache()
1923            .instruments_by_parent(venue, root, class)
1924            .into_iter()
1925            .cloned()
1926            .collect()
1927    }
1928
1929    /// Returns the bar types in the cache, optionally filtered by instrument and price type.
1930    ///
1931    /// # Panics
1932    ///
1933    /// Panics if the cache is already mutably borrowed.
1934    #[must_use]
1935    pub fn bar_types(
1936        &self,
1937        instrument_id: Option<&InstrumentId>,
1938        price_type: Option<&PriceType>,
1939        aggregation_source: AggregationSource,
1940    ) -> Vec<BarType> {
1941        self.cache()
1942            .bar_types(instrument_id, price_type, aggregation_source)
1943            .into_iter()
1944            .copied()
1945            .collect()
1946    }
1947
1948    /// Returns an owned copy of the synthetic instrument for the `instrument_id` (if found).
1949    ///
1950    /// # Panics
1951    ///
1952    /// Panics if the cache is already mutably borrowed.
1953    #[must_use]
1954    pub fn synthetic(&self, instrument_id: &InstrumentId) -> Option<SyntheticInstrument> {
1955        self.cache().synthetic(instrument_id).cloned()
1956    }
1957
1958    /// Returns an owned copy of the synthetic instrument for the `instrument_id`.
1959    ///
1960    /// # Errors
1961    ///
1962    /// Returns an error if:
1963    /// - The synthetic instrument is not present
1964    ///   in the cache.
1965    /// - The cache is already mutably borrowed.
1966    pub fn try_synthetic(
1967        &self,
1968        instrument_id: &InstrumentId,
1969    ) -> Result<SyntheticInstrument, SyntheticInstrumentLookupError> {
1970        self.try_cache("try_synthetic")?
1971            .try_synthetic(instrument_id)
1972            .cloned()
1973    }
1974
1975    /// Returns the synthetic instrument IDs in the cache.
1976    ///
1977    /// # Panics
1978    ///
1979    /// Panics if the cache is already mutably borrowed.
1980    #[must_use]
1981    pub fn synthetic_ids(&self) -> Vec<InstrumentId> {
1982        self.cache().synthetic_ids().into_iter().copied().collect()
1983    }
1984
1985    /// Returns owned copies of all synthetic instruments in the cache.
1986    ///
1987    /// # Panics
1988    ///
1989    /// Panics if the cache is already mutably borrowed.
1990    #[must_use]
1991    pub fn synthetics(&self) -> Vec<SyntheticInstrument> {
1992        self.cache().synthetics().into_iter().cloned().collect()
1993    }
1994
1995    /// Returns an owned copy of the pool for the `instrument_id` (if found).
1996    ///
1997    /// # Panics
1998    ///
1999    /// Panics if the cache is already mutably borrowed.
2000    #[cfg(feature = "defi")]
2001    #[must_use]
2002    pub fn pool(&self, instrument_id: &InstrumentId) -> Option<Pool> {
2003        self.cache().pool(instrument_id).cloned()
2004    }
2005
2006    /// Returns the pool instrument IDs in the cache, optionally filtered by `venue`.
2007    ///
2008    /// # Panics
2009    ///
2010    /// Panics if the cache is already mutably borrowed.
2011    #[cfg(feature = "defi")]
2012    #[must_use]
2013    pub fn pool_ids(&self, venue: Option<&Venue>) -> Vec<InstrumentId> {
2014        self.cache().pool_ids(venue)
2015    }
2016
2017    /// Returns owned copies of all pools in the cache, optionally filtered by `venue`.
2018    ///
2019    /// # Panics
2020    ///
2021    /// Panics if the cache is already mutably borrowed.
2022    #[cfg(feature = "defi")]
2023    #[must_use]
2024    pub fn pools(&self, venue: Option<&Venue>) -> Vec<Pool> {
2025        self.cache().pools(venue).into_iter().cloned().collect()
2026    }
2027
2028    /// Returns an owned copy of the pool profiler for the `instrument_id` (if found).
2029    ///
2030    /// # Panics
2031    ///
2032    /// Panics if the cache is already mutably borrowed.
2033    #[cfg(feature = "defi")]
2034    #[must_use]
2035    pub fn pool_profiler(&self, instrument_id: &InstrumentId) -> Option<PoolProfiler> {
2036        self.cache().pool_profiler(instrument_id).cloned()
2037    }
2038
2039    /// Returns the pool profiler instrument IDs in the cache, optionally filtered by `venue`.
2040    ///
2041    /// # Panics
2042    ///
2043    /// Panics if the cache is already mutably borrowed.
2044    #[cfg(feature = "defi")]
2045    #[must_use]
2046    pub fn pool_profiler_ids(&self, venue: Option<&Venue>) -> Vec<InstrumentId> {
2047        self.cache().pool_profiler_ids(venue)
2048    }
2049
2050    /// Returns owned copies of all pool profilers in the cache, optionally filtered by `venue`.
2051    ///
2052    /// # Panics
2053    ///
2054    /// Panics if the cache is already mutably borrowed.
2055    #[cfg(feature = "defi")]
2056    #[must_use]
2057    pub fn pool_profilers(&self, venue: Option<&Venue>) -> Vec<PoolProfiler> {
2058        self.cache()
2059            .pool_profilers(venue)
2060            .into_iter()
2061            .cloned()
2062            .collect()
2063    }
2064
2065    /// Returns an owned copy of the account for the `account_id` (if found).
2066    ///
2067    /// # Panics
2068    ///
2069    /// Panics if the cache is already mutably borrowed.
2070    #[must_use]
2071    pub fn account(&self, account_id: &AccountId) -> Option<AccountAny> {
2072        self.cache().account_owned(account_id)
2073    }
2074
2075    /// Returns an owned copy of the account for the `account_id`.
2076    ///
2077    /// # Errors
2078    ///
2079    /// Returns an error if:
2080    /// - The account is not present in the cache.
2081    /// - The cache is already mutably borrowed.
2082    pub fn try_account(&self, account_id: &AccountId) -> Result<AccountAny, AccountLookupError> {
2083        self.try_cache("try_account")?
2084            .try_account(account_id)
2085            .map(|account| account.cloned())
2086    }
2087
2088    /// Returns an owned copy of the account for the `venue` (if found).
2089    ///
2090    /// Returns `None` when more than one account is issued under the `venue`.
2091    ///
2092    /// # Panics
2093    ///
2094    /// Panics if the cache is already mutably borrowed.
2095    #[must_use]
2096    pub fn account_for_venue(&self, venue: &Venue) -> Option<AccountAny> {
2097        self.cache().account_for_venue_owned(venue)
2098    }
2099
2100    /// Returns the account ID for the `venue` (if found).
2101    ///
2102    /// Returns `None` when more than one account is issued under the `venue`.
2103    ///
2104    /// # Panics
2105    ///
2106    /// Panics if the cache is already mutably borrowed.
2107    #[must_use]
2108    pub fn account_id(&self, venue: &Venue) -> Option<AccountId> {
2109        self.cache().account_id(venue).copied()
2110    }
2111
2112    /// Returns owned copies of all accounts matching the `account_id`.
2113    ///
2114    /// # Panics
2115    ///
2116    /// Panics if the cache is already mutably borrowed.
2117    #[must_use]
2118    pub fn accounts(&self, account_id: &AccountId) -> Vec<AccountAny> {
2119        self.cache()
2120            .accounts(account_id)
2121            .into_iter()
2122            .map(|account| account.cloned())
2123            .collect()
2124    }
2125
2126    /// Returns owned copies of every account in the cache.
2127    ///
2128    /// # Panics
2129    ///
2130    /// Panics if the cache is already mutably borrowed.
2131    #[must_use]
2132    pub fn accounts_all(&self) -> Vec<AccountAny> {
2133        self.cache().accounts_all_owned()
2134    }
2135
2136    fn cache(&self) -> Ref<'_, Cache> {
2137        self.try_cache("cache read")
2138            .unwrap_or_else(|e| panic!("{e}"))
2139    }
2140
2141    fn try_cache(&self, operation: &'static str) -> Result<Ref<'_, Cache>, ComponentAccessError> {
2142        self.cache
2143            .try_borrow()
2144            .map_err(|_| ComponentAccessError::ReadConflict {
2145                resource: "cache",
2146                operation,
2147            })
2148    }
2149}