Skip to content
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

Cyclic references resolve #2491

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
* @author Denis Zavedeev
* @author ihaohong
* @author Chen Li
* @author Ilya Viaznin
* @author Vedran Pavic
* @param <K> the Redis key type against which the template works (usually a String)
* @param <V> the Redis value type against which the template works
Expand All @@ -112,8 +113,7 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
private final ValueOperations<K, V> valueOps = new DefaultValueOperations<>(this);
private final ListOperations<K, V> listOps = new DefaultListOperations<>(this);
private final SetOperations<K, V> setOps = new DefaultSetOperations<>(this);
private final StreamOperations<K, ?, ?> streamOps = new DefaultStreamOperations<>(this,
ObjectHashMapper.getSharedInstance());
private final StreamOperations<K, ?, ?> streamOps;
private final ZSetOperations<K, V> zSetOps = new DefaultZSetOperations<>(this);
private final GeoOperations<K, V> geoOps = new DefaultGeoOperations<>(this);
private final HyperLogLogOperations<K, V> hllOps = new DefaultHyperLogLogOperations<>(this);
Expand All @@ -122,7 +122,18 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
/**
* Constructs a new <code>RedisTemplate</code> instance.
*/
public RedisTemplate() {}
public RedisTemplate() {
streamOps = new DefaultStreamOperations<>(this, ObjectHashMapper.getSharedInstance());
}

/**
* Constructs a new {@link RedisTemplate} instance with custom hash mapper
*
* @param hashMapper Custom {@link ObjectHashMapper} instance
*/
public RedisTemplate(ObjectHashMapper hashMapper) {
streamOps = new DefaultStreamOperations<>(this, hashMapper);
}

@Override
public void afterPropertiesSet() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.springframework.data.redis.core;

import org.springframework.data.redis.core.convert.RedisConverter;
import org.springframework.data.redis.core.convert.RedisCustomConversions;
import org.springframework.data.redis.core.convert.ReferenceMappingRedisConverter;
import org.springframework.data.redis.core.mapping.RedisMappingContext;
import org.springframework.lang.Nullable;

/**
* Wrapper for {@link RedisKeyValueAdapter} with correct cyclic reference resolving
*
* @author Ilya Viaznin
* @see RedisKeyValueAdapter
*/
public class ReferenceRedisAdapter extends RedisKeyValueAdapter {

/**
* Evaluation caching
*/
private boolean isReferenceConverter;

/**
* Creates new {@link ReferenceRedisAdapter} with default {@link RedisMappingContext} and default
* {@link RedisCustomConversions}.
*
* @param redisOps must not be {@literal null}.
*/
public ReferenceRedisAdapter(RedisOperations<?, ?> redisOps) {
this(redisOps, new RedisMappingContext());
}

/**
* Creates new {@link ReferenceRedisAdapter} with default {@link RedisCustomConversions}.
*
* @param redisOps must not be {@literal null}.
* @param mappingContext must not be {@literal null}.
*/
public ReferenceRedisAdapter(RedisOperations<?, ?> redisOps, RedisMappingContext mappingContext) {
this(redisOps, mappingContext, new RedisCustomConversions());
}

/**
* Creates new {@link ReferenceRedisAdapter}.
*
* @param redisOps must not be {@literal null}.
* @param mappingContext must not be {@literal null}.
* @param customConversions can be {@literal null}.
* @since 2.0
*/
public ReferenceRedisAdapter(RedisOperations<?, ?> redisOps, RedisMappingContext mappingContext,
@Nullable org.springframework.data.convert.CustomConversions customConversions) {
super(redisOps, mappingContext, customConversions);
}

/**
* Creates new {@link ReferenceRedisAdapter} with specific {@link RedisConverter}.
*
* @param redisOps must not be {@literal null}.
* @param redisConverter must not be {@literal null}.
*/
public ReferenceRedisAdapter(RedisOperations<?, ?> redisOps, RedisConverter redisConverter) {
super(redisOps, redisConverter);
isReferenceConverter = redisConverter instanceof ReferenceMappingRedisConverter;
}

@Override
public <T> T get(Object id, String keyspace, Class<T> type) {
T val;
if (isReferenceConverter) {
var converter = (ReferenceMappingRedisConverter) getConverter();
converter.clearResolvedCtx();
val = super.get(id, keyspace, type);
converter.clearResolvedCtx();
}
else
val = super.get(id, keyspace, type);

return val;
}
}
Loading