Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- ์ฝ๋ฉ์ ํ
- ๋๋ฆผ์ฝ๋ฉ
- ๊ตญ๋น์ง์
- node.js
- ๊ฐ๋ฐ
- ์ฝ๋ฉํ ์คํธ
- react
- ํฌ๋กค๋ง
- ํ๋ก์ ํธ
- HTML
- ๋ชจ๊ฐ์ฝ
- ํ์ด์ฌ
- CSS
- ํ ์ดํ๋ก์ ํธ
- Til
- heapq
- ์ฝ๋ฉ
- mongodb
- ๋ ธ๋ง๋์ฝ๋
- ์๊ณ ๋ฆฌ์ฆ
- Python
- error
- javascript
- KDT
- ํ๋ก ํธ์๋
- ๊ทธ๋ฆฌ๋
- ๋ฐฑ์ค
- JS
- fe
- ํ๋ก๊ทธ๋๋จธ์ค
Archives
- Today
- Total
๐ฑ โ ๐ณ
Spring์์ Redis๋ก ์บ์ฑ์ฒ๋ฆฌ ๋ณธ๋ฌธ
728x90
- UT๋ฅผ ํตํด ๋ฐ์ดํฐ ๋ฒ ์ด์ค์ ์์ฒญ์ด ๊ฐ์ฅ ๋ง์ด ๋ค์ด์ค๋ Service์ ๋ฐ์ดํฐ๋ฅผ ๋น ๋ฅด๊ฒ ์กฐํํ ์ ์๋๋ก Server side caching์ ์ ์ฉํ์ฌ ์ฑ๋ฅ์ ํฅ์ ์์ผฐ์ต๋๋ค.
- @Cacheable์ ์ฌ์ฉํด์ ์๋น์ค์์ ๊ฐ์ฅ ์กฐํ๊ฐ ๋ง์ด ์๊ธฐ๋ ๊ณต๊ณ ์กฐํ ๋ก์ง์ Redis๋ฅผ ์ด์ฉํ์ฌ ์บ์ฑ์ฒ๋ฆฌ
// ๋ ๋์ค ์บ์
@Bean
public RedisCacheManager redisCacheManager() {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration
.defaultCacheConfig()
.disableCachingNullValues()
.serializeValuesWith(
RedisSerializationContext.SerializationPair
.fromSerializer(new GenericJackson2JsonRedisSerializer(objectMapper))
);
return RedisCacheManager.RedisCacheManagerBuilder
.fromConnectionFactory(redisConnectionFactory())
.cacheDefaults(redisCacheConfiguration)
.build();
}
// domain.study.service.StudySearchService
// ์บ์ ํค ์์ฑ
public String generateCacheKey(String keyword, int pageIndex, int pageCount, String sortedType, UserDetailsImpl userDetails, CompanyDetailsImpl companyDetails) {
String userId = userDetails != null ? String.valueOf(userDetails.getUser().getId()) : "null";
String companyId = companyDetails != null ? String.valueOf(companyDetails.getCompany().getId()) : "null";
return "CustomKey [" + keyword + "," + pageIndex + "," + pageCount + "," + sortedType + "," + userId + "," + companyId + "]";
}

์บ์ฑ ์ ์ฉ ์


์บ์ฑ ์ ์ฉ ํ


RedisTemplate (low level)
RedisTemplate์ Redis์ ์ํธ์์ฉํ๋๋ก ๋์ ์์ค์ผ๋ก ์ถ์ํ๋ฅผ ์ ๊ณตํ๋ ํด๋์ค๋ก ๋ค์ํ Redis ์ฐ์ฐ, ์์ธ ๋ณํ, ํธ๋์ญ์ , ์ง๋ ฌํ ์ปค์คํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
- Redis ํธ๋์ญ์ ๊ธฐ๋ฅ: Redis๋ ์ฑ๊ธ ์ฐ๋ ๋, Atomicํ ์๋ฃ๊ตฌ์กฐ๋ก RaceCondition์ ํผํ ์ ์๋ค. Redis ํธ๋์ญ์ ๊ธฐ๋ฅ ์ข ๋ ํฐ ๋จ์๋ก ๊ทธ๋ฌํ Atomicํ ์ฌ๋ฌ ๋ช ๋ น์ด๋ค์ ํ ๋ฌถ์์ผ๋ก ๋ฌถ์ด์ฃผ๋ ๊ธฐ๋ฅ์ด๋ค. MULTI โ commands โ EXEC/DISCARD
Redis Config
ConnectionFactory๋ lettuce๋ก ์ค์ ํ ๋ค์ RedisTemplate ๋น์ ๋ฑ๋กํด์ค๋ค.
Serializer
- JdkSerializationRedisSerializer: ๋ํดํธ๋ก ๋ฑ๋ก๋์ด์๋ Serializer์ด๋ค.
- StringRedisSerializer: String ๊ฐ์ ์ ์์ ์ผ๋ก ์ฝ์ด์ ์ ์ฅํ๋ค. ๊ทธ๋ฌ๋ ์ํฐํฐ๋ VO๊ฐ์ ํ์ ์ cast ํ ์ ์๋ค.
- Jackson2JsonRedisSerializer(classType.class): classType ๊ฐ์ json ํํ๋ก ์ ์ฅํ๋ค. ํน์ ํด๋์ค(classType)์๊ฒ๋ง ์ง์๋์ด์๋ค๋ ๋จ์ ์ด ์๋ค.
- GenericJackson2JsonRedisSerializer: ๋ชจ๋ classType์ json ํํ๋ก ์ ์ฅํ ์ ์๋ ๋ฒ์ฉ์ ์ธ Jackson2JsonRedisSerializer์ด๋ค. ์บ์ฑ์ ํด๋์ค ํ์ ๋ ์ ์ฅ๋๋ค๋ ๋จ์ ์ด ์์ง๋ง RedisTemplate์ ์ด์ฉํด ๋ค์ํ ํ์ ๊ฐ์ฒด๋ฅผ ์บ์ฑํ ๋ ์ฌ์ฉํ๊ธฐ์ ์ข๋ค

728x90
'Server > Java & Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ(OOP) (0) | 2023.05.31 |
---|---|
[Spring boot] properties ์ฌ์ฉ์ "Could not resolve placeholder" ์ค๋ฅ ๋ฐ์ (2) | 2023.05.28 |
[Spring] save() vs saveAndFlush() (0) | 2023.03.22 |
[Spring] IoC, DI๋? (0) | 2023.03.22 |
[Spring] ์์กด์ฑ ์ฃผ์ (Dependency Injection)์์ final ํค์๋๋ฅผ ์ฌ์ฉํ๋ ์ด์ (0) | 2023.03.22 |