我必须根据需要为每个请求(写/读)创建 RedisTemplate。连接工厂是 JedisConnectionFactory
JedisConnectionFactory factory=new
JedisConnectionFactory(RedisSentinelConfiguration,JedisPoolConfig);
Run Code Online (Sandbox Code Playgroud)
有一次,我用RedisTemplate.opsForHash/opsForValue做操作,如何安全的处理模板,让连接返回到JedisPool。
到目前为止,我用
template.getConnectionFactory().getConnection().close();
Run Code Online (Sandbox Code Playgroud)
这是正确的方法吗?
受到这个答案的启发,我尝试执行以下操作
@NoRepositoryBean
public interface CacheableRepository<T, ID extends Serializable>
extends JpaRepository<T, ID>, JpaSpecificationExecutor<T>, PagingAndSortingRepository<T, ID> {
@Cacheable()
T findOne(ID id);
@Cacheable()
List<T> findAll();
@Cacheable()
Page<T> findAll(Pageable pageable);
@CacheEvict(allEntries = true)
<S extends T> S save(S entity);
@CacheEvict(allEntries = true)
void delete(ID id);
}
Run Code Online (Sandbox Code Playgroud)
然后使用这个接口来定义Used存储库
@CacheConfig(cacheNames={"uniqueCache"})
public interface SomeObjectRepo extends CacheableRepository<SomeObject, Long> {
@Cacheable(key = "someobject+#p0")
List<SomeObject> findByType(Integer type);
@Cacheable(key = "someobject+#p0")
List<SomeObject> findByCategory(String field);
@Cacheable(key="someobject+#p0.concat(#p1)")
List<SomeObject> findByCategoryAndWizardType_id(String field,Integer id);
}
Run Code Online (Sandbox Code Playgroud)
上面的缓存适用于findByType, findByCategory,findByCategoryAndWizardType_id
cacheable …
我正在使用spring-data-redis,spring-session和 Spring Boot 连接到我的 Redis 实例。但是,我不想spring-data-redis连接到 DB 0(默认),而是连接到另一个本地数据库(比如 DB 1)。这是我想要存储会话的地方。spring-data-redis 可以实现吗?
我使用 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 :在允许 …
我正在使用 spring redisTemplate 和 redis 扫描
它可以在单节点的情况下查找。
但在集群环境下就不行了
我无法获取数据。
有没有办法在集群环境中获取扫描数据?
这是我的 spring redisTemplate 代码。
//String key="products:aa";
//String key="products:aac";
//String key="products:ab";
//String key="products:ac";
String workKey="products:aa*";
ScanOptions options = ScanOptions.scanOptions().match(workKey).count(100).build();
ScanOptions options1 = ScanOptions.scanOptions().build();
RedisConnectionFactory factory = redisTemplate.getConnectionFactory();
RedisConnection conn = factory.getConnection();
Cursor<byte[]> cursor = conn.scan(options);
List<Product> result = new ArrayList<Product>();
while(cursor.hasNext()){
String key=new String((byte[]) cursor.next());
Product pa=getById(key.replace("products:",""));
result.add(pa);
}
//result
//String key="products:aa";
//String key="products:aac";
Run Code Online (Sandbox Code Playgroud) 我有一个 Azure Redis 缓存 - 启用了高级和集群。我一直在尝试使用spring-boot-starter-data-redis(spring boot version: 2.3.4.RELEASE, Java version: 11) 并使用生菜客户端连接到该 Redis ,但是当我将 Redis 视为 Redis 集群时,生菜抛出以下 SSL 异常,但在使用它时连接很好作为独立的 Redis 服务器。
我的pom.xml依赖项是:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
爪哇代码:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.*;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
@Configuration
class LettuceConfig {
@Bean
StringRedisTemplate getStringRedisTemplate(final RedisProperties redisProperties) { …Run Code Online (Sandbox Code Playgroud) redis ×6
java ×5
spring ×3
spring-data ×3
spring-boot ×2
jedis ×1
lettuce ×1
spring-cache ×1
ssl ×1