-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
6 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |