在我的测试项目中,我通过 spring-data-redis 和 jedis 构建了我的缓存系统。和valueSerializer的RedisTemplate是GenericJackson2JsonRedisSerializer。但这里有一个问题,当我从 java.lang.Long 类型的缓存中范围一个列表时,有一个java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long. 任何人都知道原因,它让我困惑了很长时间;
Redis 配置细节如下:
<bean id="objRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="redisConnectionFactory" />
<property name="keySerializer">
<bean
class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="valueSerializer">
<bean
class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
代码详情如下:
public List<Long> findByProductId(Long productId) {
String key = ThemeRedisKeyGenerator.genProductThemeKey(productId);
List<Long> themeIdList = redisService.range(key, Long.class);
if (themeIdList == null) {
themeIdList = themeProductMapper
.selectThemeIdByProductId(productId);
if (!redisService.containKey(key)) {
redisService.rightPush(key,
ThemeRedisKeyGenerator.PRODUCT_THEME_KEY_EXPIRE_HOURS,
TimeUnit.HOURS, themeIdList.toArray());
}
}
return themeIdList;
} …Run Code Online (Sandbox Code Playgroud)