Merge pull request #1027 from sulake/feature/bounded_cache

Add support for bounded Redisson map cache (max size configuration).
pull/1028/head
Nikita Koksharov 8 years ago committed by GitHub
commit aa2314f8f2

File diff suppressed because it is too large Load Diff

@ -49,6 +49,7 @@ public class MapOptions<K, V> {
private MapWriter<K, V> writer;
private WriteMode writeMode = WriteMode.WRITE_THROUGH;
private int writeBehindThreads = 1;
private int maxSize = 0;
protected MapOptions() {
}
@ -134,5 +135,20 @@ public class MapOptions<K, V> {
public MapLoader<K, V> getLoader() {
return loader;
}
/**
* Sets max size of the map.
* <p>
* Currently only RedissonMapCache is supported.
*
* @param maxSize - max size
* @return MapOptions instance
*/
public MapOptions<K, V> maxSize(int maxSize) {
this.maxSize = maxSize;
return this;
}
public int getMaxSize() {
return maxSize;
}
}

@ -215,6 +215,31 @@ public class RedissonMapCacheTest extends BaseMapTest {
expected.put("3", "33");
assertThat(store).isEqualTo(expected);
}
@Test
public void testFastPutMaxSize() {
final int maxSize = 2;
Map<String, String> store = new LinkedHashMap<String, String>() {
@Override
protected boolean removeEldestEntry(Entry<String, String> eldest) {
return size() > maxSize;
}
};
MapOptions<String, String> options = MapOptions.<String, String>defaults().writer(createMapWriter(store));
options.maxSize(maxSize);
RMapCache<String, String> map = redisson.getMapCache("test", options);
map.fastPut("1", "11", 10, TimeUnit.SECONDS);
map.fastPut("2", "22", 10, TimeUnit.SECONDS);
map.fastPut("3", "33", 10, TimeUnit.SECONDS);
assertThat(map.size()).isEqualTo(maxSize);
Map<String, String> expected = new HashMap<>();
expected.put("2", "22");
expected.put("3", "33");
assertThat(store).isEqualTo(expected);
}
@Test
public void testOrdering() {

Loading…
Cancel
Save