복붙노트

[SPRING] Spring 캐시 가능 대 CachePut?

SPRING

Spring 캐시 가능 대 CachePut?

@CachePut or @Cacheable(value = "CustomerCache", key = "#id")
public Customer updateCustomer(Customer customer) {
   sysout("i am inside updateCustomer");
    ....
    return customer;
}

CachePut 소스 코드 아래에있는 문서를 찾았습니다.

@Cacheable을 사용하면 updateCustomer 메서드가 한 번만 실행되고 결과는 캐시에서 업데이트됩니다. 후속 통화 : updateCustomer는 updateCustomer를 실행하지 않고 캐시를 업데이트합니다.

@CachePut의 경우 각 호출에서 updateCustomer 메서드가 실행되고 결과는 캐시에서 업데이트됩니다.

내 이해가 맞습니까?

해결법

  1. ==============================

    1.예.

    예.

    나는 확실한 시험을했다.

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = CacheableTest.CacheConfigurations.class)
    public class CacheableTest {
    
        public static class Customer {
    
            final private String id;
            final private String name;
    
            public Customer(String id, String name) {
                this.id = id;
                this.name = name;
            }
    
            public String getId() {
                return id;
            }
    
            public String getName() {
                return name;
            }
    
        }
    
        final public static AtomicInteger cacheableCalled = new AtomicInteger(0);
        final public static AtomicInteger cachePutCalled = new AtomicInteger(0);
    
        public static class CustomerCachedService {
    
    
            @Cacheable("CustomerCache")
            public Customer cacheable(String v) {
                cacheableCalled.incrementAndGet();
                return new Customer(v, "Cacheable " + v);
            }
    
            @CachePut("CustomerCache")
            public Customer cachePut(String b) {
                cachePutCalled.incrementAndGet();
                return new Customer(b, "Cache put " + b);
            }
    
        }
    
        @Configuration
        @EnableCaching()
        public static class CacheConfigurations {
    
            @Bean
            public CustomerCachedService customerCachedService() {
                return new CustomerCachedService();
            }
    
            @Bean
            public CacheManager cacheManager() {
                return new GuavaCacheManager("CustomerCache");
            }
    
        }
    
        @Autowired
        public CustomerCachedService cachedService;
    
        @Test
        public void testCacheable() {
            for(int i = 0; i < 1000; i++) {
                cachedService.cacheable("A");
            }
            Assert.assertEquals(cacheableCalled.get(), 1);
        }
    
        @Test
        public void testCachePut() {
            for(int i = 0; i < 1000; i++) {
                cachedService.cachePut("B");
            }
            Assert.assertEquals(cachePutCalled.get(), 1000);
        }
    
    }
    
  2. ==============================

    2.@CachePut은 항상 메소드가 실행되도록합니다. 메소드 실행 결과로 캐시를 업데이트하려는 경우 일반적으로 사용됩니다. 예 : 캐시를 완전히 끊는 대신 캐시 된 오래된 데이터를 업데이트하려는 경우.

    @CachePut은 항상 메소드가 실행되도록합니다. 메소드 실행 결과로 캐시를 업데이트하려는 경우 일반적으로 사용됩니다. 예 : 캐시를 완전히 끊는 대신 캐시 된 오래된 데이터를 업데이트하려는 경우.

    @Cacheable은 주어진 캐시 키에 대해 한 번만 실행되며 이후 요청은 캐시가 만료되거나 플러시 될 때까지 메소드를 실행하지 않습니다.

  3. from https://stackoverflow.com/questions/28516398/spring-cacheable-vs-cacheput by cc-by-sa and MIT license