現在很多網站需要處理海量的訪問請求,如何提升網站的性能成為了一個緊迫的問題。使用緩存技術可以顯著地減少對資料庫的訪問,從而有效提高網站響應速度,降低資料庫負載。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的性能。
原創文章,作者:OQLZJ,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/313703.html