-
Notifications
You must be signed in to change notification settings - Fork 32
feat: Added a generic LRUCache interface and a default implementation #482
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
21d74d4
Added LRUCache and unit tests
zashraf1985 8fbd398
Added peek
zashraf1985 46f0004
Changed the implementation to use LinkedHashMap
zashraf1985 3fda8cb
added a unit test to make sure `save` also renews the item
zashraf1985 1763ac2
Added a line break for better formatting
zashraf1985 1611a86
incorporated review feedback
zashraf1985 c63affd
Fixed a spotBugs issue
zashraf1985 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
27 changes: 27 additions & 0 deletions
27
core-api/src/main/java/com/optimizely/ab/internal/Cache.java
This file contains hidden or 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,27 @@ | ||
/** | ||
* | ||
* Copyright 2022, Optimizely | ||
* | ||
* Licensed 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 com.optimizely.ab.internal; | ||
|
||
public interface Cache<T> { | ||
int DEFAULT_MAX_SIZE = 10000; | ||
int DEFAULT_TIMEOUT_SECONDS = 600; | ||
void save(String key, T value); | ||
T lookup(String key); | ||
void reset(); | ||
void setMaxSize(Integer size); | ||
void setTimeout(Long timeoutSeconds); | ||
} |
117 changes: 117 additions & 0 deletions
117
core-api/src/main/java/com/optimizely/ab/internal/DefaultLRUCache.java
This file contains hidden or 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,117 @@ | ||
/** | ||
* | ||
* Copyright 2022, Optimizely | ||
* | ||
* Licensed 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 com.optimizely.ab.internal; | ||
|
||
import com.optimizely.ab.annotations.VisibleForTesting; | ||
import com.optimizely.ab.config.parser.DefaultConfigParser; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.*; | ||
|
||
public class DefaultLRUCache<T> implements Cache<T> { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(DefaultLRUCache.class); | ||
|
||
private final Object lock = new Object(); | ||
|
||
private Integer maxSize; | ||
|
||
private Long timeoutMillis; | ||
|
||
@VisibleForTesting | ||
final LinkedHashMap<String, ItemWrapper> linkedHashMap = new LinkedHashMap<String, ItemWrapper>(16, 0.75f, true) { | ||
@Override | ||
protected boolean removeEldestEntry(Map.Entry<String, ItemWrapper> eldest) { | ||
return this.size() > maxSize; | ||
} | ||
}; | ||
|
||
public DefaultLRUCache() { | ||
this.maxSize = DEFAULT_MAX_SIZE; | ||
this.timeoutMillis = (long) (DEFAULT_TIMEOUT_SECONDS * 1000); | ||
} | ||
|
||
public void setMaxSize(Integer size) { | ||
if (linkedHashMap.size() > 0) { | ||
if (size >= linkedHashMap.size()) { | ||
maxSize = size; | ||
} else { | ||
logger.warn("Cannot set max cache size less than current size."); | ||
} | ||
} else { | ||
Integer sizeToSet = size; | ||
if (size < 0) { | ||
sizeToSet = 0; | ||
} | ||
maxSize = sizeToSet; | ||
} | ||
} | ||
|
||
public void setTimeout(Long timeoutSeconds) { | ||
this.timeoutMillis = timeoutSeconds * 1000; | ||
} | ||
|
||
public void save(String key, T value) { | ||
if (maxSize == 0) { | ||
// Cache is disabled when maxSize = 0 | ||
return; | ||
} | ||
|
||
synchronized (lock) { | ||
linkedHashMap.put(key, new ItemWrapper(value)); | ||
} | ||
} | ||
|
||
public T lookup(String key) { | ||
if (maxSize == 0) { | ||
// Cache is disabled when maxSize = 0 | ||
return null; | ||
} | ||
|
||
synchronized (lock) { | ||
if (linkedHashMap.containsKey(key)) { | ||
ItemWrapper item = linkedHashMap.get(key); | ||
Long nowMs = new Date().getTime(); | ||
|
||
// ttl = 0 means items never expire. | ||
if (timeoutMillis == 0 || (nowMs - item.timestamp < timeoutMillis)) { | ||
return item.value; | ||
} | ||
|
||
linkedHashMap.remove(key); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
public void reset() { | ||
synchronized (lock) { | ||
linkedHashMap.clear(); | ||
} | ||
} | ||
|
||
private class ItemWrapper { | ||
public T value; | ||
public Long timestamp; | ||
|
||
public ItemWrapper(T value) { | ||
this.value = value; | ||
this.timestamp = new Date().getTime(); | ||
} | ||
} | ||
} |
204 changes: 204 additions & 0 deletions
204
core-api/src/test/java/com/optimizely/ab/internal/DefaultLRUCacheTest.java
This file contains hidden or 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,204 @@ | ||
/** | ||
* | ||
* Copyright 2022, Optimizely | ||
* | ||
* Licensed 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 com.optimizely.ab.internal; | ||
|
||
import ch.qos.logback.classic.Level; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class DefaultLRUCacheTest { | ||
@Rule | ||
public LogbackVerifier logbackVerifier = new LogbackVerifier(); | ||
|
||
@Test | ||
public void createSaveAndLookupOneItem() { | ||
Cache<String> cache = new DefaultLRUCache<>(); | ||
assertNull(cache.lookup("key1")); | ||
cache.save("key1", "value1"); | ||
assertEquals("value1", cache.lookup("key1")); | ||
} | ||
|
||
@Test | ||
public void saveAndLookupMultipleItems() { | ||
DefaultLRUCache<List<String>> cache = new DefaultLRUCache<>(); | ||
|
||
cache.save("user1", Arrays.asList("segment1", "segment2")); | ||
cache.save("user2", Arrays.asList("segment3", "segment4")); | ||
cache.save("user3", Arrays.asList("segment5", "segment6")); | ||
|
||
String[] itemKeys = cache.linkedHashMap.keySet().toArray(new String[0]); | ||
assertEquals("user1", itemKeys[0]); | ||
assertEquals("user2", itemKeys[1]); | ||
assertEquals("user3", itemKeys[2]); | ||
|
||
assertEquals(Arrays.asList("segment1", "segment2"), cache.lookup("user1")); | ||
|
||
itemKeys = cache.linkedHashMap.keySet().toArray(new String[0]); | ||
// Lookup should move user1 to bottom of the list and push up others. | ||
assertEquals("user2", itemKeys[0]); | ||
assertEquals("user3", itemKeys[1]); | ||
assertEquals("user1", itemKeys[2]); | ||
|
||
assertEquals(Arrays.asList("segment3", "segment4"), cache.lookup("user2")); | ||
|
||
itemKeys = cache.linkedHashMap.keySet().toArray(new String[0]); | ||
// Lookup should move user2 to bottom of the list and push up others. | ||
assertEquals("user3", itemKeys[0]); | ||
assertEquals("user1", itemKeys[1]); | ||
assertEquals("user2", itemKeys[2]); | ||
|
||
assertEquals(Arrays.asList("segment5", "segment6"), cache.lookup("user3")); | ||
|
||
itemKeys = cache.linkedHashMap.keySet().toArray(new String[0]); | ||
// Lookup should move user3 to bottom of the list and push up others. | ||
assertEquals("user1", itemKeys[0]); | ||
assertEquals("user2", itemKeys[1]); | ||
assertEquals("user3", itemKeys[2]); | ||
} | ||
|
||
@Test | ||
public void saveShouldReorderList() { | ||
DefaultLRUCache<List<String>> cache = new DefaultLRUCache<>(); | ||
|
||
cache.save("user1", Arrays.asList("segment1", "segment2")); | ||
cache.save("user2", Arrays.asList("segment3", "segment4")); | ||
cache.save("user3", Arrays.asList("segment5", "segment6")); | ||
|
||
String[] itemKeys = cache.linkedHashMap.keySet().toArray(new String[0]); | ||
assertEquals("user1", itemKeys[0]); | ||
assertEquals("user2", itemKeys[1]); | ||
assertEquals("user3", itemKeys[2]); | ||
|
||
cache.save("user1", Arrays.asList("segment1", "segment2")); | ||
|
||
itemKeys = cache.linkedHashMap.keySet().toArray(new String[0]); | ||
// save should move user1 to bottom of the list and push up others. | ||
assertEquals("user2", itemKeys[0]); | ||
assertEquals("user3", itemKeys[1]); | ||
assertEquals("user1", itemKeys[2]); | ||
|
||
cache.save("user2", Arrays.asList("segment3", "segment4")); | ||
|
||
itemKeys = cache.linkedHashMap.keySet().toArray(new String[0]); | ||
// save should move user2 to bottom of the list and push up others. | ||
assertEquals("user3", itemKeys[0]); | ||
assertEquals("user1", itemKeys[1]); | ||
assertEquals("user2", itemKeys[2]); | ||
|
||
cache.save("user3", Arrays.asList("segment5", "segment6")); | ||
|
||
itemKeys = cache.linkedHashMap.keySet().toArray(new String[0]); | ||
// save should move user3 to bottom of the list and push up others. | ||
assertEquals("user1", itemKeys[0]); | ||
assertEquals("user2", itemKeys[1]); | ||
assertEquals("user3", itemKeys[2]); | ||
} | ||
|
||
@Test | ||
public void whenCacheIsDisabled() { | ||
DefaultLRUCache<List<String>> cache = new DefaultLRUCache<>(); | ||
cache.setMaxSize(0); | ||
cache.save("user1", Arrays.asList("segment1", "segment2")); | ||
cache.save("user2", Arrays.asList("segment3", "segment4")); | ||
cache.save("user3", Arrays.asList("segment5", "segment6")); | ||
|
||
assertNull(cache.lookup("user1")); | ||
assertNull(cache.lookup("user2")); | ||
assertNull(cache.lookup("user3")); | ||
} | ||
|
||
@Test | ||
public void whenItemsExpire() throws InterruptedException { | ||
DefaultLRUCache<List<String>> cache = new DefaultLRUCache<>(); | ||
cache.setTimeout(1L); | ||
cache.save("user1", Arrays.asList("segment1", "segment2")); | ||
assertEquals(Arrays.asList("segment1", "segment2"), cache.lookup("user1")); | ||
assertEquals(1, cache.linkedHashMap.size()); | ||
Thread.sleep(1000); | ||
assertNull(cache.lookup("user1")); | ||
assertEquals(0, cache.linkedHashMap.size()); | ||
} | ||
|
||
@Test | ||
public void whenCacheReachesMaxSize() { | ||
DefaultLRUCache<List<String>> cache = new DefaultLRUCache<>(); | ||
cache.setMaxSize(2); | ||
|
||
cache.save("user1", Arrays.asList("segment1", "segment2")); | ||
cache.save("user2", Arrays.asList("segment3", "segment4")); | ||
cache.save("user3", Arrays.asList("segment5", "segment6")); | ||
|
||
assertEquals(2, cache.linkedHashMap.size()); | ||
|
||
assertEquals(Arrays.asList("segment5", "segment6"), cache.lookup("user3")); | ||
assertEquals(Arrays.asList("segment3", "segment4"), cache.lookup("user2")); | ||
assertNull(cache.lookup("user1")); | ||
} | ||
|
||
@Test | ||
public void whenMaxSizeIsReducedInBetween() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove this test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modified the test to check the changed implementation |
||
DefaultLRUCache<List<String>> cache = new DefaultLRUCache<>(); | ||
cache.save("user1", Arrays.asList("segment1", "segment2")); | ||
cache.save("user2", Arrays.asList("segment3", "segment4")); | ||
cache.save("user3", Arrays.asList("segment5", "segment6")); | ||
|
||
assertEquals(Arrays.asList("segment1", "segment2"), cache.lookup("user1")); | ||
assertEquals(Arrays.asList("segment3", "segment4"), cache.lookup("user2")); | ||
assertEquals(Arrays.asList("segment5", "segment6"), cache.lookup("user3")); | ||
|
||
assertEquals(3, cache.linkedHashMap.size()); | ||
|
||
cache.setMaxSize(1); | ||
|
||
logbackVerifier.expectMessage(Level.WARN, "Cannot set max cache size less than current size."); | ||
|
||
assertEquals(Arrays.asList("segment5", "segment6"), cache.lookup("user3")); | ||
assertEquals(Arrays.asList("segment3", "segment4"), cache.lookup("user2")); | ||
assertEquals(Arrays.asList("segment5", "segment6"), cache.lookup("user3")); | ||
|
||
assertEquals(3, cache.linkedHashMap.size()); | ||
} | ||
|
||
@Test | ||
public void whenCacheIsReset() { | ||
DefaultLRUCache<List<String>> cache = new DefaultLRUCache<>(); | ||
cache.save("user1", Arrays.asList("segment1", "segment2")); | ||
cache.save("user2", Arrays.asList("segment3", "segment4")); | ||
cache.save("user3", Arrays.asList("segment5", "segment6")); | ||
|
||
assertEquals(Arrays.asList("segment1", "segment2"), cache.lookup("user1")); | ||
assertEquals(Arrays.asList("segment3", "segment4"), cache.lookup("user2")); | ||
assertEquals(Arrays.asList("segment5", "segment6"), cache.lookup("user3")); | ||
|
||
assertEquals(3, cache.linkedHashMap.size()); | ||
|
||
cache.reset(); | ||
|
||
assertNull(cache.lookup("user1")); | ||
assertNull(cache.lookup("user2")); | ||
assertNull(cache.lookup("user3")); | ||
|
||
assertEquals(0, cache.linkedHashMap.size()); | ||
} | ||
} |
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.
This test looks good. It will be good to have the same ordering switch tests for save ops as well.
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.
Done!