文章目录
- 1.依赖和配置:
- 2.操作数据库(查询缓存):
- service:
- controller:
- mapper
- 3.结果:
1.依赖和配置:
org.springframework.boot
spring-boot-starter-data-redis
RedisConfig:
package com.english.english_vision.config;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.data.redis.cache.RedisCacheManager;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.lang.reflect.Method;
import java.time.Duration;
@EnableCaching
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
private Jackson2JsonRedisSerializer
2.操作数据库(查询缓存):
service:
public interface ISongService extends IService {
List listbyName(String name);
}
@Service
public class SongServiceImpl extends ServiceImpl implements ISongService {
@Autowired
private RedisService redisService;
@Autowired
private SongMapper songMapper;
@Autowired
private RedisTemplate redisTemplate;
@Override
public List listbyName(String name) {
List songs = null;
String key = "songlist:"+name ;
ValueOperations> operations = redisTemplate.opsForValue();
//判断redis中是否有键为key的缓存
boolean hasKey = redisTemplate.hasKey(key);
if(hasKey) {
songs = operations.get(key);
return songs;
}
else {
songs = songMapper.listbyName(name);
operations.set(key,songs,3, TimeUnit.HOURS);
}
System.out.println("mapper查询");
return songMapper.listbyName(name);
}
}
controller:
@ApiOperation(value="查询歌曲")
@RequestMapping(value = "/query", method = RequestMethod.POST)
public ResponseResult querySong(
@ApiParam(name="name",value="歌名",required=true)
@RequestParam("name")String name)
{
// List songs = songService.listbyName(new LambdaQueryWrapper().eq(Song::getName,name));
List songs = songService.listbyName(name);
if(songs == null) return ResponseResult.error(ResponseEnum.DIARY_NOT_EXIST);
List songVos = songs.stream().map(e->{
SongVo songVo = new SongVo();
BeanUtils.copyBeanProp(songVo,e);
return songVo;
}).collect(Collectors.toList());
int numbers = songVos.size();
PageResult pageResult = new PageResult<>(songVos,numbers);
return ResponseResult.success(pageResult);
}
mapper
select * from t_song where name like "%"#{name}"%"
3.结果:
多次发起请求只打印了一次sql,可以说明后面几次是从缓存中取出的
查看redis数据库: