Skip to content

Instantly share code, notes, and snippets.

@crised
Created February 14, 2013 14:14
Show Gist options
  • Save crised/4953086 to your computer and use it in GitHub Desktop.
Save crised/4953086 to your computer and use it in GitHub Desktop.
@Named
@RequestScoped
public class CacheService implements Serializable {
//was an abstract class
@Resource(lookup = "java:jboss/infinispan/container/appcache")
EmbeddedCacheManager cacheManager;
Cache<Integer, Ad> cache;
@Inject
AdService adService;
List<Ad> adList;
static final Logger log = Logger.getLogger(CacheService.class);
@PostConstruct
public void init() {
cache = cacheManager.getCache("pagination", true);
completeList();
}
public void refreshCache() {
adList = adService.getAll();
cache.put(-1, new Ad()); // Goofy entrance, just to check if it has expired.
for (Ad ad : adList) {
cache.put(ad.getId(), ad);
}
}
public void completeList() {
if (!cache.containsKey(-1)) {
log.info("cache has expired");
refreshCache();
}
if (cache.containsKey(-1)) cache.remove(-1);
adList = new ArrayList<>(cache.values());
}
public List<Ad> getAdList() {
return adList;
}
public void setAdList(List<Ad> adList) {
this.adList = adList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment