() {
diff --git a/redisson/src/main/java/org/redisson/api/LocalCachedMapOptions.java b/redisson/src/main/java/org/redisson/api/LocalCachedMapOptions.java
index 89a600aa5..523cc5ff5 100644
--- a/redisson/src/main/java/org/redisson/api/LocalCachedMapOptions.java
+++ b/redisson/src/main/java/org/redisson/api/LocalCachedMapOptions.java
@@ -25,7 +25,7 @@ import java.util.concurrent.TimeUnit;
*/
public class LocalCachedMapOptions {
- public enum EvictionPolicy {NONE, LRU, LFU};
+ public enum EvictionPolicy {NONE, LRU, LFU, SOFT};
private boolean invalidateEntryOnChange;
private EvictionPolicy evictionPolicy;
@@ -115,6 +115,7 @@ public class LocalCachedMapOptions {
* @param evictionPolicy
* LRU
- uses cache with LRU (least recently used) eviction policy.
*
LFU
- uses cache with LFU (least frequently used) eviction policy.
+ *
SOFT
- uses cache with soft references. The garbage collector will evict items from the cache when the JVM is running out of memory.
*
NONE
- doesn't use eviction policy, but timeToLive and maxIdleTime params are still working.
* @return LocalCachedMapOptions instance
*/
diff --git a/redisson/src/main/java/org/redisson/api/RedissonClient.java b/redisson/src/main/java/org/redisson/api/RedissonClient.java
index 2845c5035..b38746061 100755
--- a/redisson/src/main/java/org/redisson/api/RedissonClient.java
+++ b/redisson/src/main/java/org/redisson/api/RedissonClient.java
@@ -742,12 +742,28 @@ public interface RedissonClient {
* Returns ScheduledExecutorService by name
* using provided codec for task, response and request serialization
*
+ * Please use getExecutorService(String name, Codec codec) method instead.
+ *
+ * @deprecated - use {@link #getExecutorService(String, Codec)} instead.
+ *
* @param name - name of object
* @param codec - codec for task, response and request
* @return ScheduledExecutorService object
*/
+ @Deprecated
RScheduledExecutorService getExecutorService(Codec codec, String name);
+ /**
+ * Returns ScheduledExecutorService by name
+ * using provided codec for task, response and request serialization
+ *
+ * @param name - name of object
+ * @param codec - codec for task, response and request
+ * @return ScheduledExecutorService object
+ * @since 2.8.2
+ */
+ RScheduledExecutorService getExecutorService(String name, Codec codec);
+
/**
* Returns object for remote operations prefixed with the default name (redisson_remote_service)
*
diff --git a/redisson/src/main/java/org/redisson/misc/AbstractCacheMap.java b/redisson/src/main/java/org/redisson/cache/AbstractCacheMap.java
similarity index 81%
rename from redisson/src/main/java/org/redisson/misc/AbstractCacheMap.java
rename to redisson/src/main/java/org/redisson/cache/AbstractCacheMap.java
index 75fecc81c..1741c6d25 100644
--- a/redisson/src/main/java/org/redisson/misc/AbstractCacheMap.java
+++ b/redisson/src/main/java/org/redisson/cache/AbstractCacheMap.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.redisson.misc;
+package org.redisson.cache;
import java.util.AbstractCollection;
import java.util.AbstractMap.SimpleEntry;
@@ -37,56 +37,8 @@ import io.netty.util.internal.PlatformDependent;
*/
public abstract class AbstractCacheMap implements Cache {
- public static class CachedValue {
-
- private final Object key;
- private final Object value;
-
- long ttl;
- long maxIdleTime;
-
- long creationTime;
- long lastAccess;
-
- public CachedValue(Object key, Object value, long ttl, long maxIdleTime) {
- this.value = value;
- this.ttl = ttl;
- this.key = key;
- this.maxIdleTime = maxIdleTime;
- creationTime = System.currentTimeMillis();
- lastAccess = creationTime;
- }
-
- 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;
- }
-
- public Object getKey() {
- return key;
- }
-
- public Object getValue() {
- lastAccess = System.currentTimeMillis();
- return value;
- }
-
- @Override
- public String toString() {
- return "CachedValue [key=" + key + ", value=" + value + "]";
- }
-
- }
-
final int size;
- final ConcurrentMap map = PlatformDependent.newConcurrentHashMap();
+ final ConcurrentMap> map = PlatformDependent.newConcurrentHashMap();
private final long timeToLiveInMillis;
private final long maxIdleInMillis;
@@ -100,11 +52,11 @@ public abstract class AbstractCacheMap implements Cache {
this.timeToLiveInMillis = timeToLiveInMillis;
}
- protected void onValueRead(CachedValue value) {
+ protected void onValueRead(CachedValue value) {
}
- protected void onValueRemove(CachedValue value) {
+ protected void onValueRemove(CachedValue value) {
}
@@ -137,7 +89,7 @@ public abstract class AbstractCacheMap implements Cache {
throw new NullPointerException();
}
- CachedValue entry = map.get(key);
+ CachedValue entry = map.get(key);
if (entry == null) {
return false;
}
@@ -161,8 +113,8 @@ public abstract class AbstractCacheMap implements Cache {
throw new NullPointerException();
}
- for (Map.Entry entry : map.entrySet()) {
- CachedValue cachedValue = entry.getValue();
+ for (Map.Entry> entry : map.entrySet()) {
+ CachedValue cachedValue = entry.getValue();
if (cachedValue.getValue().equals(value)) {
if (cachedValue.isExpired()) {
if (map.remove(cachedValue.getKey(), cachedValue)) {
@@ -187,7 +139,7 @@ public abstract class AbstractCacheMap implements Cache {
throw new NullPointerException();
}
- CachedValue entry = map.get(key);
+ CachedValue entry = map.get(key);
if (entry == null) {
return null;
}
@@ -201,8 +153,7 @@ public abstract class AbstractCacheMap implements Cache {
return readValue(entry);
}
- @SuppressWarnings("unchecked")
- protected V readValue(CachedValue entry) {
+ protected V readValue(CachedValue entry) {
onValueRead(entry);
return (V) entry.getValue();
}
@@ -216,17 +167,16 @@ public abstract class AbstractCacheMap implements Cache {
return put(key, value, timeToLiveInMillis, TimeUnit.MILLISECONDS, maxIdleInMillis, TimeUnit.MILLISECONDS);
}
- @SuppressWarnings("unchecked")
@Override
public V put(K key, V value, long ttl, TimeUnit ttlUnit, long maxIdleTime, TimeUnit maxIdleUnit) {
- CachedValue entry = create(key, value, ttlUnit.toMillis(ttl), maxIdleUnit.toMillis(maxIdleTime));
+ CachedValue entry = create(key, value, ttlUnit.toMillis(ttl), maxIdleUnit.toMillis(maxIdleTime));
if (isFull(key)) {
if (!removeExpiredEntries()) {
onMapFull();
}
}
onValueCreate(entry);
- CachedValue prevCachedValue = map.put(key, entry);
+ CachedValue prevCachedValue = map.put(key, entry);
if (prevCachedValue != null) {
onValueRemove(prevCachedValue);
if (!prevCachedValue.isExpired()) {
@@ -236,17 +186,17 @@ public abstract class AbstractCacheMap implements Cache {
return null;
}
- protected CachedValue create(K key, V value, long ttl, long maxIdleTime) {
- return new CachedValue(key, value, ttl, maxIdleTime);
+ protected CachedValue create(K key, V value, long ttl, long maxIdleTime) {
+ return new StdCachedValue(key, value, ttl, maxIdleTime);
}
- protected void onValueCreate(CachedValue entry) {
+ protected void onValueCreate(CachedValue entry) {
}
- private boolean removeExpiredEntries() {
+ protected boolean removeExpiredEntries() {
boolean removed = false;
// TODO optimize
- for (CachedValue value : map.values()) {
+ for (CachedValue value : map.values()) {
if (value.isExpired()) {
if (map.remove(value.getKey(), value)) {
onValueRemove(value);
@@ -280,10 +230,9 @@ public abstract class AbstractCacheMap implements Cache {
* (non-Javadoc)
* @see java.util.Map#remove(java.lang.Object)
*/
- @SuppressWarnings("unchecked")
@Override
public V remove(Object key) {
- CachedValue entry = map.remove(key);
+ CachedValue entry = map.remove(key);
if (entry != null) {
onValueRemove(entry);
if (!entry.isExpired()) {
@@ -346,9 +295,9 @@ public abstract class AbstractCacheMap implements Cache {
abstract class MapIterator implements Iterator {
- final Iterator> keyIterator = map.entrySet().iterator();
+ final Iterator>> keyIterator = map.entrySet().iterator();
- Map.Entry mapEntry;
+ Map.Entry> mapEntry;
@Override
public boolean hasNext() {
@@ -357,7 +306,7 @@ public abstract class AbstractCacheMap implements Cache {
}
mapEntry = null;
while (keyIterator.hasNext()) {
- Map.Entry entry = keyIterator.next();
+ Map.Entry> entry = keyIterator.next();
if (entry.getValue().isExpired()) {
continue;
}
diff --git a/redisson/src/main/java/org/redisson/misc/Cache.java b/redisson/src/main/java/org/redisson/cache/Cache.java
similarity index 96%
rename from redisson/src/main/java/org/redisson/misc/Cache.java
rename to redisson/src/main/java/org/redisson/cache/Cache.java
index 2856da98b..87dcdd863 100644
--- a/redisson/src/main/java/org/redisson/misc/Cache.java
+++ b/redisson/src/main/java/org/redisson/cache/Cache.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.redisson.misc;
+package org.redisson.cache;
import java.util.Map;
import java.util.concurrent.TimeUnit;
diff --git a/redisson/src/main/java/org/redisson/cache/CachedValue.java b/redisson/src/main/java/org/redisson/cache/CachedValue.java
new file mode 100644
index 000000000..069dbc331
--- /dev/null
+++ b/redisson/src/main/java/org/redisson/cache/CachedValue.java
@@ -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 {
+ boolean isExpired();
+
+ K getKey();
+
+ V getValue();
+}
diff --git a/redisson/src/main/java/org/redisson/cache/CachedValueReference.java b/redisson/src/main/java/org/redisson/cache/CachedValueReference.java
new file mode 100644
index 000000000..c6e0c4bfb
--- /dev/null
+++ b/redisson/src/main/java/org/redisson/cache/CachedValueReference.java
@@ -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 extends SoftReference {
+
+ 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;
+ }
+
+}
diff --git a/redisson/src/main/java/org/redisson/misc/LFUCacheMap.java b/redisson/src/main/java/org/redisson/cache/LFUCacheMap.java
similarity index 97%
rename from redisson/src/main/java/org/redisson/misc/LFUCacheMap.java
rename to redisson/src/main/java/org/redisson/cache/LFUCacheMap.java
index 720b46088..c06b21e94 100644
--- a/redisson/src/main/java/org/redisson/misc/LFUCacheMap.java
+++ b/redisson/src/main/java/org/redisson/cache/LFUCacheMap.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.redisson.misc;
+package org.redisson.cache;
import java.util.Map;
import java.util.concurrent.ConcurrentNavigableMap;
@@ -57,7 +57,7 @@ public class LFUCacheMap extends AbstractCacheMap {
}
- public static class LFUCachedValue extends CachedValue {
+ public static class LFUCachedValue extends StdCachedValue {
Long id;
long accessCount;
diff --git a/redisson/src/main/java/org/redisson/misc/LRUCacheMap.java b/redisson/src/main/java/org/redisson/cache/LRUCacheMap.java
similarity index 98%
rename from redisson/src/main/java/org/redisson/misc/LRUCacheMap.java
rename to redisson/src/main/java/org/redisson/cache/LRUCacheMap.java
index 8ba4368c1..d90ebf471 100644
--- a/redisson/src/main/java/org/redisson/misc/LRUCacheMap.java
+++ b/redisson/src/main/java/org/redisson/cache/LRUCacheMap.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.redisson.misc;
+package org.redisson.cache;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
diff --git a/redisson/src/main/java/org/redisson/misc/NoneCacheMap.java b/redisson/src/main/java/org/redisson/cache/NoneCacheMap.java
similarity index 97%
rename from redisson/src/main/java/org/redisson/misc/NoneCacheMap.java
rename to redisson/src/main/java/org/redisson/cache/NoneCacheMap.java
index 9b71681f0..db6354222 100644
--- a/redisson/src/main/java/org/redisson/misc/NoneCacheMap.java
+++ b/redisson/src/main/java/org/redisson/cache/NoneCacheMap.java
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.redisson.misc;
+package org.redisson.cache;
/**
*
diff --git a/redisson/src/main/java/org/redisson/cache/SoftCacheMap.java b/redisson/src/main/java/org/redisson/cache/SoftCacheMap.java
new file mode 100644
index 000000000..d861fb37c
--- /dev/null
+++ b/redisson/src/main/java/org/redisson/cache/SoftCacheMap.java
@@ -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 key
+ * @param value
+ */
+public class SoftCacheMap extends AbstractCacheMap {
+
+ private final ReferenceQueue queue = new ReferenceQueue();
+
+ public SoftCacheMap(long timeToLiveInMillis, long maxIdleInMillis) {
+ super(0, timeToLiveInMillis, maxIdleInMillis);
+ }
+
+ protected CachedValue create(K key, V value, long ttl, long maxIdleTime) {
+ return new SoftCachedValue(key, value, ttl, maxIdleTime, queue);
+ }
+
+ @Override
+ protected boolean removeExpiredEntries() {
+ while (true) {
+ CachedValueReference value = (CachedValueReference) queue.poll();
+ if (value == null) {
+ break;
+ }
+ map.remove(value.getOwner().getKey(), value.getOwner());
+ }
+ return super.removeExpiredEntries();
+ }
+
+ @Override
+ protected void onMapFull() {
+ }
+
+}
diff --git a/redisson/src/main/java/org/redisson/cache/SoftCachedValue.java b/redisson/src/main/java/org/redisson/cache/SoftCachedValue.java
new file mode 100644
index 000000000..efdedc100
--- /dev/null
+++ b/redisson/src/main/java/org/redisson/cache/SoftCachedValue.java
@@ -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 extends StdCachedValue implements CachedValue {
+
+ private final CachedValueReference ref;
+
+ public SoftCachedValue(K key, V value, long ttl, long maxIdleTime, ReferenceQueue queue) {
+ super(key, null, ttl, maxIdleTime);
+ this.ref = new CachedValueReference(this, value, queue);
+ }
+
+ @Override
+ public V getValue() {
+ super.getValue();
+ return ref.get();
+ }
+
+}
diff --git a/redisson/src/main/java/org/redisson/cache/StdCachedValue.java b/redisson/src/main/java/org/redisson/cache/StdCachedValue.java
new file mode 100644
index 000000000..8d41abdf6
--- /dev/null
+++ b/redisson/src/main/java/org/redisson/cache/StdCachedValue.java
@@ -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 implements CachedValue {
+
+ 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 + "]";
+ }
+
+}
diff --git a/redisson/src/main/java/org/redisson/client/RedisConnection.java b/redisson/src/main/java/org/redisson/client/RedisConnection.java
index 23c64625c..003324b2f 100644
--- a/redisson/src/main/java/org/redisson/client/RedisConnection.java
+++ b/redisson/src/main/java/org/redisson/client/RedisConnection.java
@@ -38,6 +38,11 @@ import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import io.netty.util.concurrent.ScheduledFuture;
+/**
+ *
+ * @author Nikita Koksharov
+ *
+ */
public class RedisConnection implements RedisCommands {
private static final AttributeKey CONNECTION = AttributeKey.valueOf("connection");
diff --git a/redisson/src/main/java/org/redisson/client/RedisPubSubConnection.java b/redisson/src/main/java/org/redisson/client/RedisPubSubConnection.java
index 7103d1af0..ea8566ed2 100644
--- a/redisson/src/main/java/org/redisson/client/RedisPubSubConnection.java
+++ b/redisson/src/main/java/org/redisson/client/RedisPubSubConnection.java
@@ -40,6 +40,11 @@ import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import io.netty.util.internal.PlatformDependent;
+/**
+ *
+ * @author Nikita Koksharov
+ *
+ */
public class RedisPubSubConnection extends RedisConnection {
final Queue> listeners = new ConcurrentLinkedQueue>();
diff --git a/redisson/src/main/java/org/redisson/client/handler/CommandDecoder.java b/redisson/src/main/java/org/redisson/client/handler/CommandDecoder.java
index b5024789f..b2411e589 100644
--- a/redisson/src/main/java/org/redisson/client/handler/CommandDecoder.java
+++ b/redisson/src/main/java/org/redisson/client/handler/CommandDecoder.java
@@ -184,7 +184,7 @@ public class CommandDecoder extends ReplayingDecoder {
CommandsData commandBatch) {
int i = state().getBatchIndex();
- RedisException error = null;
+ Throwable error = null;
while (in.writerIndex() > in.readerIndex()) {
CommandData