Subversion Repositories SmartDukaan

Rev

Rev 3607 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.cache;
import in.shop2020.serving.cache.EhcacheWrapper.CacheKeys;
import in.shop2020.serving.services.ContentServingService;
import in.shop2020.serving.utils.SnippetType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.ehcache.CacheManager;

import org.apache.log4j.Logger;

public class SnippetCacheWrapper
{
    private static Logger log = Logger.getLogger(SnippetCacheWrapper.class);
    private static EhcacheWrapper<String, String> categorySnippetCache = new EhcacheWrapper<String, String>(
                EhcacheWrapper.CATEGORY_SNIPPET_CACHE_NAME, CacheManager.create());
    private static EhcacheWrapper<String, String> searchSnippetCache = new EhcacheWrapper<String, String>(
            EhcacheWrapper.SEARCH_SNIPPET_CACHE_NAME, CacheManager.create());
    private static EhcacheWrapper<String, Map<SnippetType, String>> productSnippetsCache = new EhcacheWrapper<String, Map<SnippetType, String>>(
            EhcacheWrapper.PRODUCT_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());            
    private static EhcacheWrapper<String, Object> showcasePageSnippetsCache = new EhcacheWrapper<String, Object>(
                EhcacheWrapper.SHOWCASE_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
    private static EhcacheWrapper<String, List<String>> homeSnippetCache = new EhcacheWrapper<String, List<String>>(
            EhcacheWrapper.HOME_PAGE_SNIPPET_CACHE_NAME, CacheManager.create());
    private static EhcacheWrapper<String, String> privateDalSnippetCache = new EhcacheWrapper<String, String>(
                EhcacheWrapper.PRIVATE_DEAL_SNIPPET, CacheManager.create());
    
    public static Object getSnippet(CacheKeys cacheType, String docId, long sourceId){
        
        switch (cacheType) {
        case HOME_PAGE_SNIPPET_CACHE_KEY:
                        return getHomeSnippet(docId);
        case SHOWCASE_PAGE_SNIPPET_CACHE_KEY:
                        return getShowcaseSnippet(docId);
        case PRODUCT_PAGE_SNIPPET_CACHE_KEY:
                        return getProductSnippet(docId, sourceId);
        case CATEGORY_SNIPPET_CACHE_KEY:
                        return getCategorySnippet(docId, sourceId);
                case SEARCH_SNIPPET_CACHE_KEY:
                        return getSearchSnippet(docId, sourceId);
                case PRIVATE_DEAL_SNIPPET_KEY:
                        return getPrivateDealSnippet(docId, sourceId);
                        
                        

                default:
                        break;
                }
        
                return sourceId;
    }
    
        private static String getSearchSnippet(String docId, long sourceId) {
        String snippet = searchSnippetCache.get(docId);
        if (snippet == null) {
                snippet = ContentServingService.getSnippet(SnippetType.SEARCH_SNIPPET, docId, sourceId);
            if (sourceId == -1 && snippet != null) {
                searchSnippetCache.put(docId, snippet);
            }
        }
        else {
            log.info("Loading search snippet from cache : " + docId);
        }
        return snippet;
        }
        
        private static String getPrivateDealSnippet(String docId, long sourceId) {
                String snippet = privateDalSnippetCache.get(docId);
                if (snippet == null) {
                        snippet = ContentServingService.getSnippet(SnippetType.PRIVATE_DEAL_SNIPPET, docId, sourceId);
                        if (sourceId == -1 && snippet != null) {
                                privateDalSnippetCache.put(docId, snippet);
                        }
                }
                else {
                        log.info("Loading search snippet from cache : " + docId);
                }
                return snippet;
        }

        private static String getCategorySnippet(String docId, long sourceId)
    {
        String snippet = categorySnippetCache.get(docId);
        if (snippet == null) {
                snippet = ContentServingService.getSnippet(SnippetType.CATEGORY_SNIPPET, docId, sourceId);
            if (sourceId == -1 && snippet != null) {
                categorySnippetCache.put(docId, snippet);
            }
        }
        else {
            log.info("Loading category snippet from cache : " + docId);
        }
        return snippet;
        }       

        private static Map<SnippetType, String> getProductSnippet(String docId, long sourceId) {
            Map<SnippetType, String> snippets = null;
            if(sourceId == -1){
                snippets = productSnippetsCache.get(docId);
            }
            if (snippets == null) {
                log.info("Getting product snippet for :" + docId);
                snippets = new HashMap<SnippetType, String>();
                snippets.put(SnippetType.PRODUCT_PROPERTIES_SNIPPET, ContentServingService.getSnippet(SnippetType.PRODUCT_PROPERTIES_SNIPPET, docId, sourceId));
                snippets.put(SnippetType.PRODUCT_DETAIL_SNIPPET, ContentServingService.getSnippet(SnippetType.PRODUCT_DETAIL_SNIPPET, docId, sourceId));
                snippets.put(SnippetType.SLIDE_GUIDE_SNIPPET, ContentServingService.getSnippet(SnippetType.SLIDE_GUIDE_SNIPPET, docId, sourceId));
                if(sourceId == -1){
                        productSnippetsCache.put(docId, snippets);
                }
                return snippets;
            }
                return snippets;
        }
        
        
        private static Object getShowcaseSnippet(String docId) {
                // TODO Auto-generated method stub
                return null;
        }
        
    private static Object getHomeSnippet(String docId) {
                // TODO Auto-generated method stub
                return null;
        }



}