Subversion Repositories SmartDukaan

Rev

Rev 2989 | Rev 3112 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2989 Rev 3050
Line 1... Line 1...
1
package in.shop2020.serving.controllers;
1
package in.shop2020.serving.controllers;
2
 
2
 
3
import in.shop2020.datalogger.EventType;
3
import in.shop2020.datalogger.EventType;
4
import in.shop2020.model.v1.catalog.InventoryService.Client;
4
import in.shop2020.model.v1.catalog.InventoryService.Client;
5
import in.shop2020.model.v1.catalog.InventoryServiceException;
5
import in.shop2020.model.v1.catalog.InventoryServiceException;
-
 
6
import in.shop2020.serving.utils.EhcacheWrapper;
6
import in.shop2020.serving.utils.FileUtils;
7
import in.shop2020.serving.utils.FileUtils;
7
import in.shop2020.serving.utils.Utils;
8
import in.shop2020.serving.utils.Utils;
8
import in.shop2020.thrift.clients.CatalogServiceClient;
9
import in.shop2020.thrift.clients.CatalogServiceClient;
9
import in.shop2020.utils.DataLogger;
10
import in.shop2020.utils.DataLogger;
10
 
11
 
11
import java.io.File;
12
import java.io.File;
12
import java.io.IOException;
13
import java.io.IOException;
13
import java.util.ArrayList;
14
import java.util.ArrayList;
14
import java.util.List;
15
import java.util.List;
15
 
16
 
-
 
17
import net.sf.ehcache.CacheManager;
-
 
18
 
16
import org.apache.log4j.Logger;
19
import org.apache.log4j.Logger;
17
import org.apache.struts2.convention.annotation.Action;
20
import org.apache.struts2.convention.annotation.Action;
18
import org.apache.thrift.TException;
21
import org.apache.thrift.TException;
19
 
22
 
20
/**
23
/**
Line 24... Line 27...
24
 */
27
 */
