반응형
Recent Posts
Recent Comments
관리 메뉴

개발잡부

[java] API method cache - 적용 본문

JAVA/springboot

[java] API method cache - 적용

닉의네임 2023. 10. 31. 13:40
반응형

Concept

실제 서비스에 적용될 api cache 의 구성 

동적인 데이터를 처리하는 메소드와 정적인 데이터를 처리하는 메소드를 구분하여 캐싱 구성 

 

 

 

 

Detail 

Data component 데이터를 조회 / 후처리 까지 처리하는 메소드 Data component 단위로 캐싱 

 

 

 

application.yml

더보기
spring:
  profiles: local
  redis:
    timeout: 1000
    lettuce:
      pool:
        max-active: 3
        max-idle: 3
        min-idle: 3
    password: password
  elasticache:
    master:
      - name: itemList
        redisMaster:
          host: 127.0.0.1
          port: port
      - name: itemSpecial
        redisMaster:
          host: 127.0.0.1
          port: port
      - name: common
        redisMaster:
          host: 127.0.0.1
          port: port
    slave:
      - name: itemList
        redisSlave:
          - host: 127.0.0.1
            port: port
      - name: itemSpecial
        redisSlave:
          - host: 127.0.0.1
            port: port
      - name: common
        redisSlave:
          - host: 127.0.0.1
            port: port

 

ElasticCacheProperties

더보기
package kr.co.homeplus.totalsearch.core.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@ConfigurationProperties(prefix = "spring.elasticache")
public class ElasticacheProperties {

    private Map<String, RedisInstance> masterMap = new HashMap<>();
    private Map<String, List<RedisInstance>> slaveMap = new HashMap<>();

    public  RedisInstance getMaster(String clusterName) {
        return this.masterMap.get(clusterName);
    }
    public void setMaster(List<RedisInfo> master) {
        for(RedisInfo redisMaster : master){
            masterMap.put( redisMaster.getName(), redisMaster.getRedisMaster());
        }
    }

    public List<RedisInstance> getSlave(String clusterName) {
        return slaveMap.get(clusterName);
    }

    public void setSlave(List<RedisInfo> slave) {
        for(RedisInfo redisSlave : slave){
            slaveMap.put( redisSlave.getName(), redisSlave.getRedisSlave());
        }
    }

    public static class RedisInfo {

        private String name;
        private List<RedisInstance> redisSlave;
        private RedisInstance redisMaster;

        public void setName(String name) { this.name = name; }
        public String getName(){ return this.name; }

        public void setRedisSlave(List<RedisInstance> redisSlave) { this.redisSlave = redisSlave; }
        public List<RedisInstance> getRedisSlave(){ return this.redisSlave; }

        public void setRedisMaster( RedisInstance redisMaster) { this.redisMaster = redisMaster; }
        public RedisInstance getRedisMaster(){ return this.redisMaster; }

    }
    public static class RedisInstance {
        private String host;
        private int port;

        public String getHost() {
            return this.host;
        }

        public void setHost(String host) {
            this.host = host;
        }

        public int getPort() {
            return this.port;
        }

        public void setPort(int port) {
            this.port = port;
        }
    }

}

 

RedisConfig

더보기
package kr.co.homeplus.totalsearch.core.config;


import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.CacheKeyPrefix;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.session.data.redis.config.ConfigureRedisAction;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;


@RequiredArgsConstructor
@EnableCaching
@Configuration
public class RedisConfig {

    @Bean
    public ConfigureRedisAction configureRedisAction() {
        return ConfigureRedisAction.NO_OP;
    }

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setPort(port int);
        redisStandaloneConfiguration.setHostName("127.0.0.1");
//        redisStandaloneConfiguration.setUsername("common");
        redisStandaloneConfiguration.setPassword("password");

        LettuceConnectionFactory lettuceConnectionFactory =
                new LettuceConnectionFactory(redisStandaloneConfiguration);

        return lettuceConnectionFactory;
    }


    @Bean(name = "cacheManager")
    public RedisCacheManager cacheManager() {

        RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig()
                .disableCachingNullValues()
                .entryTtl(Duration.ofSeconds(CacheKey.DEFAULT_EXPIRE_SEC))
                .computePrefixWith(CacheKeyPrefix.simple())
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));

        Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<>();
        cacheConfigurations.put(CacheKey.LOCATION, RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofSeconds(CacheKey.DEFAULT_EXPIRE_SEC)));

        return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory()).cacheDefaults(configuration)
                .withInitialCacheConfigurations(cacheConfigurations).build();
    }
}

 

반응형

'JAVA > springboot' 카테고리의 다른 글

[Redis] API 성능개선 ( redis 캐시 적용하기 ) - 실전  (0) 2023.12.19
[java] API method cache  (1) 2023.10.29
[java] API - 검색 api 성능 개선 final  (2) 2023.10.15
[java] API - cache method  (0) 2023.10.15
[java] API - redis cache  (2) 2023.10.09
Comments