| 3561 |
rajveer |
1 |
package in.shop2020.serving.cache;
|
|
|
2 |
import in.shop2020.serving.cache.EhcacheWrapper.CacheKeys;
|
|
|
3 |
import in.shop2020.serving.services.ContentServingService;
|
|
|
4 |
import in.shop2020.serving.utils.SnippetType;
|
|
|
5 |
|
|
|
6 |
import java.util.HashMap;
|
|
|
7 |
import java.util.List;
|
|
|
8 |
import java.util.Map;
|
|
|
9 |
|
|
|
10 |
import net.sf.ehcache.CacheManager;
|
|
|
11 |
|
|
|
12 |
import org.apache.log4j.Logger;
|
|
|
13 |
|
|
|
14 |
public class SnippetCacheWrapper
|
|
|
15 |
{
|
|
|
16 |
private static Logger log = Logger.getLogger(SnippetCacheWrapper.class);
|
|
|
17 |
private static EhcacheWrapper<String, String> categorySnippetCache = new EhcacheWrapper<String, String>(
|
|
|
18 |
EhcacheWrapper.CATEGORY_SNIPPET_CACHE_NAME, CacheManager.create());
|
|
|
19 |
private static EhcacheWrapper<String, String> searchSnippetCache = new EhcacheWrapper<String, String>(
|
|
|
20 |
EhcacheWrapper.SEARCH_SNIPPET_CACHE_NAME, CacheManager.create());
|
|
|
21 |
private static EhcacheWrapper<String, Map<SnippetType, String>> productSnippetsCache = new EhcacheWrapper<String, Map<SnippetType, String>>(
|
|
|
22 |
EhcacheWrapper.PRODUCT_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
|
|
|
23 |
private static EhcacheWrapper<String, Object> showcasePageSnippetsCache = new EhcacheWrapper<String, Object>(
|
|
|
24 |
EhcacheWrapper.SHOWCASE_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
|
|
|
25 |
private static EhcacheWrapper<String, List<String>> homeSnippetCache = new EhcacheWrapper<String, List<String>>(
|
|
|
26 |
EhcacheWrapper.HOME_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
|
|
|
27 |
|
|
|
28 |
public static Object getSnippet(CacheKeys cacheType, String docId, long sourceId){
|
|
|
29 |
|
|
|
30 |
switch (cacheType) {
|
|
|
31 |
case HOME_PAGE_SNIPPET_CACHE_KEY:
|
|
|
32 |
return getHomeSnippet(docId);
|
|
|
33 |
case SHOWCASE_PAGE_SNIPPET_CACHE_KEY:
|
|
|
34 |
return getShowcaseSnippet(docId);
|
|
|
35 |
case PRODUCT_PAGE_SNIPPET_CACHE_KEY:
|
|
|
36 |
return getProductSnippet(docId, sourceId);
|
|
|
37 |
case CATEGORY_SNIPPET_CACHE_KEY:
|
|
|
38 |
return getCategorySnippet(docId, sourceId);
|
|
|
39 |
case SEARCH_SNIPPET_CACHE_KEY:
|
|
|
40 |
return getSearchSnippet(docId, sourceId);
|
|
|
41 |
|
|
|
42 |
default:
|
|
|
43 |
break;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
return sourceId;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
private static String getSearchSnippet(String docId, long sourceId) {
|
|
|
50 |
String snippet = searchSnippetCache.get(docId);
|
|
|
51 |
if (snippet == null) {
|
|
|
52 |
snippet = ContentServingService.getSnippet(SnippetType.SEARCH_SNIPPET, docId, sourceId);
|
|
|
53 |
if (sourceId == -1 && snippet != null) {
|
|
|
54 |
searchSnippetCache.put(docId, snippet);
|
|
|
55 |
}
|
|
|
56 |
}
|
|
|
57 |
else {
|
|
|
58 |
log.info("Loading search snippet from cache : " + docId);
|
|
|
59 |
}
|
|
|
60 |
return snippet;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
private static String getCategorySnippet(String docId, long sourceId)
|
|
|
64 |
{
|
|
|
65 |
String snippet = categorySnippetCache.get(docId);
|
|
|
66 |
if (snippet == null) {
|
| 3607 |
chandransh |
67 |
snippet = ContentServingService.getSnippet(SnippetType.CATEGORY_SNIPPET, docId, sourceId);
|
| 3561 |
rajveer |
68 |
if (sourceId == -1 && snippet != null) {
|
|
|
69 |
categorySnippetCache.put(docId, snippet);
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
else {
|
|
|
73 |
log.info("Loading category snippet from cache : " + docId);
|
|
|
74 |
}
|
|
|
75 |
return snippet;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
private static Map<SnippetType, String> getProductSnippet(String docId, long sourceId) {
|
|
|
79 |
Map<SnippetType, String> snippets = null;
|
|
|
80 |
if(sourceId == -1){
|
|
|
81 |
snippets = productSnippetsCache.get(docId);
|
|
|
82 |
}
|
|
|
83 |
if (snippets == null) {
|
|
|
84 |
log.info("Getting product snippet for :" + docId);
|
|
|
85 |
snippets = new HashMap<SnippetType, String>();
|
|
|
86 |
snippets.put(SnippetType.PRODUCT_PROPERTIES_SNIPPET, ContentServingService.getSnippet(SnippetType.PRODUCT_PROPERTIES_SNIPPET, docId, sourceId));
|
|
|
87 |
snippets.put(SnippetType.PRODUCT_DETAIL_SNIPPET, ContentServingService.getSnippet(SnippetType.PRODUCT_DETAIL_SNIPPET, docId, sourceId));
|
|
|
88 |
snippets.put(SnippetType.SLIDE_GUIDE_SNIPPET, ContentServingService.getSnippet(SnippetType.SLIDE_GUIDE_SNIPPET, docId, sourceId));
|
|
|
89 |
if(sourceId == -1){
|
|
|
90 |
productSnippetsCache.put(docId, snippets);
|
|
|
91 |
}
|
|
|
92 |
return snippets;
|
|
|
93 |
}
|
|
|
94 |
return snippets;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
private static Object getShowcaseSnippet(String docId) {
|
|
|
99 |
// TODO Auto-generated method stub
|
|
|
100 |
return null;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
private static Object getHomeSnippet(String docId) {
|
|
|
104 |
// TODO Auto-generated method stub
|
|
|
105 |
return null;
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
}
|