Skip to content

Introduce session callback, reduce connection allocation overhead #2129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.6.0-SNAPSHOT</version>
<version>2.6.0-GH-2110-SNAPSHOT</version>

<name>Spring Data Redis</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ RedisSerializer stringSerializer() {
}

@Nullable
<T> T execute(RedisCallback<T> callback, boolean exposeConnection) {
return template.execute(callback, exposeConnection);
<T> T execute(RedisCallback<T> callback) {
return template.execute(callback, true);
}

public RedisOperations<K, V> getOperations() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Set<K> keys(final RedisClusterNode node, final K pattern) {

Assert.notNull(node, "ClusterNode must not be null.");

return execute(connection -> deserializeKeys(connection.keys(node, rawKey(pattern))));
return doInCluster(connection -> deserializeKeys(connection.keys(node, rawKey(pattern))));
}

/*
Expand All @@ -71,7 +71,7 @@ public K randomKey(final RedisClusterNode node) {

Assert.notNull(node, "ClusterNode must not be null.");

return execute(connection -> deserializeKey(connection.randomKey(node)));
return doInCluster(connection -> deserializeKey(connection.randomKey(node)));
}

/*
Expand All @@ -83,7 +83,7 @@ public String ping(final RedisClusterNode node) {

Assert.notNull(node, "ClusterNode must not be null.");

return execute(connection -> connection.ping(node));
return doInCluster(connection -> connection.ping(node));
}

/*
Expand All @@ -95,7 +95,7 @@ public void addSlots(final RedisClusterNode node, final int... slots) {

Assert.notNull(node, "ClusterNode must not be null.");

execute((RedisClusterCallback<Void>) connection -> {
doInCluster((RedisClusterCallback<Void>) connection -> {
connection.clusterAddSlots(node, slots);
return null;
});
Expand Down Expand Up @@ -123,7 +123,7 @@ public void bgReWriteAof(final RedisClusterNode node) {

Assert.notNull(node, "ClusterNode must not be null.");

execute((RedisClusterCallback<Void>) connection -> {
doInCluster((RedisClusterCallback<Void>) connection -> {
connection.bgReWriteAof(node);
return null;
});
Expand All @@ -138,7 +138,7 @@ public void bgSave(final RedisClusterNode node) {

Assert.notNull(node, "ClusterNode must not be null.");

execute((RedisClusterCallback<Void>) connection -> {
doInCluster((RedisClusterCallback<Void>) connection -> {
connection.bgSave(node);
return null;
});
Expand All @@ -153,7 +153,7 @@ public void meet(final RedisClusterNode node) {

Assert.notNull(node, "ClusterNode must not be null.");

execute((RedisClusterCallback<Void>) connection -> {
doInCluster((RedisClusterCallback<Void>) connection -> {
connection.clusterMeet(node);
return null;
});
Expand All @@ -168,7 +168,7 @@ public void forget(final RedisClusterNode node) {

Assert.notNull(node, "ClusterNode must not be null.");

execute((RedisClusterCallback<Void>) connection -> {
doInCluster((RedisClusterCallback<Void>) connection -> {
connection.clusterForget(node);
return null;
});
Expand All @@ -183,7 +183,7 @@ public void flushDb(final RedisClusterNode node) {

Assert.notNull(node, "ClusterNode must not be null.");

execute((RedisClusterCallback<Void>) connection -> {
doInCluster((RedisClusterCallback<Void>) connection -> {
connection.flushDb(node);
return null;
});
Expand All @@ -198,7 +198,7 @@ public Collection<RedisClusterNode> getSlaves(final RedisClusterNode node) {

Assert.notNull(node, "ClusterNode must not be null.");

return execute(connection -> connection.clusterGetSlaves(node));
return doInCluster(connection -> connection.clusterGetSlaves(node));
}

/*
Expand All @@ -210,7 +210,7 @@ public void save(final RedisClusterNode node) {

Assert.notNull(node, "ClusterNode must not be null.");

execute((RedisClusterCallback<Void>) connection -> {
doInCluster((RedisClusterCallback<Void>) connection -> {
connection.save(node);
return null;
});
Expand All @@ -225,7 +225,7 @@ public void shutdown(final RedisClusterNode node) {

Assert.notNull(node, "ClusterNode must not be null.");

execute((RedisClusterCallback<Void>) connection -> {
doInCluster((RedisClusterCallback<Void>) connection -> {
connection.shutdown(node);
return null;
});
Expand All @@ -241,7 +241,7 @@ public void reshard(final RedisClusterNode source, final int slot, final RedisCl
Assert.notNull(source, "Source node must not be null.");
Assert.notNull(target, "Target node must not be null.");

execute((RedisClusterCallback<Void>) connection -> {
doInCluster((RedisClusterCallback<Void>) connection -> {

connection.clusterSetSlot(target, slot, AddSlots.IMPORTING);
connection.clusterSetSlot(source, slot, AddSlots.MIGRATING);
Expand All @@ -262,16 +262,13 @@ public void reshard(final RedisClusterNode source, final int slot, final RedisCl
* @return execution result. Can be {@literal null}.
*/
@Nullable
public <T> T execute(RedisClusterCallback<T> callback) {
<T> T doInCluster(RedisClusterCallback<T> callback) {

Assert.notNull(callback, "ClusterCallback must not be null!");

RedisClusterConnection connection = template.getConnectionFactory().getClusterConnection();

try {
try (RedisClusterConnection connection = template.getConnectionFactory().getClusterConnection()) {
return callback.doInRedis(connection);
} finally {
connection.close();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Long add(K key, Point point, M member) {
byte[] rawKey = rawKey(key);
byte[] rawMember = rawValue(member);

return execute(connection -> connection.geoAdd(rawKey, point, rawMember), true);
return execute(connection -> connection.geoAdd(rawKey, point, rawMember));
}

/*
Expand All @@ -88,7 +88,7 @@ public Long add(K key, Map<M, Point> memberCoordinateMap) {
rawMemberCoordinateMap.put(rawMember, memberCoordinateMap.get(member));
}

return execute(connection -> connection.geoAdd(rawKey, rawMemberCoordinateMap), true);
return execute(connection -> connection.geoAdd(rawKey, rawMemberCoordinateMap));
}

/*
Expand Down Expand Up @@ -117,7 +117,7 @@ public Distance distance(K key, M member1, M member2) {
byte[] rawMember1 = rawValue(member1);
byte[] rawMember2 = rawValue(member2);

return execute(connection -> connection.geoDist(rawKey, rawMember1, rawMember2), true);
return execute(connection -> connection.geoDist(rawKey, rawMember1, rawMember2));
}

/*
Expand All @@ -131,7 +131,7 @@ public Distance distance(K key, M member1, M member2, Metric metric) {
byte[] rawMember1 = rawValue(member1);
byte[] rawMember2 = rawValue(member2);

return execute(connection -> connection.geoDist(rawKey, rawMember1, rawMember2, metric), true);
return execute(connection -> connection.geoDist(rawKey, rawMember1, rawMember2, metric));
}

/*
Expand All @@ -144,7 +144,7 @@ public List<String> hash(K key, M... members) {
byte[] rawKey = rawKey(key);
byte[][] rawMembers = rawValues(members);

return execute(connection -> connection.geoHash(rawKey, rawMembers), true);
return execute(connection -> connection.geoHash(rawKey, rawMembers));
}

/*
Expand All @@ -156,7 +156,7 @@ public List<Point> position(K key, M... members) {
byte[] rawKey = rawKey(key);
byte[][] rawMembers = rawValues(members);

return execute(connection -> connection.geoPos(rawKey, rawMembers), true);
return execute(connection -> connection.geoPos(rawKey, rawMembers));
}

/*
Expand All @@ -168,7 +168,7 @@ public GeoResults<GeoLocation<M>> radius(K key, Circle within) {

byte[] rawKey = rawKey(key);

GeoResults<GeoLocation<byte[]>> raw = execute(connection -> connection.geoRadius(rawKey, within), true);
GeoResults<GeoLocation<byte[]>> raw = execute(connection -> connection.geoRadius(rawKey, within));

return deserializeGeoResults(raw);
}
Expand All @@ -182,7 +182,7 @@ public GeoResults<GeoLocation<M>> radius(K key, Circle within, GeoRadiusCommandA

byte[] rawKey = rawKey(key);

GeoResults<GeoLocation<byte[]>> raw = execute(connection -> connection.geoRadius(rawKey, within, args), true);
GeoResults<GeoLocation<byte[]>> raw = execute(connection -> connection.geoRadius(rawKey, within, args));

return deserializeGeoResults(raw);
}
Expand All @@ -196,8 +196,8 @@ public GeoResults<GeoLocation<M>> radius(K key, M member, double radius) {

byte[] rawKey = rawKey(key);
byte[] rawMember = rawValue(member);
GeoResults<GeoLocation<byte[]>> raw = execute(connection -> connection.geoRadiusByMember(rawKey, rawMember, radius),
true);
GeoResults<GeoLocation<byte[]>> raw = execute(
connection -> connection.geoRadiusByMember(rawKey, rawMember, radius));

return deserializeGeoResults(raw);
}
Expand All @@ -213,7 +213,7 @@ public GeoResults<GeoLocation<M>> radius(K key, M member, Distance distance) {
byte[] rawMember = rawValue(member);

GeoResults<GeoLocation<byte[]>> raw = execute(
connection -> connection.geoRadiusByMember(rawKey, rawMember, distance), true);
connection -> connection.geoRadiusByMember(rawKey, rawMember, distance));

return deserializeGeoResults(raw);
}
Expand All @@ -229,7 +229,7 @@ public GeoResults<GeoLocation<M>> radius(K key, M member, Distance distance, Geo
byte[] rawMember = rawValue(member);

GeoResults<GeoLocation<byte[]>> raw = execute(
connection -> connection.geoRadiusByMember(rawKey, rawMember, distance, param), true);
connection -> connection.geoRadiusByMember(rawKey, rawMember, distance, param));

return deserializeGeoResults(raw);
}
Expand All @@ -243,10 +243,10 @@ public Long remove(K key, M... members) {

byte[] rawKey = rawKey(key);
byte[][] rawMembers = rawValues(members);
return execute(connection -> connection.zRem(rawKey, rawMembers), true);
return execute(connection -> connection.zRem(rawKey, rawMembers));
}

/*
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.GeoOperations#search(java.lang.Object, org.springframework.data.redis.connection.RedisGeoCommands.GeoReference, org.springframework.data.redis.connection.RedisGeoCommands.GeoShape, org.springframework.data.redis.connection.RedisGeoCommands.GeoSearchCommandArgs)
*/
Expand All @@ -258,12 +258,12 @@ public GeoResults<GeoLocation<M>> search(K key, GeoReference<M> reference,
GeoReference<byte[]> rawMember = getGeoReference(reference);

GeoResults<GeoLocation<byte[]>> raw = execute(
connection -> connection.geoSearch(rawKey, rawMember, geoPredicate, args), true);
connection -> connection.geoSearch(rawKey, rawMember, geoPredicate, args));

return deserializeGeoResults(raw);
}

/*
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.GeoOperations#searchAndStore(java.lang.Object, java.lang.Object, org.springframework.data.redis.connection.RedisGeoCommands.GeoReference, org.springframework.data.redis.connection.RedisGeoCommands.GeoShape, org.springframework.data.redis.connection.RedisGeoCommands.GeoSearchStoreCommandArgs)
*/
Expand All @@ -275,7 +275,7 @@ public Long searchAndStore(K key, K destKey, GeoReference<M> reference,
byte[] rawDestKey = rawKey(destKey);
GeoReference<byte[]> rawMember = getGeoReference(reference);

return execute(connection -> connection.geoSearchStore(rawDestKey, rawKey, rawMember, geoPredicate, args), true);
return execute(connection -> connection.geoSearchStore(rawDestKey, rawKey, rawMember, geoPredicate, args));
}

@SuppressWarnings("unchecked")
Expand Down
Loading