1. SpringBoot 自带的缓存
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
@EnableCaching
public class SpringbootStudyApplication {
@Service
@CacheConfig(cacheNames = "studentsCache")
public class StudentService {
private Logger logger = LoggerFactory.getLogger(this.getClass());
@Cacheable
public Student queryById(Integer id) {
logger.error(LocalDateTime.now()+"queryById("+id+")");
Student muzi = new Student();
muzi.setId(1);
muzi.setName("木子");
return muzi;
}
@CacheEvict(key = "#id")
public void deleteById(Integer id) {
logger.warn(LocalDateTime.now()+"deleteById("+id+")");
}
@CachePut(key = "#student.id")
public Student editById(Student student) {
logger.warn(LocalDateTime.now()+"editById()");
student.setName("木子2");
return student;
}
}
2. 整合Redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring.redis.port=6379
spring.redis.host=172.16.100.98
spring.redis.password=softeem
@SpringBootTest
class SpringbootStudyApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void testRedis(){
redisTemplate.opsForValue().set("muzi6","666");
String muzi6 = (String)redisTemplate.opsForValue().get("muzi6");
System.out.println(muzi6);
redisTemplate.delete("muzi6");
}
}