Java源码示例:org.redisson.api.RMapCache
示例1
public BaseRegion(RMapCache<Object, Object> mapCache, ConnectionManager connectionManager, RegionFactory regionFactory, CacheDataDescription metadata, Properties properties, String defaultKey) {
super();
this.mapCache = mapCache;
this.regionFactory = regionFactory;
this.metadata = metadata;
this.connectionManager = connectionManager;
String maxEntries = getProperty(properties, mapCache.getName(), defaultKey, RedissonRegionFactory.MAX_ENTRIES_SUFFIX);
if (maxEntries != null) {
mapCache.setMaxSize(Integer.valueOf(maxEntries));
}
String timeToLive = getProperty(properties, mapCache.getName(), defaultKey, RedissonRegionFactory.TTL_SUFFIX);
if (timeToLive != null) {
ttl = Integer.valueOf(timeToLive);
}
String maxIdleTime = getProperty(properties, mapCache.getName(), defaultKey, RedissonRegionFactory.MAX_IDLE_SUFFIX);
if (maxIdleTime != null) {
maxIdle = Integer.valueOf(maxIdleTime);
}
String fallbackValue = (String) properties.getOrDefault(RedissonRegionFactory.FALLBACK, "false");
fallback = Boolean.valueOf(fallbackValue);
}
示例2
@Test
public void testCreatedListener() {
RMapCache<Integer, Integer> map = redisson.getMapCache("simple");
checkCreatedListener(map, 1, 2, () -> map.put(1, 2));
checkCreatedListener(map, 10, 2, () -> map.put(10, 2, 2, TimeUnit.SECONDS));
checkCreatedListener(map, 2, 5, () -> map.fastPut(2, 5));
checkCreatedListener(map, 13, 2, () -> map.fastPut(13, 2, 2, TimeUnit.SECONDS));
checkCreatedListener(map, 3, 2, () -> map.putIfAbsent(3, 2));
checkCreatedListener(map, 14, 2, () -> map.putIfAbsent(14, 2, 2, TimeUnit.SECONDS));
checkCreatedListener(map, 4, 1, () -> map.fastPutIfAbsent(4, 1));
checkCreatedListener(map, 15, 2, () -> map.fastPutIfAbsent(15, 2, 2, TimeUnit.SECONDS));
map.destroy();
RMapCache<Integer, Integer> map2 = redisson.getMapCache("simple3", new CompositeCodec(redisson.getConfig().getCodec(), IntegerCodec.INSTANCE));
checkCreatedListener(map2, 5, 10, () -> map2.addAndGet(5, 10));
map2.destroy();
}
示例3
private void checkCreatedListener(RMapCache<Integer, Integer> map, Integer key, Integer value, Runnable runnable) {
AtomicBoolean ref = new AtomicBoolean();
int createListener1 = map.addListener(new EntryCreatedListener<Integer, Integer>() {
@Override
public void onCreated(EntryEvent<Integer, Integer> event) {
try {
assertThat(event.getKey()).isEqualTo(key);
assertThat(event.getValue()).isEqualTo(value);
if (!ref.compareAndSet(false, true)) {
Assert.fail();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
runnable.run();
await().atMost(Duration.ONE_SECOND).untilTrue(ref);
map.removeListener(createListener1);
map.destroy();
}
示例4
@Test
public void testAddAndGetTTL() {
RMapCache<String, Object> mapCache = redisson.getMapCache("test_put_if_absent", LongCodec.INSTANCE);
assertThat(mapCache.putIfAbsent("4", 0L, 10000L, TimeUnit.SECONDS)).isNull();
assertThat(mapCache.addAndGet("4", 1L)).isEqualTo(1L);
assertThat(mapCache.putIfAbsent("4", 0L)).isEqualTo(1L);
Assert.assertEquals(1L, mapCache.get("4"));
mapCache.destroy();
mapCache = redisson.getMapCache("test_put_if_absent_1", LongCodec.INSTANCE);
mapCache.putIfAbsent("4", 0L);
mapCache.addAndGet("4", 1L);
mapCache.putIfAbsent("4", 0L);
Assert.assertEquals(1L, mapCache.get("4"));
RMap map = redisson.getMap("test_put_if_absent_2", LongCodec.INSTANCE);
map.putIfAbsent("4", 0L);
map.addAndGet("4", 1L);
map.putIfAbsent("4", 0L);
Assert.assertEquals(1L, map.get("4"));
RMapCache<String, Object> mapCache1 = redisson.getMapCache("test_put_if_absent_3", DoubleCodec.INSTANCE);
mapCache1.putIfAbsent("4", 1.23, 10000L, TimeUnit.SECONDS);
mapCache1.addAndGet("4", 1D);
Assert.assertEquals(2.23, mapCache1.get("4"));
mapCache.destroy();
mapCache1.destroy();
}
示例5
public RedissonStorage(RMapCache<Object, Object> mapCache, ConnectionManager connectionManager, Map<String, Object> properties, String defaultKey) {
super();
this.mapCache = mapCache;
this.connectionManager = connectionManager;
String maxEntries = getProperty(properties, mapCache.getName(), defaultKey, RedissonRegionFactory.MAX_ENTRIES_SUFFIX);
if (maxEntries != null) {
mapCache.setMaxSize(Integer.valueOf(maxEntries));
}
String timeToLive = getProperty(properties, mapCache.getName(), defaultKey, RedissonRegionFactory.TTL_SUFFIX);
if (timeToLive != null) {
ttl = Integer.valueOf(timeToLive);
}
String maxIdleTime = getProperty(properties, mapCache.getName(), defaultKey, RedissonRegionFactory.MAX_IDLE_SUFFIX);
if (maxIdleTime != null) {
maxIdle = Integer.valueOf(maxIdleTime);
}
String fallbackValue = (String) properties.getOrDefault(RedissonRegionFactory.FALLBACK, "false");
fallback = Boolean.valueOf(fallbackValue);
}
示例6
@Override
protected DomainDataStorageAccess createDomainDataStorageAccess(DomainDataRegionConfig regionConfig,
DomainDataRegionBuildingContext buildingContext) {
String defaultKey = null;
if (!regionConfig.getCollectionCaching().isEmpty()) {
defaultKey = COLLECTION_DEF;
} else if (!regionConfig.getEntityCaching().isEmpty()) {
defaultKey = ENTITY_DEF;
} else if (!regionConfig.getNaturalIdCaching().isEmpty()) {
defaultKey = NATURAL_ID_DEF;
} else {
throw new IllegalArgumentException("Unable to determine entity cache type!");
}
RMapCache<Object, Object> mapCache = getCache(regionConfig.getRegionName(), buildingContext.getSessionFactory().getProperties(), defaultKey);
return new RedissonStorage(mapCache, ((Redisson)redisson).getConnectionManager(), buildingContext.getSessionFactory().getProperties(), defaultKey);
}
示例7
@Test
public void testFastPutIfAbsentWithTTL() throws Exception {
RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simpleTTL");
SimpleKey key = new SimpleKey("1");
SimpleValue value = new SimpleValue("2");
map.fastPutIfAbsent(key, value, 1, TimeUnit.SECONDS);
assertThat(map.fastPutIfAbsent(key, new SimpleValue("3"), 1, TimeUnit.SECONDS)).isFalse();
assertThat(map.get(key)).isEqualTo(value);
Thread.sleep(1100);
assertThat(map.fastPutIfAbsent(key, new SimpleValue("3"), 1, TimeUnit.SECONDS)).isTrue();
assertThat(map.get(key)).isEqualTo(new SimpleValue("3"));
assertThat(map.fastPutIfAbsent(key, new SimpleValue("4"), 1, TimeUnit.SECONDS)).isFalse();
assertThat(map.get(key)).isEqualTo(new SimpleValue("3"));
Thread.sleep(1100);
assertThat(map.fastPutIfAbsent(key, new SimpleValue("4"), 1, TimeUnit.SECONDS, 500, TimeUnit.MILLISECONDS)).isTrue();
Thread.sleep(550);
assertThat(map.fastPutIfAbsent(key, new SimpleValue("5"), 1, TimeUnit.SECONDS, 500, TimeUnit.MILLISECONDS)).isTrue();
map.destroy();
}
示例8
@Test
public void testWriterFastPutTTL() {
Map<String, String> store = new HashMap<>();
RMapCache<String, String> map = (RMapCache<String, String>) getWriterTestMap("test", store);
map.fastPut("1", "11", 10, TimeUnit.SECONDS);
map.fastPut("2", "22", 10, TimeUnit.SECONDS);
map.fastPut("3", "33", 10, TimeUnit.SECONDS);
Map<String, String> expected = new HashMap<>();
expected.put("1", "11");
expected.put("2", "22");
expected.put("3", "33");
assertThat(store).isEqualTo(expected);
map.destroy();
}
示例9
@Test
public void testKeySet() throws InterruptedException {
RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple03");
map.put(new SimpleKey("33"), new SimpleValue("44"), 1, TimeUnit.SECONDS);
map.put(new SimpleKey("1"), new SimpleValue("2"));
Assert.assertTrue(map.keySet().contains(new SimpleKey("33")));
Assert.assertFalse(map.keySet().contains(new SimpleKey("44")));
Assert.assertTrue(map.keySet().contains(new SimpleKey("1")));
Thread.sleep(1000);
Assert.assertFalse(map.keySet().contains(new SimpleKey("33")));
Assert.assertFalse(map.keySet().contains(new SimpleKey("44")));
Assert.assertTrue(map.keySet().contains(new SimpleKey("1")));
map.destroy();
}
示例10
@Test
public void testExpireOverwrite() throws InterruptedException, ExecutionException {
RMapCache<String, Integer> map = redisson.getMapCache("simple");
map.put("123", 3, 1, TimeUnit.SECONDS);
Thread.sleep(800);
map.put("123", 3, 1, TimeUnit.SECONDS);
Thread.sleep(800);
Assert.assertEquals(3, (int)map.get("123"));
Thread.sleep(200);
Assert.assertFalse(map.containsKey("123"));
map.destroy();
}
示例11
public BaseRegion(RMapCache<Object, Object> mapCache, ConnectionManager connectionManager, RegionFactory regionFactory, CacheDataDescription metadata, Properties properties, String defaultKey) {
super();
this.mapCache = mapCache;
this.regionFactory = regionFactory;
this.metadata = metadata;
this.connectionManager = connectionManager;
String maxEntries = getProperty(properties, mapCache.getName(), defaultKey, RedissonRegionFactory.MAX_ENTRIES_SUFFIX);
if (maxEntries != null) {
mapCache.setMaxSize(Integer.valueOf(maxEntries));
}
String timeToLive = getProperty(properties, mapCache.getName(), defaultKey, RedissonRegionFactory.TTL_SUFFIX);
if (timeToLive != null) {
ttl = Integer.valueOf(timeToLive);
}
String maxIdleTime = getProperty(properties, mapCache.getName(), defaultKey, RedissonRegionFactory.MAX_IDLE_SUFFIX);
if (maxIdleTime != null) {
maxIdle = Integer.valueOf(maxIdleTime);
}
String fallbackValue = (String) properties.getOrDefault(RedissonRegionFactory.FALLBACK, "false");
fallback = Boolean.valueOf(fallbackValue);
}
示例12
@Test
public void testFastPutIfAbsentTTL() throws Exception {
RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("simple");
SimpleKey key = new SimpleKey("1");
SimpleValue value = new SimpleValue("2");
map.put(key, value);
assertThat(map.fastPutIfAbsent(key, new SimpleValue("3"))).isFalse();
assertThat(map.get(key)).isEqualTo(value);
SimpleKey key1 = new SimpleKey("2");
SimpleValue value1 = new SimpleValue("4");
assertThat(map.fastPutIfAbsent(key1, value1)).isTrue();
assertThat(map.get(key1)).isEqualTo(value1);
SimpleKey key2 = new SimpleKey("3");
map.put(key2, new SimpleValue("31"), 500, TimeUnit.MILLISECONDS);
assertThat(map.fastPutIfAbsent(key2, new SimpleValue("32"))).isFalse();
Thread.sleep(500);
assertThat(map.fastPutIfAbsent(key2, new SimpleValue("32"))).isTrue();
assertThat(map.get(key2)).isEqualTo(new SimpleValue("32"));
map.destroy();
}
示例13
@Override
public List<JobExecutor> queryJobExecutors() {
String uuid = UUID.randomUUID().toString();
int subscribers = (int) workerTopic.publish(uuid);
if (subscribers == 0) {
return Collections.emptyList();
}
RMapCache<String, JobExecutor> cache = redissonClient
.getMapCache(String.format(Container.WORKER_REQ_VAL, uuid));
waitFor(uuid, subscribers);
return new ArrayList<>(cache.readAllValues());
}
示例14
@Override
public TimestampsRegion buildTimestampsRegion(String regionName, Properties properties) throws CacheException {
log.debug("Building timestamps cache region: " + regionName);
RMapCache<Object, Object> mapCache = getCache(regionName, properties, TIMESTAMPS_DEF);
return new RedissonTimestampsRegion(mapCache, ((Redisson)redisson).getConnectionManager(),this, properties, TIMESTAMPS_DEF);
}
示例15
@Test
public void testReadAllEntrySet() throws InterruptedException {
RMapCache<Integer, String> map = redisson.getMapCache("simple12");
map.put(1, "12");
map.put(2, "33", 10, TimeUnit.MINUTES, 60, TimeUnit.MINUTES);
map.put(3, "43");
assertThat(map.readAllEntrySet()).isEqualTo(map.entrySet());
map.destroy();
}
示例16
@Test
public void testRMapCacheValues() {
final RMapCache<String, String> map = redisson.getMapCache("testRMapCacheValues");
map.put("1234", "5678", 1, TimeUnit.MINUTES, 60, TimeUnit.MINUTES);
assertThat(map.values()).containsOnly("5678");
map.destroy();
}
示例17
@Test
public void testExpiredListener() {
RMapCache<Integer, Integer> map = redisson.getMapCache("simple");
checkExpiredListener(map, 10, 2, () -> map.put(10, 2, 2, TimeUnit.SECONDS));
checkExpiredListener(map, 13, 2, () -> map.fastPut(13, 2, 2, TimeUnit.SECONDS));
checkExpiredListener(map, 14, 2, () -> map.putIfAbsent(14, 2, 2, TimeUnit.SECONDS));
checkExpiredListener(map, 15, 2, () -> map.fastPutIfAbsent(15, 2, 2, TimeUnit.SECONDS));
map.destroy();
}
示例18
public EntryEvent(RMapCache<K, V> source, Type type, K key, V value, V oldValue) {
super();
this.source = source;
this.type = type;
this.key = key;
this.value = value;
this.oldValue = oldValue;
}
示例19
@Test
public void testValueSize() {
Assume.assumeTrue(RedisRunner.getDefaultRedisServerInstance().getRedisVersion().compareTo("3.2.0") > 0);
RMap<String, String> map = getMap("getAll");
Assume.assumeTrue(!(map instanceof RMapCache));
map.put("1", "1234");
assertThat(map.valueSize("4")).isZero();
assertThat(map.valueSize("1")).isEqualTo(7);
destroy(map);
}
示例20
@Test
public void testSizeInMemory() {
Assume.assumeTrue(RedisRunner.getDefaultRedisServerInstance().getRedisVersion().compareTo("4.0.0") > 0);
RMapCache<Integer, Integer> map = redisson.getMapCache("test");
for (int i = 0; i < 10; i++) {
map.put(i, i, 5, TimeUnit.SECONDS);
}
assertThat(map.sizeInMemory()).isGreaterThanOrEqualTo(466);
}
示例21
@Test
public void testFastPutTTL() throws InterruptedException {
RMapCache<SimpleKey, SimpleValue> map = redisson.getMapCache("getAll");
map.trySetMaxSize(1);
map.fastPut(new SimpleKey("1"), new SimpleValue("3"), 5, TimeUnit.SECONDS, 0, TimeUnit.SECONDS);
Thread.sleep(5000);
assertThat(map.get(new SimpleKey("1"))).isNull();
map.fastPut(new SimpleKey("1"), new SimpleValue("4"), 5, TimeUnit.SECONDS, 0, TimeUnit.SECONDS);
Thread.sleep(10000);
assertThat(map.get(new SimpleKey("1"))).isNull();
}
示例22
@Test
public void testWriterPutIfAbsentTTL() {
Map<String, String> store = new HashMap<>();
RMapCache<String, String> map = (RMapCache<String, String>) getWriterTestMap("test", store);
map.putIfAbsent("1", "11", 10, TimeUnit.SECONDS);
map.putIfAbsent("1", "00", 10, TimeUnit.SECONDS);
map.putIfAbsent("2", "22", 10, TimeUnit.SECONDS);
Map<String, String> expected = new HashMap<>();
expected.put("1", "11");
expected.put("2", "22");
assertThat(store).isEqualTo(expected);
map.destroy();
}
示例23
@Override
public TimestampsRegion buildTimestampsRegion(String regionName, Properties properties) throws CacheException {
log.debug("Building timestamps cache region: " + regionName);
RMapCache<Object, Object> mapCache = getCache(regionName, properties, TIMESTAMPS_DEF);
return new RedissonTimestampsRegion(mapCache, ((Redisson)redisson).getConnectionManager(),this, properties, TIMESTAMPS_DEF);
}
示例24
@Override
public NaturalIdRegion buildNaturalIdRegion(String regionName, Properties properties, CacheDataDescription metadata)
throws CacheException {
log.debug("Building naturalId cache region: " + regionName);
RMapCache<Object, Object> mapCache = getCache(regionName, properties, NATURAL_ID_DEF);
return new RedissonNaturalIdRegion(mapCache, ((Redisson)redisson).getConnectionManager(),this, metadata, settings, properties, NATURAL_ID_DEF, cacheKeysFactory);
}
示例25
@Test
public void testCacheValues() {
final RMapCache<String, String> map = redisson.getMapCache("testRMapCacheValues");
map.put("1234", "5678", 0, TimeUnit.MINUTES, 60, TimeUnit.MINUTES);
assertThat(map.values()).containsOnly("5678");
map.destroy();
}
示例26
private RMap<String, String> getMap() {
RMap<String, String> map = null;
if (RMapCache.class.isAssignableFrom(mapClass)) {
map = redisson.getMapCache("map");
} else {
map = redisson.getMap("map");
}
return map;
}
示例27
@Test
public void testExpiredIterator() throws InterruptedException {
RMapCache<String, String> cache = redisson.getMapCache("simple");
cache.put("0", "8");
cache.put("1", "6", 1, TimeUnit.SECONDS);
cache.put("2", "4", 3, TimeUnit.SECONDS);
cache.put("3", "2", 4, TimeUnit.SECONDS);
cache.put("4", "4", 1, TimeUnit.SECONDS);
Thread.sleep(1000);
assertThat(cache.keySet()).containsOnly("0", "2", "3");
cache.destroy();
}
示例28
@Test
public void testExpire() throws InterruptedException {
RMapCache<String, String> cache = redisson.getMapCache("simple");
cache.put("0", "8", 1, TimeUnit.SECONDS);
cache.expire(100, TimeUnit.MILLISECONDS);
Thread.sleep(500);
Assert.assertEquals(0, cache.size());
cache.destroy();
}
示例29
@Test
public void testClearExpire() throws InterruptedException {
RMapCache<String, String> cache = redisson.getMapCache("simple");
cache.put("0", "8", 1, TimeUnit.SECONDS);
cache.expireAt(System.currentTimeMillis() + 100);
cache.clearExpire();
Thread.sleep(500);
Assert.assertEquals(1, cache.size());
cache.destroy();
}
示例30
@Test
public void testEntrySet() throws InterruptedException {
RMapCache<Integer, String> map = redisson.getMapCache("simple12");
map.put(1, "12");
map.put(2, "33", 1, TimeUnit.SECONDS);
map.put(3, "43");
Map<Integer, String> expected = new HashMap<>();
map.put(1, "12");
map.put(3, "43");
assertThat(map.entrySet()).containsAll(expected.entrySet());
assertThat(map).hasSize(3);
map.destroy();
}