Skip to content

Commit

Permalink
[FEAT] cache api 추가 및 redisson 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
ohksj77 committed Mar 15, 2024
1 parent 200f13c commit 3f0607e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
1 change: 0 additions & 1 deletion backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ dependencies {
implementation 'org.springframework.cloud:spring-cloud-gcp-storage:1.2.5.RELEASE'
implementation 'com.google.firebase:firebase-admin:6.8.1'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310'
implementation 'org.redisson:redisson-spring-data-30:3.27.1'

annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package com.twtw.backend.config.redis;

import com.twtw.backend.global.properties.RedisProperties;

import lombok.RequiredArgsConstructor;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.spring.data.connection.RedissonConnectionFactory;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
Expand All @@ -29,25 +24,11 @@
@RequiredArgsConstructor
public class RedisConfig {
private static final Long TIME_TO_LIVE = 1L;
private static final String REDISSON_HOST_PREFIX = "redis://";
private static final String URL_DELIMITER = ":";
private final RedisProperties redisProperties;

@Bean
public RedissonClient redissonClient() {
final Config config = new Config();
config.useSingleServer()
.setAddress(
REDISSON_HOST_PREFIX
+ redisProperties.getHost()
+ URL_DELIMITER
+ redisProperties.getPort());
return Redisson.create(config);
}

@Bean
public RedisConnectionFactory redisConnectionFactory() {
return new RedissonConnectionFactory(redissonClient());
return new LettuceConnectionFactory(redisProperties.getHost(), redisProperties.getPort());
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@ public ResponseEntity<PlaceResponse> searchSurroundPlace(
@ModelAttribute final SurroundPlaceRequest surroundPlaceRequest) {
return ResponseEntity.ok(placeService.searchSurroundPlace(surroundPlaceRequest));
}

@GetMapping("surround/cache")
public ResponseEntity<PlaceResponse> searchSurroundPlaceWithCache(
@ModelAttribute final SurroundPlaceRequest surroundPlaceRequest) {
return ResponseEntity.ok(placeService.searchSurroundPlaceWithCache(surroundPlaceRequest));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import lombok.RequiredArgsConstructor;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

Expand All @@ -19,12 +20,21 @@ public class PlaceService {
private final PlaceMapper placeMapper;
private final KakaoMapClient<SurroundPlaceRequest, SurroundPlaceResponse> surroundPlaceClient;

@CacheEvict(
value = "surroundPlace",
key = "'searchSurroundPlace'.concat(#surroundPlaceRequest.toString())",
cacheManager = "cacheManager")
public PlaceResponse searchSurroundPlace(final SurroundPlaceRequest surroundPlaceRequest) {
final SurroundPlaceResponse response = surroundPlaceClient.request(surroundPlaceRequest);
return new PlaceResponse(response.getDocuments(), response.getMeta().getIsEnd());
}

@Cacheable(
value = "surroundPlace",
key = "'searchSurroundPlace'.concat(#surroundPlaceRequest)",
key = "'searchSurroundPlace'.concat(#surroundPlaceRequest.toString())",
cacheManager = "cacheManager",
unless = "#result.results.size() <= 0")
public PlaceResponse searchSurroundPlace(final SurroundPlaceRequest surroundPlaceRequest) {
public PlaceResponse searchSurroundPlaceWithCache(final SurroundPlaceRequest surroundPlaceRequest) {
final SurroundPlaceResponse response = surroundPlaceClient.request(surroundPlaceRequest);
return new PlaceResponse(response.getDocuments(), response.getMeta().getIsEnd());
}
Expand Down

0 comments on commit 3f0607e

Please sign in to comment.