-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
utils: add wrapper for the loading cache #9638
Open
shwstppr
wants to merge
1
commit into
apache:main
Choose a base branch
from
shapeblue:add-cache-wrapper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
48 changes: 48 additions & 0 deletions
48
utils/src/main/java/org/apache/cloudstack/utils/cache/LazyCache.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,48 @@ | ||
// 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.cloudstack.utils.cache; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Function; | ||
|
||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import com.github.benmanes.caffeine.cache.LoadingCache; | ||
|
||
public class LazyCache<K, V> { | ||
|
||
private final LoadingCache<K, V> cache; | ||
|
||
public LazyCache(long maximumSize, long expireAfterWriteSeconds, Function<K, V> loader) { | ||
this.cache = Caffeine.newBuilder() | ||
.maximumSize(maximumSize) | ||
.expireAfterWrite(expireAfterWriteSeconds, TimeUnit.SECONDS) | ||
.build(loader::apply); | ||
} | ||
|
||
public V get(K key) { | ||
return cache.get(key); | ||
} | ||
|
||
public void invalidate(K key) { | ||
cache.invalidate(key); | ||
} | ||
|
||
public void clear() { | ||
cache.invalidateAll(); | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
utils/src/test/java/org/apache/cloudstack/utils/cache/LazyCacheTest.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,115 @@ | ||
// 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.cloudstack.utils.cache; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.function.Function; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class LazyCacheTest { | ||
private final long expireSeconds = 1; | ||
private final String cacheValuePrefix = "ComputedValueFor:"; | ||
private LazyCache<String, String> cache; | ||
private Function<String, String> mockLoader; | ||
|
||
@Before | ||
public void setUp() { | ||
mockLoader = Mockito.mock(Function.class); | ||
Mockito.when(mockLoader.apply(Mockito.anyString())).thenAnswer(invocation -> cacheValuePrefix + invocation.getArgument(0)); | ||
cache = new LazyCache<>(4, expireSeconds, mockLoader); | ||
} | ||
|
||
@Test | ||
public void testCacheMissAndLoader() { | ||
String key = "key1"; | ||
String value = cache.get(key); | ||
assertEquals(cacheValuePrefix + key, value); | ||
Mockito.verify(mockLoader).apply(key); | ||
} | ||
|
||
@Test | ||
public void testLoaderNotCalledIfPresent() { | ||
String key = "key2"; | ||
cache.get(key); | ||
try { | ||
Thread.sleep((long)(0.9 * expireSeconds * 1000)); | ||
} catch (InterruptedException ie) { | ||
Assert.fail(String.format("Exception occurred: %s", ie.getMessage())); | ||
} | ||
cache.get(key); | ||
Mockito.verify(mockLoader, Mockito.times(1)).apply(key); | ||
} | ||
|
||
@Test | ||
public void testCacheExpiration() { | ||
String key = "key3"; | ||
cache.get(key); | ||
try { | ||
Thread.sleep((long)(1.1 * expireSeconds * 1000)); | ||
} catch (InterruptedException ie) { | ||
Assert.fail(String.format("Exception occurred: %s", ie.getMessage())); | ||
} | ||
cache.get(key); | ||
Mockito.verify(mockLoader, Mockito.times(2)).apply(key); | ||
} | ||
|
||
@Test | ||
public void testInvalidateKey() { | ||
String key = "key4"; | ||
cache.get(key); | ||
cache.invalidate(key); | ||
cache.get(key); | ||
Mockito.verify(mockLoader, Mockito.times(2)).apply(key); | ||
} | ||
|
||
@Test | ||
public void testClearCache() { | ||
String key1 = "key5"; | ||
String key2 = "key6"; | ||
cache.get(key1); | ||
cache.get(key2); | ||
cache.clear(); | ||
cache.get(key1); | ||
Mockito.verify(mockLoader, Mockito.times(2)).apply(key1); | ||
Mockito.verify(mockLoader, Mockito.times(1)).apply(key2); | ||
} | ||
|
||
@Test | ||
public void testMaximumSize() { | ||
String key = "key7"; | ||
cache.get(key); | ||
for (int i = 0; i < 4; i++) { | ||
cache.get(String.format("newkey-%d", i)); | ||
} | ||
try { | ||
Thread.sleep(100); | ||
} catch (InterruptedException ie) { | ||
Assert.fail(String.format("Exception occurred: %s", ie.getMessage())); | ||
} | ||
cache.get(key); | ||
Mockito.verify(mockLoader, Mockito.times(2)).apply(key); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor nit - CacheUtils ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
intentionally didn't use *Utils as we are/will be creating objects of this class unlike other static Utils classes. Any other suggestions maybe?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about
org.apache.cloudstack.utils.Cache
, @shwstppr ?