<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
yaml
spring:
cache:
type: simple
@Service
public class MyService {
@Cacheable("myCache")
public String getCachedValue(String key) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Cached Value";
}
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringCacheExampleApplicationTests {
@Autowired
private MyService myService;
@Test
public void testCache() {
String result1 = myService.getCachedValue("key");
String result2 = myService.getCachedValue("key");
assertEquals(result1, result2);
}
}