JingsongLi commented on code in PR #192:
URL: https://github.com/apache/paimon-rust/pull/192#discussion_r3031712833


##########
crates/paimon/src/catalog/rest/rest_token_file_io.rs:
##########
@@ -20,30 +20,63 @@
 //! This module provides a FileIO wrapper that supports getting data access
 //! tokens from a REST Server. It handles token caching, expiration detection,
 //! and automatic refresh.
+//!
+//! Unlike the previous implementation that only refreshed tokens during
+//! `build_file_io()`, this implementation implements `FileIOProvider` and
+//! checks token validity before each file operation, matching the Java
+//! implementation behavior.
 
 use std::collections::HashMap;
+use std::sync::OnceLock;
+use std::time::Duration;
 
+use moka::future::Cache;
 use tokio::sync::{OnceCell, RwLock};
 
 use crate::api::rest_api::RESTApi;
 use crate::api::rest_util::RESTUtil;
 use crate::catalog::Identifier;
 use crate::common::{CatalogOptions, Options};
 use crate::io::storage_oss::OSS_ENDPOINT;
-use crate::io::FileIO;
-use crate::Result;
+use crate::io::{FileIO, FileIOProvider, FileStatus, InputFile, OutputFile};
+use crate::{Error, Result};
 
 use super::rest_token::RESTToken;
 
 /// Safe time margin (in milliseconds) before token expiration to trigger 
refresh.
 const TOKEN_EXPIRATION_SAFE_TIME_MILLIS: i64 = 3_600_000;
 
+/// Maximum number of entries in the global FileIO cache.
+const FILE_IO_CACHE_MAX_CAPACITY: u64 = 1000;
+/// Time-to-live for cache entries in seconds (10 hours).
+const FILE_IO_CACHE_TTL_SECS: u64 = 10 * 60 * 60;
+
+/// Global static FileIO cache, similar to Java's Caffeine cache.
+///
+/// This cache stores FileIO instances keyed by their corresponding RESTToken.
+/// Features:
+/// - max_capacity: 1000 entries
+/// - time_to_live: 10 hours (entries expire after this duration)
+/// - thread-safe via moka's internal synchronization
+static FILE_IO_CACHE: OnceLock<Cache<RESTToken, FileIO>> = OnceLock::new();
+
+/// Get the global FileIO cache, initializing it if necessary.
+fn get_file_io_cache() -> &'static Cache<RESTToken, FileIO> {
+    FILE_IO_CACHE.get_or_init(|| {
+        Cache::builder()
+            .max_capacity(FILE_IO_CACHE_MAX_CAPACITY)
+            .time_to_live(Duration::from_secs(FILE_IO_CACHE_TTL_SECS))
+            .build()
+    })
+}
+
 /// A FileIO wrapper that supports getting data access tokens from a REST 
Server.
 ///
 /// This struct handles:
 /// - Token caching with expiration detection
 /// - Automatic token refresh via `RESTApi::load_table_token`
 /// - Merging token credentials into catalog options to build the underlying 
`FileIO`
+/// - FileIO caching based on token to avoid rebuilding FileIO unnecessarily
 pub struct RESTTokenFileIO {

Review Comment:
   No Clone for this?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to