From dfef4584d831e873ff20a076e608365163672ed0 Mon Sep 17 00:00:00 2001 From: "Yuan, Harry Hongbin" Date: Mon, 20 May 2024 18:16:16 +0200 Subject: [PATCH] fix error and add CacheDevController (#249) --- deploy/docker-compose-backend-app.yaml | 1 + .../officialsite/member/MemberController.java | 2 +- .../oauth2/AccessTokenCacheService.java | 14 +++++++--- .../oauth2/controller/CacheDevController.java | 27 +++++++++++++++++++ 4 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/dl/officialsite/oauth2/controller/CacheDevController.java diff --git a/deploy/docker-compose-backend-app.yaml b/deploy/docker-compose-backend-app.yaml index e56bd9c7..e21fd84b 100644 --- a/deploy/docker-compose-backend-app.yaml +++ b/deploy/docker-compose-backend-app.yaml @@ -24,6 +24,7 @@ services: - CRON_DIS= ports: - '8080:8080' + - '127.0.0.1:9093:9093' volumes: - ./logs:/logs restart: always diff --git a/src/main/java/com/dl/officialsite/member/MemberController.java b/src/main/java/com/dl/officialsite/member/MemberController.java index edbdaafb..326c405a 100644 --- a/src/main/java/com/dl/officialsite/member/MemberController.java +++ b/src/main/java/com/dl/officialsite/member/MemberController.java @@ -146,7 +146,7 @@ public BaseResponse createMember(@Valid @RequestBody Member member, @RequestPara @PutMapping("/update") public BaseResponse updateMemberByAddress(@RequestParam String address, @RequestBody MemberVo member, HttpServletRequest request) { - this.memberService.nickNameExists(member.getNickName()); + // this.memberService.nickNameExists(member.getNickName()); Optional memberData = memberRepository.findByAddress(address); diff --git a/src/main/java/com/dl/officialsite/oauth2/AccessTokenCacheService.java b/src/main/java/com/dl/officialsite/oauth2/AccessTokenCacheService.java index 60a78511..3fa3031d 100644 --- a/src/main/java/com/dl/officialsite/oauth2/AccessTokenCacheService.java +++ b/src/main/java/com/dl/officialsite/oauth2/AccessTokenCacheService.java @@ -3,6 +3,7 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; +import java.util.Map; import java.util.concurrent.TimeUnit; public class AccessTokenCacheService { @@ -10,14 +11,19 @@ public class AccessTokenCacheService { .expireAfterWrite(6, TimeUnit.MINUTES) .build(); - public static void addGitHubAccessToken(String username, String accessToken){ - ACCESS_TOKEN_CACHE.put(gitHubAccessTokenKey(username),accessToken); + public static void addGitHubAccessToken(String username, String accessToken) { + ACCESS_TOKEN_CACHE.put(gitHubAccessTokenKey(username), accessToken); } - public static String getGitHubAccessToken(String username){ + + public static String getGitHubAccessToken(String username) { return ACCESS_TOKEN_CACHE.getIfPresent(gitHubAccessTokenKey(username)); } - private static String gitHubAccessTokenKey(String username){ + public static Map listAll() { + return ACCESS_TOKEN_CACHE.asMap(); + } + + private static String gitHubAccessTokenKey(String username) { return String.format("%s_GITHUB_ACCESS_TOKEN", username); } diff --git a/src/main/java/com/dl/officialsite/oauth2/controller/CacheDevController.java b/src/main/java/com/dl/officialsite/oauth2/controller/CacheDevController.java new file mode 100644 index 00000000..8f604b91 --- /dev/null +++ b/src/main/java/com/dl/officialsite/oauth2/controller/CacheDevController.java @@ -0,0 +1,27 @@ +package com.dl.officialsite.oauth2.controller; + + +import com.dl.officialsite.common.base.BaseResponse; +import com.dl.officialsite.oauth2.AccessTokenCacheService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.annotation.Profile; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@Slf4j +@Profile({"dev", "local"}) +public class CacheDevController { + + @GetMapping("cache/list") + public BaseResponse listAll(){ + return BaseResponse.successWithData(AccessTokenCacheService.listAll()); + } + @GetMapping("cache/add") + public BaseResponse add(@RequestParam String key,@RequestParam String value){ + AccessTokenCacheService.addGitHubAccessToken(key,value); + return BaseResponse.success(); + } + +}