Skip to content

Commit

Permalink
Support redis specific repository.
Browse files Browse the repository at this point in the history
  • Loading branch information
junghoon-vans committed Dec 28, 2023
1 parent 2e29a99 commit 689fdad
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.springframework.data.redis.repository;

import org.springframework.data.keyvalue.repository.KeyValueRepository;

/**
* Redis specific {@link org.springframework.data.repository.Repository} interface.
*
* @author Junghoon Ban
* @param <T>
* @param <ID>
*/
public interface RedisRepository<T, ID> extends KeyValueRepository<T, ID> {}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
import java.util.Collection;
import java.util.Collections;

import java.util.List;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.data.keyvalue.repository.KeyValueRepository;
import org.springframework.data.keyvalue.repository.config.KeyValueRepositoryConfigurationExtension;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.RedisKeyValueAdapter;
Expand All @@ -35,6 +37,7 @@
import org.springframework.data.redis.core.convert.MappingRedisConverter;
import org.springframework.data.redis.core.convert.RedisCustomConversions;
import org.springframework.data.redis.core.mapping.RedisMappingContext;
import org.springframework.data.redis.repository.RedisRepository;
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
import org.springframework.data.repository.config.RepositoryConfigurationSource;
import org.springframework.util.StringUtils;
Expand All @@ -44,6 +47,7 @@
*
* @author Christoph Strobl
* @author Mark Paluch
* @author Junghoon Ban
* @since 1.7
*/
public class RedisRepositoryConfigurationExtension extends KeyValueRepositoryConfigurationExtension {
Expand All @@ -69,6 +73,11 @@ protected String getDefaultKeyValueTemplateRef() {
return "redisKeyValueTemplate";
}

@Override
protected Collection<Class<?>> getIdentifyingTypes() {
return List.of(RedisRepository.class, KeyValueRepository.class);
}

@Override
public void registerBeansForRoot(BeanDefinitionRegistry registry, RepositoryConfigurationSource configuration) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* @author Christoph Strobl
* @author Oliver Gierke
* @author Mark Paluch
* @author Junghoon Ban
* @since 1.7
*/
public class RedisRepositoryFactory extends KeyValueRepositoryFactory {
Expand Down Expand Up @@ -75,6 +76,11 @@ public RedisRepositoryFactory(KeyValueOperations keyValueOperations,
this.operations = keyValueOperations;
}

@Override
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
return SimpleRedisRepository.class;
}

@Override
protected RepositoryFragments getRepositoryFragments(RepositoryMetadata metadata) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.springframework.data.redis.repository.support;

import org.springframework.data.keyvalue.core.KeyValueOperations;
import org.springframework.data.keyvalue.repository.support.SimpleKeyValueRepository;
import org.springframework.data.redis.repository.RedisRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.core.EntityInformation;

/**
* Redis specific repository implementation.
*
* @author Junghoon Ban
* @param <T>
* @param <ID>
*/
@NoRepositoryBean
public class SimpleRedisRepository<T, ID> extends SimpleKeyValueRepository<T, ID> implements RedisRepository<T, ID> {

public SimpleRedisRepository(EntityInformation<T, ID> metadata, KeyValueOperations operations) {
super(metadata, operations);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.Collection;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry;
Expand All @@ -33,6 +32,7 @@
import org.springframework.data.keyvalue.repository.KeyValueRepository;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.RedisKeyValueAdapter.EnableKeyspaceEvents;
import org.springframework.data.redis.repository.RedisRepository;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
import org.springframework.data.repository.config.RepositoryConfiguration;
Expand Down Expand Up @@ -65,6 +65,12 @@ void isStrictMatchIfRepositoryExtendsStoreSpecificBase() {
assertHasRepo(StoreRepository.class, extension.getRepositoryConfigurations(configurationSource, loader, true));
}

@Test // GH-2816
void isStrictMatchIfRepositoryExtendsRedisSpecificBase() {
assertHasRepo(KeyValueSpecificRepository.class,
extension.getRepositoryConfigurations(configurationSource, loader, true));
}

@Test // DATAREDIS-425
void isNotStrictMatchIfDomainTypeIsNotAnnotatedWithDocument() {

Expand Down Expand Up @@ -197,5 +203,8 @@ interface SampleRepository extends Repository<Sample, Long> {}

interface UnannotatedRepository extends Repository<Object, Long> {}

interface StoreRepository extends KeyValueRepository<Object, Long> {}
interface StoreRepository extends RedisRepository<Object, Long> {}

interface KeyValueSpecificRepository extends KeyValueRepository<Object, Long> {}

}

0 comments on commit 689fdad

Please sign in to comment.