diff --git a/src/main/java/org/springframework/samples/petclinic/cache/CacheConfig.java b/src/main/java/org/springframework/samples/petclinic/cache/CacheConfig.java new file mode 100644 index 000000000..f5e22317e --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/cache/CacheConfig.java @@ -0,0 +1,19 @@ +package org.springframework.samples.petclinic.cache; + +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.cache.concurrent.ConcurrentMapCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableCaching +public class CacheConfig { + + @Bean + public CacheManager cacheManager() { + ConcurrentMapCacheManager cacheManager = new ConcurrentMapCacheManager(); + cacheManager.setCacheNames(java.util.Arrays.asList("dataCache")); + return cacheManager; + } +} \ No newline at end of file diff --git a/src/main/java/org/springframework/samples/petclinic/cache/CacheableService.java b/src/main/java/org/springframework/samples/petclinic/cache/CacheableService.java new file mode 100644 index 000000000..d91276a11 --- /dev/null +++ b/src/main/java/org/springframework/samples/petclinic/cache/CacheableService.java @@ -0,0 +1,36 @@ +package org.springframework.samples.petclinic.cache; + +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +@Service +public class CacheableService { + + private final Map dataStore = new HashMap<>(); + + public CacheableService() { + // Инициализация тестовых данных + dataStore.put("key1", "value1"); + dataStore.put("key2", "value2"); + dataStore.put("key3", "value3"); + } + + @Cacheable(value = "dataCache", key = "#key") + public String getData(String key) { + // Имитация задержки при получении данных + try { + TimeUnit.MILLISECONDS.sleep(100); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + return dataStore.get(key); + } + + public void updateData(String key, String value) { + dataStore.put(key, value); + } +} \ No newline at end of file diff --git a/src/test/java/org/springframework/samples/petclinic/cache/CacheableServiceTest.java b/src/test/java/org/springframework/samples/petclinic/cache/CacheableServiceTest.java new file mode 100644 index 000000000..5e537a8bc --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/cache/CacheableServiceTest.java @@ -0,0 +1,55 @@ +package org.springframework.samples.petclinic.cache; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cache.CacheManager; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +public class CacheableServiceTest { + + @Autowired + private CacheableService cacheableService; + + @Autowired + private CacheManager cacheManager; + + @Test + public void testCacheHit() { + // Первый вызов - кэш промах + long startTime1 = System.currentTimeMillis(); + String result1 = cacheableService.getData("key1"); + long duration1 = System.currentTimeMillis() - startTime1; + + // Второй вызов - должен быть кэш хит + long startTime2 = System.currentTimeMillis(); + String result2 = cacheableService.getData("key1"); + long duration2 = System.currentTimeMillis() - startTime2; + + // Проверяем результаты + assertEquals("value1", result1); + assertEquals("value1", result2); + + // Второй вызов должен быть быстрее из-за кэширования + assertTrue(duration2 < duration1); + } + + @Test + public void testCacheMiss() { + // Проверяем несуществующий ключ + String result = cacheableService.getData("nonExistentKey"); + assertNull(result); + } + + @Test + public void testCacheUpdate() { + // Обновляем значение + cacheableService.updateData("key1", "newValue"); + + // Получаем обновленное значение + String result = cacheableService.getData("key1"); + assertEquals("newValue", result); + } +} \ No newline at end of file