forked from apache/doris
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f50d7d
commit 60fce37
Showing
8 changed files
with
141 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...a-extensions/java-common/src/main/java/org/apache/doris/common/jni/utils/ExpiringMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// 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. | ||
|
||
package org.apache.doris.common.jni.utils; | ||
|
||
import org.apache.log4j.Logger; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class ExpiringMap<K, V> { | ||
private final ConcurrentHashMap<K, V> map = new ConcurrentHashMap<>(); // key --> value | ||
private final ConcurrentHashMap<K, Long> ttlMap = new ConcurrentHashMap<>(); // key --> ttl interval | ||
// key --> expirationTime(ttl interval + currentTimeMillis) | ||
private final ConcurrentHashMap<K, Long> expirationMap = new ConcurrentHashMap<>(); | ||
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); | ||
private static final long DEFAULT_INTERVAL_TIME = 10 * 60 * 1000L; // 10 minutes | ||
public static final Logger LOG = Logger.getLogger(ExpiringMap.class); | ||
|
||
public ExpiringMap() { | ||
startExpirationTask(); | ||
} | ||
|
||
public void put(K key, V value, long expirationTimeMs) { | ||
long expirationTime = System.currentTimeMillis() + expirationTimeMs; | ||
map.put(key, value); | ||
expirationMap.put(key, expirationTime); | ||
ttlMap.put(key, expirationTimeMs); | ||
} | ||
|
||
public V get(K key) { | ||
Long expirationTime = expirationMap.get(key); | ||
if (expirationTime == null || System.currentTimeMillis() > expirationTime) { | ||
map.remove(key); | ||
expirationMap.remove(key); | ||
ttlMap.remove(key); | ||
return null; | ||
} | ||
// reset time again | ||
long ttl = ttlMap.get(key); | ||
long newExpirationTime = System.currentTimeMillis() + ttl; | ||
expirationMap.put(key, newExpirationTime); | ||
return map.get(key); | ||
} | ||
|
||
private void startExpirationTask() { | ||
scheduler.scheduleAtFixedRate(() -> { | ||
long now = System.currentTimeMillis(); | ||
for (K key : expirationMap.keySet()) { | ||
if (expirationMap.get(key) <= now) { | ||
map.remove(key); | ||
expirationMap.remove(key); | ||
ttlMap.remove(key); | ||
} | ||
} | ||
}, DEFAULT_INTERVAL_TIME, DEFAULT_INTERVAL_TIME, TimeUnit.MINUTES); | ||
} | ||
|
||
public void remove(K key) { | ||
map.remove(key); | ||
expirationMap.remove(key); | ||
ttlMap.remove(key); | ||
} | ||
|
||
public void shutdown() { | ||
scheduler.shutdown(); | ||
try { | ||
if (!scheduler.awaitTermination(60, TimeUnit.SECONDS)) { | ||
scheduler.shutdownNow(); | ||
} | ||
} catch (InterruptedException e) { | ||
scheduler.shutdownNow(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters