您的位置:

如何正确配置SpringRedis,提升网站性能

现在很多网站需要处理海量的访问请求,如何提升网站的性能成为了一个紧迫的问题。使用缓存技术可以显著地减少对数据库的访问,从而有效提高网站响应速度,降低数据库负载。SpringRedis是一种流行的缓存框架,它提供了丰富的API和配置选项,下面将介绍如何正确配置SpringRedis,帮助你提升网站的性能。

一、安装和配置SpringRedis

1、安装Redis

$ wget http://download.redis.io/releases/redis-5.0.6.tar.gz
$ tar xzf redis-5.0.6.tar.gz
$ cd redis-5.0.6
$ make
$ make install

2、添加SpringRedis依赖

<dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-redis</artifactId>
   <version>2.3.2.RELEASE</version>
</dependency>

3、配置RedisTemplate

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setDefaultSerializer(new GenericJackson2JsonRedisSerializer());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }
}

二、使用SpringRedis

1、添加缓存注解

@Service
@CacheConfig(cacheNames = "books")
public class BookService {

    @Autowired
    private BookRepository bookRepository;

    @Cacheable
    public Book getById(Long id) {
        return bookRepository.findById(id).orElse(null);
    }

    @Cacheable
    public Book getByTitle(String title) {
        return bookRepository.findByTitle(title).orElse(null);
    }

    @CachePut(key = "#book.id")
    public Book save(Book book) {
        bookRepository.save(book);
        return book;
    }

    @CacheEvict(key = "#id")
    public void deleteById(Long id) {
        bookRepository.deleteById(id);
    }
}

2、使用缓存

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    
    private final BookRepository bookRepository;

    public BookServiceImpl(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }

    public Book getById(Long id) {
        String key = "book_" + id;
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        Book book = (Book) operations.get(key);
        if (book == null) {
            book = bookRepository.findById(id).orElse(null);
            if (book != null) {
                operations.set(key, book);
            }
        }
        return book;
    }
}

三、优化SpringRedis性能

1、缓存预热

public class CacheListener implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        ValueOperations<String, Object> operations = redisTemplate.opsForValue();
        List<Book> books = bookRepository.findAll();
        for (Book book : books) {
            String key = "book_" + book.getId();
            operations.set(key, book);
        }
    }
}

2、注解优化

@Service
@CacheConfig(cacheNames = "books")
public class BookService {

    @Autowired
    private BookRepository bookRepository;
    
    @Cacheable(key = "#id", unless="#result == null")
    public Book getById(Long id) {
        return bookRepository.findById(id).orElse(null);
    }

    @Cacheable(key = "#title", unless="#result == null")
    public Book getByTitle(String title) {
        return bookRepository.findByTitle(title).orElse(null);
    }

    @CachePut(key = "#book.id", condition="#book.price > 0")
    public Book save(Book book) {
        bookRepository.save(book);
        return book;
    }

    @CacheEvict(key = "#id")
    public void deleteById(Long id) {
        bookRepository.deleteById(id);
    }
}

3、哨兵配置

spring.redis.sentinel.master = mymaster
spring.redis.sentinel.nodes = 127.0.0.1:26379,127.0.0.1:26380,127.0.0.1:26381
spring.redis.sentinel.password = password

四、总结

本文介绍了如何正确配置SpringRedis以及如何使用和优化SpringRedis。通过使用SpringRedis缓存框架,可以有效地提高网站的性能,减少数据库负载。同时,合理的设置缓存注解和缓存策略也可以进一步提高SpringRedis的性能。