我使用 spring-data-redis 版本 1.7.0.M1,jedis 版本 2.8.0\n这是我的配置
\n\n\n\n\n\n\n\n\n\n\n\n\n
\n\n<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">\n <property name="connectionFactory" ref="redisConnectionFactory"></property>\n <property name="keySerializer">\n <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>\n </property>\n <property name="hashKeySerializer">\n <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>\n </property>\n <property name="valueSerializer">\n <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>\n </property>\n <property name="hashValueSerializer">\n <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>\n </property>\n</bean>\nRun Code Online (Sandbox Code Playgroud)\n\n并使用 \xe3\x80\x90redisTemplate.opsForValue().get("foo")\xe3\x80\x91 进行测试
\n\n抛出异常
\n\n org.springframework.dao.InvalidDataAccessApiUsageException: MOVED 12182 192.168.1.223:7002; nested exception is redis.clients.jedis.exceptions.JedisMovedDataException: MOVED 12182 192.168.1.223:7002\nRun Code Online (Sandbox Code Playgroud)\n\n使用spring-data-redis 1.7.0.M1时如何配置redis-cluster?
\n我正在做Spring Redis,我把钥匙作为
redistemplate.opsForHash().put("Customer", Customer.class, List<Customers>)
Run Code Online (Sandbox Code Playgroud)
我想从 搜索List<>,
ScanOptions options = ScanOptions.scanOptions().match(pattern).count(1).build();
Cursor<Entry<Object, Object>> keys = redistemplate.opsForHash().scan("Customer", options);
Run Code Online (Sandbox Code Playgroud)
也不工作。请帮忙!!
我们有几个使用 Java Spring 编写的 Web 应用程序,我们正在使用spring-data-redis和@EnableRedisHttpSession。我想知道春季会议的内部结构是什么。在创建新会话之前,它会检查 Redis 数据库是否有重复的会话密钥?
我查看了 spring 文档,也进行了谷歌搜索,但无法得到明确的答案。
我正在将 spring 会话与 redis 一起使用,但我想在进行测试时禁用它。我的班级有注释:
@ActiveProfiles("integrationtests")
Run Code Online (Sandbox Code Playgroud)
我的 application-integrationtests.tml 文件包含:
spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
Run Code Online (Sandbox Code Playgroud)
但它仍然失败:
Caused by: org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
Run Code Online (Sandbox Code Playgroud)
如果我打开 redis-server ,测试就会工作,但我当然不想保持这种状态;)
//更新
我一直在尝试
@SpringBootTest(classes = {Application.class})
@ActiveProfiles("integrationtests")
Run Code Online (Sandbox Code Playgroud)
和排除 Redis 的 Application.class:
@SpringBootApplication(exclude={SessionAutoConfiguration.class, RedisAutoConfiguration.class, RedisHttpSessionConfiguration.class})
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
但它失败了:
Error creating bean with name 'redisMessageListenerContainer' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]
Run Code Online (Sandbox Code Playgroud)
spring autoconfigure debug看到我已经排除了这个类,但是没有效果: …
我想在 Spring Boot 中使用 RedisTemplate。我可以成功使用StringRedisTemplate,但无法使用RedisTemplate。这是代码。
\n\n@RunWith(SpringRunner.class)\n@SpringBootTest\npublic class RedisEntityTests {\n\n @Autowired\n private StringRedisTemplate stringRedisTemplate;\n\n @Autowired\n private RedisTemplate<String, RedisEntity> redisTemplate;\n\n // This test case can run successfully.\n @Test\n public void testString() {\n // save string\n stringRedisTemplate.opsForValue().set("aaa", "111");\n Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa"));\n }\n\n // This test case I got error.\n @Test\n public void testObject() throws Exception {\n // save object\n RedisEntity redisEntity = new RedisEntity("Tom", 20);\n redisTemplate.opsForValue().set(redisEntity.getName(), redisEntity);\n\n Assert.assertEquals(20, (redisTemplate.opsForValue().get("Tom")).getAge().longValue());\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n然后,运行测试方法\xef\xbc\x9atestObject(),这里是错误报告\xef\xbc\x9a
\n\n\n2017-12-17 16:12:12.079 错误 4708 --- [ main]\n ostest.context.TestContextManager :在允许 …
在下面的代码中,如果我想在redis数据库中将id存储为student_id,将name存储为student_name,并且我不想更改java代码中的变量名称。有没有办法为变量提供别名,就像我们在 spring data jpa 中所做的那样,我们在 @Column 注释中指定列的名称。如果有人有相同的解决方案,请帮助我。
@RedisHash("Student")
public class Student implements Serializable
{
@Id
private Long id;
private String name;
private float marksObtained;
private String schoolName;
}
Run Code Online (Sandbox Code Playgroud) 我将 spring boot (无关)与 spring-data-redis:jar:2.0.9 结合使用,它使用 lettuce 连接到我的 REDIS。我使用的哈希结构包含大约 100 个键。在这些键下,我放置了一些类型也不相关的对象:
private static final String HASH_KEY_NAME = "myspecialhashes:somekey";
@Autowired
private RedisTemplate<String, MyDto> myDtoRedisTemplate;
Run Code Online (Sandbox Code Playgroud)
现在我只是将对象列表放入哈希中,使用它们的 id 作为键:
myDtoRedisTemplate.opsForHash().put(HASH_KEY_NAME, dto.getId(), dto);
Run Code Online (Sandbox Code Playgroud)
这工作得很好,并且从哈希中检索所有元素都很好,并且仅检索键
List allDtosRaw = myDtoRedisTemplate.opsForHash().values(HASH_KEY_NAME);
Run Code Online (Sandbox Code Playgroud)
另外,在列出键时:
myDtoRedisTemplate.boundHashOps(HASH_KEY_NAME).keys()
Run Code Online (Sandbox Code Playgroud)
看起来不错,返回的密钥集以以下内容开头:
(java.util.LinkedHashSet<E>) [fakeservicetest:dummy3:write, fakesingle:dummy:sub1:write, ....
Run Code Online (Sandbox Code Playgroud)
由于有很多键,我希望能够使用 HSCAN 过滤以令牌开头的对象列表,而不是获取所有键并在我的 Java 应用程序中过滤它们。所以,这就是我如何进行 HSCAN 来获取所有以“fake”开头的哈希条目
List filteredDtosRaw = new LinkedList<>();
ScanOptions scanOptions = ScanOptions.scanOptions().match("fake*").count(10000).build();
Cursor cursor = myDtoRedisTemplate.boundHashOps(HASH_KEY_NAME).scan(scanOptions);
cursor.forEachRemaining(filteredDtosRaw ::add);
Run Code Online (Sandbox Code Playgroud)
不幸的是,这返回零结果。我尝试了各种方法来解决这个问题并取得了一些成果。最终我转向 redis 命令行来看看 REDIS 是如何看待这一切的
redis-cli HSCAN "myspecialhashes:somekey" 0 MATCH "fake*" COUNT 1000
Run Code Online (Sandbox Code Playgroud)
确实是零结果。接下来的事情是查看其中的所有键并查看哈希中实际包含的内容
redis-cli …Run Code Online (Sandbox Code Playgroud) 所以,今天遇到了一个有趣的问题,弄乱了 spring-boot-starter-data-rest。我的用例是在 Redis 上存储一些具有一定过期时间的数据,以便在该时间过去后它将被驱逐。我已配置好所有内容,并且可以在本地 redis 上正常运行,没有任何问题。当尝试使用安全连接 (SSL) 在 AWS 上使用它时,它会成为一个问题,并且是一个示例错误:
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766)
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
at com.springbootapp.config.Application.main(Application.java:16)
... 6 more
Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: ERR Unsupported CONFIG parameter: notify-keyspace-events; nested exception is redis.clients.jedis.exceptions.JedisDataException: ERR Unsupported CONFIG parameter: notify-keyspace-events
at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:44)
at org.springframework.data.redis.connection.jedis.JedisExceptionConverter.convert(JedisExceptionConverter.java:36)
at org.springframework.data.redis.PassThroughExceptionTranslationStrategy.translate(PassThroughExceptionTranslationStrategy.java:37)
at org.springframework.data.redis.FallbackExceptionTranslationStrategy.translate(FallbackExceptionTranslationStrategy.java:37)
at org.springframework.data.redis.connection.jedis.JedisConnection.convertJedisAccessException(JedisConnection.java:210)
at org.springframework.data.redis.connection.jedis.JedisConnection.setConfig(JedisConnection.java:633)
at org.springframework.session.data.redis.config.ConfigureNotifyKeyspaceEventsAction.configure(ConfigureNotifyKeyspaceEventsAction.java:63)
at org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration$EnableRedisKeyspaceNotificationsInitializer.afterPropertiesSet(RedisHttpSessionConfiguration.java:167) …Run Code Online (Sandbox Code Playgroud) 我正在将一些微服务从 SpringBoot1.5 移植到 2.1。
我们正在使用 spring-data-redis。似乎默认的内部移动从 jedis 到 lettuce。
问题是我们现在观察到一些奇怪的行为,当我们保存一个对象然后检索它时,有一个微小的差异:
空列表属性将替换为 null。
这是一个例子:
//repo
public interface TestRepository extends CrudRepository<Test, String> {}
...
//object
@RedisHash(timeToLive = 60)
public static class Test{
@Id private String id;
int age;
List<String> friends;
}
...
//saving then retreiving
Test test = new Test("1", 15, Collections.emptyList());
System.out.println(test);
testRepository.save(test);
Test testGet = testRepository.findById("1").get();
System.out.println(testGet);
Run Code Online (Sandbox Code Playgroud)
发生的事情是这样的:
//before
{
"id": "1",
"age": 15,
"friends": []
}
//after
{
"id": "1",
"age": 15
}
Run Code Online (Sandbox Code Playgroud)
空列表friends消失了。这种新行为会在许多地方影响我们的代码,导致 NullPointerException 等。
显然,有多个可用的序列化器,但这似乎没有任何效果。任何想法? …
ZSETs 用于在 redis 中存储排序集。我用于spring-data-redisZSetOperations。
所有范围方法都返回Set<>而不是SortedSet<>,我想知道其背后的原因是什么?
另外,由于这些不返回排序集,所有反向范围操作是否都会变得多余,因为无论如何都不会维护顺序,并且我们不能只获取索引处的任何元素?
我可能会遗漏一些非常明显的东西,请原谅我在这件事上的天真。
我的用例:
我正在存储带有日期的值作为分数。我想获取两个日期之间的所有值,检查计数并使用最近的日期。
我本来打算做类似的事情:
// get reverse sorted values, with most recent date being on top
SrotedSet<String> values = zSetOperations.rangeByScore(key, this.getBeginDate(), this.getEndDate());
// check values size and do values.get(0) i.e. the most recent date.
Run Code Online (Sandbox Code Playgroud)
但返回类型rangeByScore为Set,因此没有“get”操作。
我必须获得分数范围,然后自己对它们进行排序。
或者我必须转换为数组并获取元素。
或者做类似的事情values.iterator().next()