Created
February 14, 2013 14:14
-
-
Save crised/4953086 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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