Skip to content

Commit

Permalink
Merge pull request #152 from 4m9d/refactor/#151/use-ehcache-to-improv…
Browse files Browse the repository at this point in the history
…e-performance(swm-426)

Refactor/#151/use ehcache to improve performance(swm 426)
  • Loading branch information
D-w-nJ authored Mar 4, 2024
2 parents 0db2e67 + 0f4435b commit a06ab4b
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 1 deletion.
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ dependencies {
// json
implementation 'com.fasterxml.jackson.core:jackson-databind:2.11.0'

// ehcache
implementation 'org.springframework.boot:spring-boot-starter-cache'
implementation 'net.sf.ehcache:ehcache'

compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
}
Expand Down
58 changes: 58 additions & 0 deletions src/main/java/com/m9d/sroom/common/EhcacheController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.m9d.sroom.common;

import com.m9d.sroom.playlist.PlaylistService;
import com.m9d.sroom.video.VideoService;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import org.springframework.cache.CacheManager;
import org.springframework.cache.ehcache.EhCacheCache;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api")
public class EhcacheController {

private final CacheManager cacheManager;
private final VideoService videoService;
private final PlaylistService playlistService;
public EhcacheController(CacheManager cacheManager, VideoService videoService, PlaylistService playlistService) {
this.cacheManager = cacheManager;
this.videoService = videoService;
this.playlistService = playlistService;
}

@GetMapping("/ehcache")
public Object findAll(){
return cacheManager.getCacheNames().stream()
.map(cacheName -> {
EhCacheCache cache = (EhCacheCache) cacheManager.getCache(cacheName);
Ehcache ehcache = cache.getNativeCache();
Map<String, List<String>> entry = new HashMap<>();

ehcache.getKeys().forEach(key -> {
Element element = ehcache.get(key);
if (element != null) {
entry.computeIfAbsent(cacheName, k -> new ArrayList<>()).add(element.toString());
}
});
return entry;
})
.collect(Collectors.toList());
}

// @GetMapping("/ehcache/test/video/{videoCode}")
// public Object getVideoInfo(@PathVariable String videoCode){
// return videoService.getRecentVideo(videoCode);
// }
//
// @GetMapping("/ehcache/test/playlist/{playlistCode}")
// public Object getPlaylistInfo(@PathVariable String playlistCode){
// return playlistService.getRecentPlaylist(playlistCode);
// }
}
28 changes: 28 additions & 0 deletions src/main/java/com/m9d/sroom/config/EhcacheConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.m9d.sroom.config;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.ClassPathResource;

@Configuration
@EnableCaching
public class EhcacheConfig {
@Bean
@Primary
public CacheManager cacheManager(EhCacheManagerFactoryBean ehCacheManagerFactoryBean) {
return new EhCacheCacheManager(ehCacheManagerFactoryBean.getObject());
}

@Bean
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml"));
ehCacheManagerFactoryBean.setShared(true);
return ehCacheManagerFactoryBean;
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/m9d/sroom/playlist/PlaylistService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.m9d.sroom.youtube.vo.PlaylistItemInfo;
import com.m9d.sroom.youtube.vo.PlaylistVideoInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.*;
Expand All @@ -41,6 +42,7 @@ public PlaylistService(PlaylistRepository playlistRepository, YoutubeMapper yout
this.playlistVideoRepository = playlistVideoRepository;
}

@Cacheable(value = "PlaylistInfoGetter")
public Playlist getRecentPlaylist(String playlistCode) {
Optional<PlaylistEntity> playlistEntityOptional = playlistRepository.findByCode(playlistCode);
int reviewCount = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/m9d/sroom/video/VideoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.m9d.sroom.video.constant.VideoConstant;
import com.m9d.sroom.youtube.YoutubeMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
Expand All @@ -38,7 +39,7 @@ public VideoService(VideoRepository videoRepository, YoutubeMapper youtubeServic
this.aiService = aiService;
}


@Cacheable(value = "VideoInfoGetter")
public Video getRecentVideo(String videoCode) {
Optional<VideoEntity> videoEntityOptional = videoRepository.findByCode(videoCode);
int reviewCount = 0;
Expand Down
39 changes: 39 additions & 0 deletions src/main/resources/ehcache.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>

<defaultCache
maxElementsInMemory="1000"
maxElementsOnDisk="0"
eternal="false"
statistics="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
overflowToDisk="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU"/>

<cache
name="VideoInfoGetter"
maxElementsInMemory="100000"
maxElementsOnDisk="0"
eternal="false"
statistics="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
overflowToDisk="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU"/>

<cache
name="PlaylistInfoGetter"
maxElementsInMemory="100000"
maxElementsOnDisk="0"
eternal="false"
statistics="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
overflowToDisk="false"
diskPersistent="false"
memoryStoreEvictionPolicy="LRU"/>

</ehcache>

0 comments on commit a06ab4b

Please sign in to comment.