mkleen commented on code in PR #22613:
URL: https://github.com/apache/datafusion/pull/22613#discussion_r3323042460


##########
datafusion/execution/src/cache/default_cache.rs:
##########
@@ -0,0 +1,310 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+use std::sync::{Arc, Mutex};
+use std::time::Duration;
+
+use datafusion_common::TableReference;
+use datafusion_common::instant::Instant;
+use datafusion_common::{HashMap, Result};
+
+use crate::cache::lru_queue::LruQueue;
+use crate::cache::{Cache, CacheAccessor, CacheEntryInfo, Key, Value};
+
+/// Source of the current time used by a [`DefaultCache`] when applying TTLs.
+pub trait TimeProvider: Send + Sync {
+    /// Return the current instant.
+    fn now(&self) -> Instant;
+}
+
+/// [`TimeProvider`] backed by [`Instant::now`].
+///
+/// This is the default time source used by [`DefaultCache`]
+#[derive(Debug, Default)]
+pub struct SystemTimeProvider;
+
+impl TimeProvider for SystemTimeProvider {
+    fn now(&self) -> Instant {
+        Instant::now()
+    }
+}
+
+#[derive(Clone)]
+struct ValueEntry<V: Value> {
+    value: V,
+    expires: Option<Instant>,
+}
+
+struct DefaultCacheState<K: Key, V: Value> {
+    lru_queue: LruQueue<K, ValueEntry<V>>,
+    hits: HashMap<K, usize>,
+    memory_limit: usize,
+    memory_used: usize,
+    ttl: Option<Duration>,
+}
+
+impl<K: Key, V: Value> DefaultCacheState<K, V> {
+    fn new(memory_limit: usize, ttl: Option<Duration>) -> Self {
+        Self {
+            lru_queue: LruQueue::new(),
+            hits: HashMap::new(),
+            memory_limit,
+            memory_used: 0,
+            ttl,
+        }
+    }
+
+    fn get(&mut self, key: &K, now: Instant) -> Option<V> {
+        let entry = self.lru_queue.get(key)?;
+        if let Some(exp) = entry.expires
+            && now > exp
+        {
+            self.remove(key);
+            return None;
+        }
+        let value = entry.value.clone();
+        *self.hits.entry(key.clone()).or_insert(0) += 1;
+        Some(value)
+    }
+
+    fn contains_key(&mut self, key: &K, now: Instant) -> bool {
+        let Some(entry) = self.lru_queue.peek(key) else {
+            return false;
+        };
+        match entry.expires {
+            Some(exp) if now > exp => {
+                self.remove(key);
+                false
+            }
+            _ => true,
+        }
+    }
+
+    fn put(&mut self, key: &K, value: V, now: Instant) -> Option<V> {
+        let value_size = value.size();
+
+        if value_size == 0 {

Review Comment:
   List-files-cache rejected empty values which is needed to operate correctly.



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to