-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrate variable expiration with JCache
JCache has its own expiration policy, but unfortunately differences in the API and the requirement to retain existing functionality means that the two cannot be merged yet. The settings currently allow using both variable and fixed expiration together. This is because the spec authors assumed that a maximum size would also be enabled, thereby evicting expired entries lazily. Since we exposed Caffeine's eager policy, then only for fixed, these two could be used together. With Caffeine's new variable policy, it does not allow the combination of fixed and variable in the core library. Thus, lazy-expiration cannot be mapped directly to Caffeine's version. To retain the previous combinations, we could adapt only when the fixed settings are not used. Then supply an eternal no-op policy for the lazy logic and an adapter for the eager logic. Unfortunately API differences makes this more difficult. For example, JCache's putIfAbsent does not return a value when failing and therefore is not a read. Caffeine's does, which results in TCK failures when the expiry policy calls are verified. A possible solution would be to use a ThreadLocal to signal the cases when the expiry calls should be ignored. That adds brittleness to an already complex adapter. Instead, perhaps in v3 we can remove the lazy and fixed settings, simplify the adapter, and use the ThreadLocal trick to correct these edge cases. Given that the major JCache implementors are lazy, expect a maximum, and that JCache's ExpiryPolicy is neutered by not providing the key or value, I don't think a hacky solution is worth the effort. Users can use Caffeine's expiry if they need eager behavior.
- Loading branch information
Showing
7 changed files
with
137 additions
and
22 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
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
74 changes: 74 additions & 0 deletions
74
jcache/src/test/java/com/github/benmanes/caffeine/jcache/expiry/JCacheExpiryTest.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,74 @@ | ||
/* | ||
* Copyright 2016 Ben Manes. All Rights Reserved. | ||
* | ||
* 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.github.benmanes.caffeine.jcache.expiry; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyLong; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Optional; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.mockito.Mockito; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
import com.github.benmanes.caffeine.cache.Expiry; | ||
import com.github.benmanes.caffeine.jcache.AbstractJCacheTest; | ||
import com.github.benmanes.caffeine.jcache.configuration.CaffeineConfiguration; | ||
|
||
/** | ||
* The test cases that ensure the <tt>variable expiry</tt> policy is configured. | ||
* | ||
* @author [email protected] (Ben Manes) | ||
*/ | ||
@Test(singleThreaded = true) | ||
@SuppressWarnings("unchecked") | ||
public final class JCacheExpiryTest extends AbstractJCacheTest { | ||
private static final long ONE_MINUTE = TimeUnit.MINUTES.toNanos(1); | ||
|
||
private Expiry<Integer, Integer> expiry = Mockito.mock(Expiry.class); | ||
|
||
@BeforeMethod | ||
public void setup() { | ||
Mockito.reset(expiry); | ||
when(expiry.expireAfterCreate(any(), any(), anyLong())).thenReturn(ONE_MINUTE); | ||
when(expiry.expireAfterUpdate(any(), any(), anyLong(), anyLong())).thenReturn(ONE_MINUTE); | ||
when(expiry.expireAfterRead(any(), any(), anyLong(), anyLong())).thenReturn(ONE_MINUTE); | ||
} | ||
|
||
@Override | ||
protected CaffeineConfiguration<Integer, Integer> getConfiguration() { | ||
CaffeineConfiguration<Integer, Integer> configuration = new CaffeineConfiguration<>(); | ||
configuration.setExpiryFactory(Optional.of(() -> expiry)); | ||
configuration.setTickerFactory(() -> ticker::read); | ||
return configuration; | ||
} | ||
|
||
@Test | ||
public void configured() { | ||
jcache.put(KEY_1, VALUE_1); | ||
verify(expiry, times(1)).expireAfterCreate(any(), any(), anyLong()); | ||
|
||
jcache.put(KEY_1, VALUE_2); | ||
verify(expiry).expireAfterUpdate(any(), any(), anyLong(), anyLong()); | ||
|
||
jcache.get(KEY_1); | ||
verify(expiry).expireAfterRead(any(), any(), anyLong(), anyLong()); | ||
} | ||
} |