Skip to content

Instantly share code, notes, and snippets.

@crised
Created February 12, 2013 22:49
Show Gist options
  • Save crised/4774213 to your computer and use it in GitHub Desktop.
Save crised/4774213 to your computer and use it in GitHub Desktop.
package web;
import model.Ad;
import org.infinispan.Cache;
import org.infinispan.manager.EmbeddedCacheManager;
import org.jboss.logging.Logger;
import service.AdService;
import util.Loggable;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;
/**
* Date: 2/12/13
* Time: 3:28 PM
*/
@Loggable
public abstract class CacheService {
@Resource(lookup = "java:jboss/infinispan/container/appcache")
private EmbeddedCacheManager cacheManager;
private Cache<Integer, Ad> cache;
@Inject
AdService adService;
static final Logger log = Logger.getLogger(CacheService.class);
@PostConstruct
public void initCache() {
cache = cacheManager.getCache("pagination", false);
if(cache==null) log.warn("cache is null when called grom cacheManager!");
}
public void populateCache() {
log.info("calling populateCache");
List<Ad> adList = adService.getAll();
if (cache != null) {
log.info(cacheManager.isDefaultRunning());
for (Ad ad : adList) {
if (cache == null) log.warn("cache is null");
cache.put(ad.getId(), ad);
}
} else log.error("cache is null");
log.info(cache.size());
}
public List<Ad> getCompleteList() {
log.info("Cache List Size from getCompleteList(): " + getCache().size());
return new ArrayList<>(getCache().values());
}
public List<Ad> getSubList(int fromIndex, int toIndex) {
log.info("Cache List Size from getSublist: " + getCache().size());
log.info("Complete List Size from getSublist:" + getCompleteList().size());
if (getCompleteList().size() >= 1) {
int maxToIndex = getCompleteList().size() - 1;
if (toIndex > maxToIndex) toIndex = maxToIndex;
} else return null; //Empty List
return getCompleteList().subList(fromIndex, toIndex);
}
public Cache<Integer, Ad> getCache() {
if (cache == null) {
initCache();
populateCache();
return cache;
}
if (cache.isEmpty()) {
populateCache();
return cache;
}
return cache;
}
}
package web;
import model.Ad;
import org.infinispan.Cache;
import org.infinispan.manager.EmbeddedCacheManager;
import org.jboss.logging.Logger;
import util.Loggable;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import java.util.List;
/**
* Date: 2/12/13
* Time: 4:22 PM
*/
@Named
@RequestScoped
public class ViewBean extends CacheService {
static final Logger log2 = Logger.getLogger(ViewBean.class);
private List<Ad> adSubList;
@PostConstruct
public void init(){
//adSubList = getSubList(0,5); // Get the first 6
adSubList = getCompleteList();
log2.info("ViewBean Created");
}
@PreDestroy
public void destroy(){
log2.info("ViewBean Destroyed");
}
public List<Ad> getAdSubList() {
return adSubList;
}
public void setAdSubList(List<Ad> adSubList) {
this.adSubList = adSubList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment