{"id":1571,"date":"2021-01-10T15:47:41","date_gmt":"2021-01-10T06:47:41","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=1571"},"modified":"2021-01-10T15:47:41","modified_gmt":"2021-01-10T06:47:41","slug":"spring-boot-redis-entity-%eb%b3%84%eb%a1%9c-ttl-%ec%a7%80%ec%a0%95%ed%95%98%ea%b8%b0","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=1571","title":{"rendered":"Spring Boot Redis Entity \ubcc4\ub85c TTL \uc9c0\uc815\ud558\uae30"},"content":{"rendered":"<h1>Spring Boot Redis Entity \ubcc4\ub85c TTL \uc9c0\uc815\ud558\uae30<\/h1>\n<h2>\uc758\uc874\uc131 \ucd94\uac00<\/h2>\n<p>\uc544\ub798 \uc124\uc815\uc73c\ub85c \uc124\uc815\uc774 \ub05d\ub0a9\ub2c8\ub2e4.<\/p>\n<p>Redis Java Client \ub294 <code>Lettuce<\/code> \uac00 \uc790\ub3d9\uc73c\ub85c \ucd94\uac00\ub429\ub2c8\ub2e4.<\/p>\n<p>build.gradle<\/p>\n<pre><code class=\"language-groovy\">dependencies {\n    implementation &#039;org.springframework.boot:spring-boot-starter-data-redis&#039;\n}<\/code><\/pre>\n<h2>EnableRedisRepositories \uc0dd\uc131<\/h2>\n<p><code>RedisCacheManager<\/code> \uc5d0\uc11c Entity \ub9c8\ub2e4\uc758 TTL \uc744 \uc124\uc815\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-java\">@RequiredArgsConstructor\n@Configuration\n@Slf4j\n@EnableRedisRepositories\npublic class RedisRepositoryConfig {\n\n    private final RedisProperties redisProperties;\n\n    private final int DEFAULT_EXPIRE_SECONDS = 1;\n\n    private final String ApiAccessInfo = &quot;ApiAccessInfo&quot;;\n    private final int API_ACCESS_INFO_EXPIRE_SECONDS = 600;\n\n    private final String Menus = &quot;Menus&quot;;\n    private final int MENUS_EXPIRE_SECONDS = 3600;\n\n    @Bean(name = &quot;cacheManager&quot;)\n    public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) {\n        RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig()\n                .disableCachingNullValues()\n                .entryTtl(Duration.ofSeconds(DEFAULT_EXPIRE_SECONDS))\n                .computePrefixWith(CacheKeyPrefix.simple())\n                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));\n\n        Map&lt;String, RedisCacheConfiguration&gt; cacheConfigurations = new HashMap&lt;&gt;();\n\n        \/\/ ApiAccessInfo\n        cacheConfigurations.put(ApiAccessInfo, RedisCacheConfiguration.defaultCacheConfig()\n                .entryTtl(Duration.ofSeconds(API_ACCESS_INFO_EXPIRE_SECONDS)));\n\n        \/\/ Menus\n        cacheConfigurations.put(Menus, RedisCacheConfiguration.defaultCacheConfig()\n                .entryTtl(Duration.ofSeconds(MENUS_EXPIRE_SECONDS)));\n\n        return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(connectionFactory).cacheDefaults(configuration)\n                .withInitialCacheConfigurations(cacheConfigurations).build();\n    }\n\n    @Bean\n    public RedisConnectionFactory redisConnectionFactory() {\n        return new LettuceConnectionFactory(redisProperties.getHost(), redisProperties.getPort());\n    }\n\n    @Bean\n    public RedisTemplate&lt;?, ?&gt; redisTemplate() {\n        RedisTemplate&lt;byte[], byte[]&gt; redisTemplate = new RedisTemplate&lt;&gt;();\n        redisTemplate.setConnectionFactory(redisConnectionFactory());\n        return redisTemplate;\n    }\n}<\/code><\/pre>\n<h2>Cacheable \ucd94\uac00<\/h2>\n<p>Cacheable \uc744 \ucd94\uac00\ud574 \uc90c\uc73c\ub85c \ud574\uc11c TTL \uc774 10\ubd84\uc73c\ub85c \uc9c0\uc815\ub429\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-java\">@RequiredArgsConstructor\n@Service\npublic class ApiAccessInfoService {\n\n    private final ApiAccessInfoRepository apiAccessInfoRepository;\n\n    @Transactional(readOnly = true)\n    @Cacheable(value=&quot;ApiAccessInfo&quot;)\n    public ApiAccessInfoResponseDto findByAccessIdAndAccessKeyAndAccessIp(String accessId, String accessKey, String accessIp) {\n\n        ApiAccessInfo apiAccessInfo = apiAccessInfoRepository.findByAccessIdAndAccessKeyAndAccessIp(accessId, accessKey, accessIp);\n\n        if (apiAccessInfo != null) {\n            return new ApiAccessInfoResponseDto(apiAccessInfo);\n        }\n\n        return new ApiAccessInfoResponseDto();\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot Redis Entity \ubcc4\ub85c TTL \uc9c0\uc815\ud558\uae30 \uc758\uc874\uc131 \ucd94\uac00 \uc544\ub798 \uc124\uc815\uc73c\ub85c \uc124\uc815\uc774 \ub05d\ub0a9\ub2c8\ub2e4. Redis Java Client \ub294 Lettuce \uac00 \uc790\ub3d9\uc73c\ub85c \ucd94\uac00\ub429\ub2c8\ub2e4. build.gradle dependencies { implementation &#039;org.springframework.boot:spring-boot-starter-data-redis&#039; } EnableRedisRepositories \uc0dd\uc131 RedisCacheManager \uc5d0\uc11c Entity \ub9c8\ub2e4\uc758 TTL \uc744 \uc124\uc815\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. @RequiredArgsConstructor @Configuration @Slf4j @EnableRedisRepositories public class RedisRepositoryConfig { private final RedisProperties redisProperties; private final int DEFAULT_EXPIRE_SECONDS =\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=1571\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1571","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1571"}],"version-history":[{"count":1,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1571\/revisions"}],"predecessor-version":[{"id":1572,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1571\/revisions\/1572"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}