1 ๋ถ„ ์†Œ์š”

์บ์‹œ(Cache)๋ž€?

์บ์‹œ(Cache)๋ž€ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฏธ๋ฆฌ ๋ณด๊ด€ํ•ด๋‘” ์ž„์‹œ ์žฅ์†Œ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
์บ์‹œ๋Š” ๋น„๊ต์  ์ €์žฅ ๊ณต๊ฐ„์ด ์ ๊ณ , ์ „์ฒด์ ์ธ ๋น„์šฉ์ด ๋น„์‹ธ๋‹ค.
์—ฌ๊ธฐ์„œ ๋น„์šฉ์„ ๋น„๊ตํ•˜๋Š” ๋Œ€์ƒ์€ ์ผ๋ฐ˜์ ์œผ๋กœ ํ•˜๋“œ๋””์Šคํฌ์™€ ๊ฐ™์€ ์ €์žฅ์†Œ๊ฐ€ ๋˜๊ฒ ๋‹ค.
๋Œ€์‹  ๋น ๋ฅธ IO๋ฅผ ํ†ตํ•ด ์„ฑ๋Šฅ์  ์ด์ ์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

์ผ๋ฐ˜์ ์œผ๋กœ ์•„๋ž˜์™€ ๊ฐ™์€ ํŠน์ง•์„ ๊ฐ–๋Š” ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์บ์‹œ๋ฅผ ์ ์šฉํ•˜๋ฉด ์ข‹๋‹ค.

  1. ๋„์ค‘์— ๋ณ€๊ฒฝ๋  ์ผ์ด ์—†๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์กฐํšŒ ๊ฐ’
  2. ์ž์ฃผ ํ˜ธ์ถœ๋˜๋Š” ๋ฐ์ดํ„ฐ

์Šคํ”„๋ง๋ถ€ํŠธ์—์„œ์˜ ์บ์‹œ

์Šคํ”„๋ง๋ถ€ํŠธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์บ์‹œ๋Š” ๋Œ€๋ถ€๋ถ„ JSR-107์„ ๋”ฐ๋ฅธ๋‹ค.
์ด๋ฅผ ๋”ฐ๋ฅด๋Š” ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์–ด๋–ค ๊ตฌํ˜„์ฒด ์บ์‹œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”์ง€์— ๊ด€๊ณ„์—†์ด ์ถ”์ƒํ™”๋ฅผ ์ง€์›ํ•œ๋‹ค.

JSR

  • Java Specification Requests์˜ ์ค„์ž„๋ง, ์ž๋ฐ” ํ”Œ๋žซํผ์— ๋Œ€ํ•œ ๊ทœ๊ฒฉ์„ ์ œ์•ˆํ•˜๊ฑฐ๋‚˜ ๊ธฐ์ˆ ํ•œ ๊ฒƒ์„ ์˜๋ฏธ
  • ๊ทธ ์ค‘ JSR-107์€ JCACHE(Java Temporary Caching API)์— ๊ด€ํ•œ ๋‚ด์šฉ

๋กœ์ปฌ ์บ์‹œ

๋กœ์ปฌ ์บ์‹œ๋ž€ ๋กœ์ปฌ์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๋Š” ์บ์‹œ๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
์™ธ๋ถ€ ์„œ๋ฒ„์™€ ํŠธ๋žœ์žญ์…˜ ๋น„์šฉ์ด ๋“ค์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.
ํ•˜์ง€๋งŒ ๋กœ์ปฌ์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ถ„์‚ฐ ์„œ๋ฒ„์˜ ๊ตฌ์กฐ์—์„œ ์บ์‹œ๋ฅผ ๊ณต์œ ํ•˜๊ธฐ ์–ด๋ ต๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ๋‹ค.

๊ธ€๋กœ๋ฒŒ ์บ์‹œ

