Cache - hyunwoo-e/Spring GitHub Wiki

์บ์‹œ ๋™์ž‘ ๋ฐฉ์‹

  • ์‘๋‹ต ํ—ค๋”์˜ cache-control์— no-store ์˜ต์…˜์„ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ ์บ์‹œ๋˜์ง€ ์•Š์Œ
  • ์บ์‹œ๋œ ๋ฐ์ดํ„ฐ์˜ ๋งŒ๋ฃŒ์ผ์ž(expire)/๋งŒ๋ฃŒ์‹œ๊ฐ„(max-age)๊ฐ€ ๋‚จ์€ ๊ฒฝ์šฐ ์ตœ์‹ ์ƒํƒœ๋กœ ๊ฐ„์ฃผ
  • ์บ์‹œ๊ฐ€ ์ตœ์‹ ์ด ์•„๋‹ ๊ฒฝ์šฐ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ์š”์ฒญ, 304 ๋ฆฌํ„ด์‹œ ๋งŒ๋ฃŒ์ผ์ž/์‹œ๊ฐ„์„ ์—…๋ฐ์ดํŠธ

cache-control: no-store vs no-cache

์‘๋‹ต ํ—ค๋”์˜ ์บ์‹œ ์ง€์‹œ์ž๋Š” cache-control์„ ์‚ฌ์šฉ(pragma, expires๋Š” ํ˜ธํ™˜, ์‹ฑํฌ ๋ฌธ์ œ๊ฐ€ ์žˆ์Œ)

  • no-store: ์š”์ฒญ์ด๋‚˜ ์‘๋‹ต์„ ์ €์žฅํ•˜์ง€ ์•Š์Œ
  • no-cache
    • Request - ํ”„๋ก์‹œ๋ฅผ ๊ฑฐ์ณ ์‹ค ์„œ๋ฒ„๊นŒ์ง€ ๋„๋‹ฌํ•˜๊ฒŒ ํ•จ
    • Response - ์บ์‹œํ•˜๊ณ  ์žˆ์œผ๋‚˜ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๋ฅผ ๊ฐ•์ œํ•จ

์ •์  ๋ฐ์ดํ„ฐ

์ •์ ๋ฐ์ดํ„ฐ(js, css, image ๋“ฑ)์€ ์‘๋‹ต ํ—ค๋”์— Last-Modified๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด ์บ์‹œํ•  ์ˆ˜ ์žˆ์Œ

  • Apache์—์„œ ์บ์‹œํ•  ์ˆ˜ ์žˆ์Œ
  • Spring์—์„œ ์บ์‹œํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Servlet Context ํŒŒ์ผ์— mvc:resources๋ฅผ ํ†ตํ•ด ์ง€์ •
<mvc:resources mapping="/js/**" location="/js/" cache-period="86400"/>
<mvc:resources mapping="/style/**" location="/style/" cache-period="86400"/>
<mvc:resources mapping="/image/**" location="/image/" cache-period="31556926"/>
  • cache-period
    • Specifies the cache period for the resources served by this resource handler, in seconds.
    • The default is to not send any cache headers but rather to rely on last-modified timestamps only.
    • Set this to 0 in order to send cache headers that prevent caching, or to a positive number of seconds in order to send cache headers with the given max-age value.

๋™์  ๋ฐ์ดํ„ฐ

PHP, ASP, JSP๋“ฑ ๋™์ ์œผ๋กœ ์ƒ์„ฑ๋œ ์ปจํ…์ธ ๋Š” Last-Modified, Cache-Control ๋“ฑ์˜ ์ •๋ณด๊ฐ€ ์—†์–ด ์†์‰ฝ๊ฒŒ ์บ์‹œํ•  ์ˆ˜ ์—†์Œ

์Šคํ”„๋ง์—์„œ ์บ์‹œ ์ œ์–ด

์Šคํ”„๋ง์—์„œ ์บ์‹œ๋ฅผ ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Servlet Context ํŒŒ์ผ์— WebContentInterceptor๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค

  • /api ์ดํ•˜ cache, ์‚ฌ์ง„์€ ์˜ˆ์™ธ์ ์œผ๋กœ 24์‹œ๊ฐ„ ๋™์•ˆ cache
<mvc:interceptors>
     <mvc:interceptor>
          <mvc:mapping path="/api/**/*"/>
          <bean class="org.springframework.web.servlet.mvc.WebContentInterceptor">
               <property name="cacheSeconds" value="0"/>
               <property name="cacheMappings">
                   <props>
                      <prop key="/api/photo/**">86400</prop>
                   </props>
               </property>
           </bean>
     </mvc:interceptor>
</mvc-interceptors>

์ฐธ๊ณ 

๋ณ„๋„

โš ๏ธ **GitHub.com Fallback** โš ๏ธ