Merge branch 'master' into 3.0.0
# Conflicts: # pom.xml # redisson-all/pom.xml # redisson-tomcat/pom.xml # redisson-tomcat/redisson-tomcat-6/pom.xml # redisson-tomcat/redisson-tomcat-7/pom.xml # redisson-tomcat/redisson-tomcat-8/pom.xml # redisson/pom.xmlpull/802/merge
commit
b45343b0d7
@ -0,0 +1,27 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2016 Nikita Koksharov
|
||||||
|
*
|
||||||
|
* 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 org.redisson.cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by jribble on 2/20/17.
|
||||||
|
*/
|
||||||
|
public interface CachedValue<K, V> {
|
||||||
|
boolean isExpired();
|
||||||
|
|
||||||
|
K getKey();
|
||||||
|
|
||||||
|
V getValue();
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2016 Nikita Koksharov
|
||||||
|
*
|
||||||
|
* 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 org.redisson.cache;
|
||||||
|
|
||||||
|
import java.lang.ref.ReferenceQueue;
|
||||||
|
import java.lang.ref.SoftReference;
|
||||||
|
|
||||||
|
public class CachedValueReference<V> extends SoftReference<V> {
|
||||||
|
|
||||||
|
private final CachedValue<?, ?> owner;
|
||||||
|
|
||||||
|
public CachedValueReference(CachedValue<?, ?> owner, V referent, ReferenceQueue<? super V> q) {
|
||||||
|
super(referent, q);
|
||||||
|
this.owner = owner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CachedValue<?, ?> getOwner() {
|
||||||
|
return owner;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2016 Nikita Koksharov
|
||||||
|
*
|
||||||
|
* 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 org.redisson.cache;
|
||||||
|
|
||||||
|
import java.lang.ref.ReferenceQueue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Nikita Koksharov
|
||||||
|
*
|
||||||
|
* @param <K> key
|
||||||
|
* @param <V> value
|
||||||
|
*/
|
||||||
|
public class SoftCacheMap<K, V> extends AbstractCacheMap<K, V> {
|
||||||
|
|
||||||
|
private final ReferenceQueue<V> queue = new ReferenceQueue<V>();
|
||||||
|
|
||||||
|
public SoftCacheMap(long timeToLiveInMillis, long maxIdleInMillis) {
|
||||||
|
super(0, timeToLiveInMillis, maxIdleInMillis);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected CachedValue<K, V> create(K key, V value, long ttl, long maxIdleTime) {
|
||||||
|
return new SoftCachedValue<K, V>(key, value, ttl, maxIdleTime, queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean removeExpiredEntries() {
|
||||||
|
while (true) {
|
||||||
|
CachedValueReference<V> value = (CachedValueReference<V>) queue.poll();
|
||||||
|
if (value == null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
map.remove(value.getOwner().getKey(), value.getOwner());
|
||||||
|
}
|
||||||
|
return super.removeExpiredEntries();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onMapFull() {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2016 Nikita Koksharov
|
||||||
|
*
|
||||||
|
* 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 org.redisson.cache;
|
||||||
|
|
||||||
|
import java.lang.ref.ReferenceQueue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by jribble on 2/20/17.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class SoftCachedValue<K, V> extends StdCachedValue<K, V> implements CachedValue<K, V> {
|
||||||
|
|
||||||
|
private final CachedValueReference<V> ref;
|
||||||
|
|
||||||
|
public SoftCachedValue(K key, V value, long ttl, long maxIdleTime, ReferenceQueue<V> queue) {
|
||||||
|
super(key, null, ttl, maxIdleTime);
|
||||||
|
this.ref = new CachedValueReference<V>(this, value, queue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V getValue() {
|
||||||
|
super.getValue();
|
||||||
|
return ref.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,71 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2016 Nikita Koksharov
|
||||||
|
*
|
||||||
|
* 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 org.redisson.cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by jribble on 2/20/17.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class StdCachedValue<K, V> implements CachedValue<K, V> {
|
||||||
|
|
||||||
|
protected final K key;
|
||||||
|
protected final V value;
|
||||||
|
|
||||||
|
long ttl;
|
||||||
|
long maxIdleTime;
|
||||||
|
|
||||||
|
long creationTime;
|
||||||
|
long lastAccess;
|
||||||
|
|
||||||
|
public StdCachedValue(K key, V value, long ttl, long maxIdleTime) {
|
||||||
|
this.value = value;
|
||||||
|
this.ttl = ttl;
|
||||||
|
this.key = key;
|
||||||
|
this.maxIdleTime = maxIdleTime;
|
||||||
|
creationTime = System.currentTimeMillis();
|
||||||
|
lastAccess = creationTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isExpired() {
|
||||||
|
boolean result = false;
|
||||||
|
long currentTime = System.currentTimeMillis();
|
||||||
|
if (ttl != 0 && creationTime + ttl < currentTime) {
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
if (maxIdleTime != 0 && lastAccess + maxIdleTime < currentTime) {
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public K getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public V getValue() {
|
||||||
|
lastAccess = System.currentTimeMillis();
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "CachedValue [key=" + key + ", value=" + value + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2016 Nikita Koksharov
|
||||||
|
*
|
||||||
|
* 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 org.redisson.config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Nikita Koksharov
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public enum SubscriptionMode {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subscribe to slave nodes
|
||||||
|
*/
|
||||||
|
SLAVE,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subscribe to master node
|
||||||
|
*/
|
||||||
|
MASTER
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2016 Nikita Koksharov
|
||||||
|
*
|
||||||
|
* 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 org.redisson.connection;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
|
import org.redisson.api.RFuture;
|
||||||
|
import org.redisson.misc.RPromise;
|
||||||
|
import org.redisson.misc.RedissonPromise;
|
||||||
|
|
||||||
|
import io.netty.util.concurrent.Future;
|
||||||
|
import io.netty.util.concurrent.FutureListener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Nikita Koksharov
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class CountListener implements FutureListener<Void> {
|
||||||
|
|
||||||
|
private final RPromise<Void> res;
|
||||||
|
|
||||||
|
private final AtomicInteger counter;
|
||||||
|
|
||||||
|
public static RPromise<Void> create(RFuture<Void>... futures) {
|
||||||
|
RPromise<Void> result = new RedissonPromise<Void>();
|
||||||
|
FutureListener<Void> listener = new CountListener(result, futures.length);
|
||||||
|
for (RFuture<Void> future : futures) {
|
||||||
|
future.addListener(listener);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CountListener(RPromise<Void> res, int amount) {
|
||||||
|
super();
|
||||||
|
this.res = res;
|
||||||
|
this.counter = new AtomicInteger(amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void operationComplete(Future<Void> future) throws Exception {
|
||||||
|
if (!future.isSuccess()) {
|
||||||
|
res.tryFailure(future.cause());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (counter.decrementAndGet() == 0) {
|
||||||
|
res.trySuccess(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
package org.redisson.misc;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.redisson.cache.Cache;
|
||||||
|
import org.redisson.cache.SoftCacheMap;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
public class SoftCacheMapTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMaxIdleTimeEviction() throws InterruptedException {
|
||||||
|
Cache<Integer, Integer> map = new SoftCacheMap<Integer, Integer>(0, 0);
|
||||||
|
map.put(1, 0, 0, TimeUnit.MILLISECONDS, 400, TimeUnit.MILLISECONDS);
|
||||||
|
assertThat(map.get(1)).isEqualTo(0);
|
||||||
|
Thread.sleep(200);
|
||||||
|
assertThat(map.get(1)).isEqualTo(0);
|
||||||
|
Thread.sleep(200);
|
||||||
|
assertThat(map.get(1)).isEqualTo(0);
|
||||||
|
Thread.sleep(200);
|
||||||
|
assertThat(map.get(1)).isEqualTo(0);
|
||||||
|
Thread.sleep(410);
|
||||||
|
assertThat(map.keySet()).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTTLEviction() throws InterruptedException {
|
||||||
|
Cache<Integer, Integer> map = new SoftCacheMap<Integer, Integer>(0, 0);
|
||||||
|
map.put(1, 0, 500, TimeUnit.MILLISECONDS, 0, TimeUnit.MILLISECONDS);
|
||||||
|
assertThat(map.get(1)).isEqualTo(0);
|
||||||
|
Thread.sleep(100);
|
||||||
|
assertThat(map.get(1)).isEqualTo(0);
|
||||||
|
assertThat(map.keySet()).containsOnly(1);
|
||||||
|
Thread.sleep(500);
|
||||||
|
assertThat(map.keySet()).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSizeEviction() {
|
||||||
|
Cache<Integer, Integer> map = new SoftCacheMap<Integer, Integer>(0, 0);
|
||||||
|
map.put(1, 0);
|
||||||
|
map.put(2, 0);
|
||||||
|
|
||||||
|
assertThat(map.keySet()).containsOnly(1, 2);
|
||||||
|
map.put(3, 0);
|
||||||
|
map.put(4, 0);
|
||||||
|
|
||||||
|
assertThat(map.keySet()).containsOnly(1, 2, 3, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
// This test requires using -XX:SoftRefLRUPolicyMSPerMB=0 to pass
|
||||||
|
@Test
|
||||||
|
public void testSoftReferences() {
|
||||||
|
Cache<Integer, Integer> map = new SoftCacheMap<Integer, Integer>(0, 0);
|
||||||
|
for(int i=0;i<100000;i++) {
|
||||||
|
map.put(i, new Integer(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
assertThat(map.values().stream().filter(Objects::nonNull).count()).isEqualTo(100000);
|
||||||
|
System.gc();
|
||||||
|
assertThat(map.values().stream().filter(Objects::nonNull).count()).isZero();
|
||||||
|
assertThat(map.values().size()).isZero();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue