我正在尝试通过 java 连接到 Redis,我有以下配置,
@SpringBootApplication
@Configuration
@ComponentScan(basePackages = "com.infy.redisDemo")
public class RedisDemoApplication {
@Bean
public LettuceConnectionFactory getConnectionFactory(){
LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory("localhost",6379);
return lettuceConnectionFactory;
}
@Bean
public RedisTemplate<String,Object> getRedisTemplate(){
RedisTemplate<String, Object> redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(getConnectionFactory());
return redisTemplate;
}
public static void main(String[] args) {
SpringApplication.run(RedisDemoApplication.class, args);
}
}
我的回购课程:
@Repository
public class UserRepoImpl implements UserRepository {
private RedisTemplate<String, Object> redisTemplate;
private HashOperations hashOperations;
public final String key = "USER";
public UserRepoImpl(RedisTemplate redisTemplate,
HashOperations hashOperations) {
this.redisTemplate = redisTemplate;
this.hashOperations = redisTemplate.opsForHash();
}
@Override
public void save(User user) {
hashOperations.put(key,user.getId(),user);
}
@Override
public void update(User user) {
hashOperations.put(key,user.getId(),user);
}
@Override
public Map findAll() {
return hashOperations.entries(key);
}
}
public interface UserRepository {
void save(User user);
void update(User user);
Map findAll();
}
万千封印
相关分类