๊ธ€๋กœ๋ฒŒ ์บ์‹œ๋ž€, ์—ฌ๋Ÿฌ ์„œ๋ฒ„์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์บ์‹œ ์„œ๋ฒ„๋ฅผ ๊ตฌ์ถ•ํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์„ ๋œปํ•œ๋‹ค.
๋„คํŠธ์›Œํฌ๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ํŠธ๋žœ์žญ์…˜ ๋น„์šฉ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋กœ์ปฌ ์บ์‹œ์— ๋น„ํ•ด ์ƒ๋Œ€์ ์œผ๋กœ ๋Š๋ฆฐ ํŽธ์ด๋‹ค.
ํ•˜์ง€๋งŒ ๋ณ„๋„์˜ ์„œ๋ฒ„๋กœ ์šด์˜๋˜๊ธฐ ๋•Œ๋ฌธ์— ์„œ๋ฒ„ ๊ฐ„ ๋ฐ์ดํ„ฐ ๊ณต์œ ์— ์šฉ์ดํ•˜๋‹ค.

Redis

Redis(Remote Dictionary Server)์˜ ์•ฝ์ž๋กœ ํ‚ค-๊ฐ’ ๊ตฌ์กฐ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์˜คํ”ˆ์†Œ์Šค ๊ธฐ๋ฐ˜์˜ ๋น„๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ ์‹œ์Šคํ…œ์ด๋‹ค.

๊ณต์‹ ์‚ฌ์ดํŠธ: https://redis.io/

Redis์˜ ํŠน์ง• 1: In-memory data structures

Redis์˜ ํŠน์ง• 2: Persistence

Redis์˜ ํŠน์ง• 3: Extensibility(ํ™•์žฅ์„ฑ)

Redis์˜ ํŠน์ง• 4: Clustering(๋™๊ธฐํ™” ์ฒ˜๋ฆฌ)

Redis์˜ ํŠน์ง• 5: High availability



SpringBoot & Redis

์˜์กด์„ฑ ์ถ”๊ฐ€

pom.xml ํ˜น์€ build.gradle์— redis์˜ ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•ด์ฃผ์ž.
์—ฌ๊ธฐ์„œ ์ฐพ์•„๋ณด์ž > https://mvnrepository.com/

Java์—์„œ ์‚ฌ์šฉํ•˜๋Š” Redis Client๋Š” ํฌ๊ฒŒ 2๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

  1. Jedis
  2. Lettuce

์Šคํ”„๋ง๋ถ€ํŠธ 2.0 ์ดํ›„๋กœ๋Š” ๊ธฐ๋ณธ ํด๋ผ์ด์–ธํŠธ๋กœ Lettuce๊ฐ€ ์‚ฌ์šฉ๋œ๋‹ค๊ณ  ํ•œ๋‹ค. (Jedis๋Š” ๋ช‡๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•จ)

application.properties

application.propertiesํŒŒ์ผ์— ์•„๋ž˜์™€ ๊ฐ™์€ ์„ค์ •์„ ์ถ”๊ฐ€ํ•ด์ฃผ์ž.

## redis ์„ค์ •
spring.redis.host=localhost
spring.redis.port=6379

Ehcache ์„ค์ •

RedisConfig ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์ž

@Configuration
public class RedisCacheConfig implements CacheConfig {

    @Value("localhost")
    private String host;

    @Value("6379")
    private int port;

    @Bean
    public RedisConnectionFactory redisConnectFactory() {
        return new LettuceConnectionFactory(host, port);
    }

}

redis์—์„œ ์‚ฌ์šฉํ•  ๊ฐ์ฒด ์„ค์ •(Optional)

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Builder
@RedisHash(value = "shortUrl", timeToLive = 60)
public class ShortUrlResopnseDto implements Serializable {

    private static final long serialVersionUID = -214490344996507077L;

    @Id
    private String orgUrl;

    private String shortUrl;
}

RedisRepository ์„ค์ •

public interface ShortUrlRedisRepository extends CrudRepository<ShortUrlResponseDto, String> {
    
}

์ฐธ๊ณ ์ž๋ฃŒ

https://www.youtube.com/watch?v=4YDVww8fXVA&t=110s

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