25
@SuppressWarnings("serial")
28
@SuppressWarnings("serial")
26
public class HomeController extends BaseController {
29
public class HomeController extends BaseController {
27
 
30
 
28
	private static Logger logger = Logger.getLogger(Class.class);
31
	private static Logger logger = Logger.getLogger(Class.class);
-
 
32
	private static final String CACHE_NAME = "HomePageSnippets";
-
 
33
	private static final String BEST_DEALS_CACHE_KEY = "home_best_deals_snippet";
-
 
34
	private static final String BEST_SELLERS_CACHE_KEY = "home_best_sellers_snippet";
-
 
35
	private static final String LATEST_ARRIVALS_CACHE_KEY = "home_latest_arrivals_snippet";
-
 
36
    
-
 
37
	
29
	
38
	
30
	private static List<String> bestDealSnippets = null;
39
	private List<String> bestDealSnippets = null;
31
	private static List<String> bestSellerSnippets = null;
40
	private List<String> bestSellerSnippets = null;
32
	private static List<String> latestArrivalSnippets = null;
41
	private List<String> latestArrivalSnippets = null;
33
	
42
	
34
	public HomeController(){
43
	public HomeController(){
35
		super();
44
		super();
36
	}
45
	}
37
 
46
 
Line 45... Line 54...
45
     */
54
     */
46
	@Action("/")
55
	@Action("/")
47
    public String index() throws SecurityException, IOException {
56
    public String index() throws SecurityException, IOException {
48
    	logger.debug("userinfo:" + userinfo.toString());
57
    	logger.debug("userinfo:" + userinfo.toString());
49
    	logger.info("Rendering the home page");
58
    	logger.info("Rendering the home page");
50
    	
-
 
51
    	if(bestDealSnippets == null || bestSellerSnippets == null || latestArrivalSnippets == null){
-
 
52
    	    logger.info("Cache miss: Reloading best deals, best sellers and latest arrivals");
-
 
53
            setSnippets();    	    
59
    	setSnippets();
54
    	}
-
 
55
    	
-
 
56
    	DataLogger.logData(EventType.HOME_PAGE, session.getId(), userinfo.getUserId(), userinfo.getEmail());
60
    	DataLogger.logData(EventType.HOME_PAGE, session.getId(), userinfo.getUserId(), userinfo.getEmail());
57
    	return "index";
61
    	return "index";
58
    }
62
    }
59
 
63
 
60
    /**
64
    /**
Line 66... Line 70...
66
     * @throws SecurityException
70
     * @throws SecurityException
67
     * @throws IOException
71
     * @throws IOException
68
     */
72
     */
69
	@Action("/refresh-home")
73
	@Action("/refresh-home")
70
	public String destroy() throws SecurityException, IOException{
74
	public String destroy() throws SecurityException, IOException{
71
	    logger.info("Reloading best deals, best sellers and latest arrivals");
75
        logger.info("Reloading best deals, best sellers and latest arrivals");
-
 
76
        EhcacheWrapper<String, List<String>> homeSnippetCache = new EhcacheWrapper<String, List<String>>(
-
 
77
                CACHE_NAME, CacheManager.create());
-
 
78
        homeSnippetCache.removeAll();
72
	    setSnippets();
79
	    setSnippets();
73
	    return "index";
80
	    return "index";
74
	}
81
	}
75
	
82
	
76
	public List<String> getBestDealSnippets(){
83
	public List<String> getBestDealSnippets(){
Line 88... Line 95...
88
	/**
95
	/**
89
	 * Loads the snippets for best deals, best sellers and latest arrivals
96
	 * Loads the snippets for best deals, best sellers and latest arrivals
90
	 * into memory.
97
	 * into memory.
91
	 */
98
	 */
92
    private void setSnippets() {
99
    private void setSnippets() {
-
 
100
        EhcacheWrapper<String, List<String>> homeSnippetCache = new EhcacheWrapper<String, List<String>>(
-
 
101
                CACHE_NAME, CacheManager.create());
-
 
102
        bestDealSnippets = homeSnippetCache.get(BEST_DEALS_CACHE_KEY);
-
 
103
        bestSellerSnippets = homeSnippetCache.get(BEST_SELLERS_CACHE_KEY);
-
 
104
        latestArrivalSnippets = homeSnippetCache.get(LATEST_ARRIVALS_CACHE_KEY);
-
 
105
        
-
 
106
        if (bestDealSnippets != null && bestSellerSnippets != null && latestArrivalSnippets != null) {
-
 
107
            return;
-
 
108
        }
93
        List<Long> bestDealCatalogIds = null;
109
        List<Long> bestDealCatalogIds = null;
94
        List<Long> bestSellerCatalogIds = null;
110
        List<Long> bestSellerCatalogIds = null;
95
        List<Long> latestArrivalCatalogIds = null;
111
        List<Long> latestArrivalCatalogIds = null;
96
        
112
        
97
        int bestSellerCount = 4;
113
        int bestSellerCount = 4;
Line 129... Line 145...
129
        }
145
        }
130
        
146
        
131
        bestDealSnippets = getSnippets(bestDealCatalogIds);
147
        bestDealSnippets = getSnippets(bestDealCatalogIds);
132
        bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, bestSellerCount));
148
        bestSellerSnippets = getSnippets(bestSellerCatalogIds.subList(0, bestSellerCount));
133
        latestArrivalSnippets = getSnippets(latestArrivalCatalogIds).subList(0, latestArrivalCount);
149
        latestArrivalSnippets = getSnippets(latestArrivalCatalogIds).subList(0, latestArrivalCount);
-
 
150
        homeSnippetCache.put(BEST_DEALS_CACHE_KEY, bestDealSnippets);
-
 
151
        homeSnippetCache.put(BEST_SELLERS_CACHE_KEY, bestSellerSnippets);
-
 
152
        homeSnippetCache.put(LATEST_ARRIVALS_CACHE_KEY, latestArrivalSnippets);
134
    }
153
    }
135
	
154
	
136
    /**
155
    /**
137
     * Returns a list of snippets to be used on the home page corresponding to
156
     * Returns a list of snippets to be used on the home page corresponding to
138
     * the given catalog ids. The snippets are in the same order as the ids in
157
     * the given catalog ids. The snippets are in the same order as the ids in
Line 155... Line 174...
155
            }
174
            }
156
        }
175
        }
157
        return snippets;
176
        return snippets;
158
    }
177
    }
159
}
178
}
-
 
179