not-to-save-null-in-spring-redis

#spring #tip

Spring Redis Cache ์‚ฌ์šฉ์—์„œ null์„ ์บ์‹ฑํ•˜์ง€ ์•Š๋Š” ๋ฒ•

1. RedisCacheManager

  • ์„ค์ •ํ•  ๋•Œ disableCachingNullValues() ์ถ”๊ฐ€

  • ์ „์—ญ์ ์œผ๋กœ null ์„ ์บ์‹ฑํ•˜์ง€ ์•Š๋„๋ก ํ•จ

  • ๋‹จ ์‚ฌ์šฉ์‹œ, ์บ์‹œ์— null ๋‹ด๊ธฐ๋ ค ํ•˜๋ฉด ์—๋Ÿฌ ๋ฐœ์ƒํ•จ.

2. unless = "#result == null"

  • @Cacheable(value="defaultCache", key="#pk", unless="#result == null") ์™€ ๊ฐ™์ด, ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜์— unless = "#result == null" ์„ ๋ช…์‹œ.

  • ๊ทธ๋Ÿฌ๋ฉด null ์€ ์บ์‹ฑํ•˜์ง€ ์•Š๊ฒŒ ๋จ

  • ์—๋Ÿฌ ๋ฐ˜ํ™˜๋„ ์—†๊ณ .

  • ์ดํ•ดํ•˜์ž๋ฉด, result ๊ฐ€ null์ด ์•„๋‹ˆ๋ผ๋ฉด(unless) ์บ์‹ฑํ•œ๋‹ค๋Š” ์˜๋ฏธ์ธ๋“ฏ.

    • unless ๋ผ๋Š” ์ด๋ฆ„์ด ๋ถ™์œผ๋ฉด, ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋œ ์ด์œ ์— ํŒ๋‹จ๋œ๋‹ค.

    • ๋‹ค์‹œ ๋งํ•ด, cache์— ๊ฐ’์ด ์žˆ์œผ๋ฉด ๊ทธ ๊ฐ’์ด ๋ฆฌํ„ด๋˜๊ณ , ์บ์‹œ์— ๊ฐ’์ด ์—†๊ณ , ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜์—ˆ๋Š”๋ฐ ๊ทธ ๊ฐ’์ด null ์ด๋ผ๋ฉด?

    • ํ•ด๋‹น ํŒจํ„ด์— ์˜ํ•ด์„œ ์บ์‹ฑ์ด ๋˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ์ดํ•ด๋จ.

์ฐธ๊ณ 

Last updated