当我使用springboot时,我使用redis作为我的缓存服务器。但是我读了 spring-data-redis 源代码,当逐出缓存时,代码是
byte[][] keys = Optional.ofNullable(connection.keys(pattern)).orElse(Collections.emptySet())
.toArray(new byte[0][]);
Run Code Online (Sandbox Code Playgroud)
Redis 建议将keys 命令替换为scan 命令。为什么Spring团队不这样做呢?
当redis通过spring boot()作为缓存技术时<artifactId>spring-boot-starter-data-redis</artifactId>,我看到文件中很少有像TTL这样的属性可以设置application.properties。前任:
spring.cache.cache-names=cache1,cache2
spring.cache.redis.time-to-live=600000
Run Code Online (Sandbox Code Playgroud)
以及附录 A 中的更多片段。常见应用程序属性
spring.redis.database=0 # Database index used by the connection factory.
spring.redis.url= # Connection URL. Overrides host, port, and password. User is ignored. Example: redis://user:password@example.com:6379
spring.redis.host=localhost # Redis server host.
Run Code Online (Sandbox Code Playgroud)
但我无法弄清楚如何设置缓存逐出策略,例如 - 最不常用或最近最后使用等。
我必须如何以及在哪里提供此配置详细信息?
我有一个连接到 AWS DynamoDB 的应用程序。我正在尝试使用 Jedis 在此应用程序中实现 Redis 连接。
但是,就在我添加此依赖项之后 -
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我收到这个错误 -
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userInfoRepository in org.csulb.md.service.DBService required a bean of type 'org.csulb.md.repo.UserInfoRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'org.csulb.md.repo.UserInfoRepository' in your configuration.
Run Code Online (Sandbox Code Playgroud)
UserInfoRepository 实现了 CrudRepository。
我不确定我错过了什么。简而言之,向此代码添加 spring-boot-starter-data-redis 依赖项会出现此错误。如果没有这种依赖性,代码就可以正常工作。
下面是我的其余代码。
主应用程序.java
package org.csulb.md;
@SpringBootApplication
public class MainApp {
public static void main(String[] args) {
SpringApplication.run(MainApp.class, …Run Code Online (Sandbox Code Playgroud) jedis spring-data spring-data-redis spring-boot aws-java-sdk-dynamodb
我是缓存事物的新手,并为我的 Spring Boot 应用程序学习一些不同的解决方案。我正在研究 Spring Cache,它是比我看到的 redis 缓存更简单的缓存机制(这就是我所寻找的)。而且还有很多像“spring+redis缓存”这样的资源。当我查看简单用法时,我发现没有任何区别。即使注释是相同的(Cacheable、CacheEvict、CachePut 等),除了额外的 redis 配置和 redis docker 容器等之外,我看不到用法上的差异...而且这些spring+redis 缓存文章都没有告诉我们有什么区别介于 spring 缓存和 spring+redis 缓存之间。
redis缓存相对于spring缓存有什么优势?或者你能告诉一个简单的用例,我肯定需要使用 redis 缓存,而我无法使用 spring 缓存实现它吗?
当我调用get()方法时,发生了异常
这是代码
@Service("RedisService")
public class RedisServiceImpl implements RedisService {
@Autowired
RedisTemplate<String, Long> redisTemplate;
@Override
public Long get(String key) {
return redisTemplate.opsForValue().get(key);
}
@Override
public Long incrBy(String key, long increment) {
return redisTemplate.opsForValue().increment(key, increment);
}
Run Code Online (Sandbox Code Playgroud)
当我使用incrBy方法时,没有例外但只有错误只有get方法
在这里才是栈跟踪---
java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2280)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2749)
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:779)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)
at org.springframework.core.serializer.DefaultDeserializer.deserialize(DefaultDeserializer.java:38)
at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:58)
at org.springframework.core.serializer.support.DeserializingConverter.convert(DeserializingConverter.java:1)
at org.springframework.data.redis.serializer.JdkSerializationRedisSerializer.deserialize(JdkSerializationRedisSerializer.java:40)
at org.springframework.data.redis.core.AbstractOperations.deserializeValue(AbstractOperations.java:198)
at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:50)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:162)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:133)
at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:84)
at org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:42)
at net.daum.air21.bot.common.service.RedisServiceImpl.get(RedisServiceImpl.java:29)
at net.daum.air21.bot.user.service.SeraCoffeeServiceImpl.getCurrentCount(SeraCoffeeServiceImpl.java:41)
Run Code Online (Sandbox Code Playgroud) 根据这个答案,人们RedisTemplate不能支持多个值序列化器.所以我想为不同的需求创建多个RedisTemplate,特别是一个用于字符串操作,一个用于JSON序列化的对象,用于RedisCacheManager.我正在使用Spring Boot并且当前RedisTemplate是自动装配的,我想知道声明第二个RedisTemplate实例共享同一个Jedis连接工厂但有自己的序列化器的正确方法是什么?
在两个不同的组件中尝试过类似的东西,
组件1声明,
@Autowired
private RedisTemplate redisTemplate;
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Instance.class));
Run Code Online (Sandbox Code Playgroud)
组件2声明,
@Autowired
private StringRedisTemplate stringRedisTemplate;
Run Code Online (Sandbox Code Playgroud)
在这种情况下,两个模板实际上是相同的.追溯到Spring代码,发现组件1的模板已解析为自动配置stringRedisTemplate.
手动调用RedisTemplate的构造函数然后afterPropertiesSet()它将无法工作,因为它抱怨没有找到连接工厂.
我知道这个请求可能与在Spring应用程序中定义另一个bean没什么大不同,但不确定当前的Spring-Data-Redis集成对我来说是最好的方式.请帮忙,谢谢.
我想用KeyExpirationEventMessageListener监听过期事件,但找不到示例。
有人知道如何使用Spring Boot 1.4.3和Spring Data Redis吗?
我目前正在这样做
JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
this.jedis = pool.getResource();
this.jedis.psubscribe(new JedisPubSub() {
@Override
public void onPMessage(String pattern, String channel, String message) {
System.out.println("onPMessage pattern " + pattern + " " + channel + " " + message);
List<Object> txResults = redisTemplate.execute(new SessionCallback<List<Object>>() {
public List<Object> execute(RedisOperations operations) throws DataAccessException {
operations.multi();
operations.opsForValue().get("val:" + message);
operations.delete("val:" + message);
return operations.exec();
}
});
System.out.println(txResults.get(0));
}
}, "__keyevent@0__:expired");
Run Code Online (Sandbox Code Playgroud)
我想直接使用Spring代替Jedis。
问候
我想使用spring-data-redis在spring boot应用程序中缓存数据。但是它总是说强制转换异常。我用了一段时间的Google搜索,但是没有任何想法。请帮帮我。谢谢,任何建议都很棒!
这是我的RedisConfiguation:
@Configuration
@EnableCaching
public class RedisConfiguration extends CachingConfigurerSupport {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private String port;
@Value("${spring.redis.expire}")
private String expire;
@Value("${spring.redis.database}")
private String databaseIndex;
@Value("${spring.redis.password}")
private String password;
@Bean
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
public Object generate(Object target, Method method, Object... params) {
if (params.length == 0) {
return "reids_" + method.getName();
}
if (params.length == 1) {
Object param = params[0];
if (param != null && !param.getClass().isArray()) {
return "reids_" + method.getName() + …Run Code Online (Sandbox Code Playgroud) 有什么方法可以检查RedisTemplate是否存在密钥吗?或者换句话说,existsRedisTemplate API中是否有等效的Redis 命令?
我正在春季缓存抽象注释,以将缓存应用于我的服务方法。
由于我将Redis用作缓存存储,因此我想使用在特定时间使缓存过期的选项,因为Redis支持该选项。Redis中的expireat命令可用于设置将来的到期时间。
我不确定使用RedisCache时如何对属于缓存的键进行操作。
我试图通过创建一个bean来定制RedisCacheManager。
我看到有一个暴露的getNativeCache()方法。但是我没有找到任何方法来设置使用它的expireat的值。
如果有一种自定义RedisCacheManager的方法,以便特定缓存的所有键在到期时使用同一时间,请告诉我。