Spring boot Redis integration step is as follows:
1, arranged in pom.xml related dependent jar:
<! - Load spring boot redis bag -> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2, the configuration information in connection redis Springboot core configuration file application.properties in:
spring.redis.host=192.168.230.128 spring.redis.port=6379 spring.redis.password=123456
3, the configuration of the above steps, Spring boot will automatically configure RedisTemplate, injection redisTemplate redis need to operate in the class of:
Redis entity class must implement the operation sequence of the interface
After the success of key discovery into redis have some special encoding format, readability is not high
spring boot injected redisTemplate class to help us, which can only write the generic <String, String>, <Object, Object>
In the case of high concurrency, this method may be problematic: may not be cached, are taking to the database query, to increase pressure on the database server.
In action in the simulated multi-threaded method to access the sevice
@Autowired private TClassService tClassService; @RequestMapping("index.do") public @ResponseBody Object list(){ //模拟高并发 Runnable runnable = new Runnable() { @Override public void run() { tClassService.list (); } }; ExecutorService executorService = Executors.newFixedThreadPool(10); for (int i=0;i<100;i++){ executorService.submit(runnable); } Return tClassService.list (); }
Modify service method prompts
public List<TClass> list() { RedisSerializer redisSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(redisSerializer); // First cache query List <TClass> List = (List <TClass>) redisTemplate.opsForValue () GET ( "data.class." ); // if there is no data in the cache, to fetch data from the database IF (List == null ) { System.out.println ( "database query" ); // get data from the database query results into the cache after List = classMapper.list (); redisTemplate.opsForValue().set("data.class",list); }else { System.out.println ( "query caching" ); } return list; }
Start, test results, many of which are issued in the database search, rather than the query cache
Principle: simultaneous multi-threading into the method
A solution: a synchronization method
Folder on the method synchronized keyword, synchronization method, efficiency is too low
Two solutions: a synchronization code blocks
public List<TClass> list() { RedisSerializer redisSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(redisSerializer); // First cache query List <TClass> List = (List <TClass>) redisTemplate.opsForValue () GET ( "data.class." ); IF (List == null ) { // If found, not the lock, to improve efficiency the synchronized ( the this ) { List = (List <TClass>) redisTemplate.opsForValue () GET ( "data.class." ); // if there is no data in the cache, to fetch data from the database IF (List == null ) { System.out.println ( "database query" ); // get data from the database query results into the cache after List = classMapper.list (); redisTemplate.opsForValue().set("data.class",list); }else { System.out.println ( "query caching" ); } } }else{ System.out.println ( "query caching" ); } return list; }
The test results, only the first query the database, the rest are query cache
Sentinel redis cluster configuration mode:
#redis cluster configuration mode Sentinel spring.redis.password=redis spring.redis.sentinel.master=mymaster spring.redis.sentinel.nodes=192.168.179.128:26380,192.168.179.128:26382,192.168.179.128:26384