mirror of
https://github.com/magefree/mage.git
synced 2026-01-09 20:32:06 -08:00
Added optional caching for card objects
This commit is contained in:
parent
5fa99262bf
commit
c630ecd2d5
2 changed files with 32 additions and 5 deletions
28
Mage.Sets/src/mage/cache/CacheService.java
vendored
28
Mage.Sets/src/mage/cache/CacheService.java
vendored
|
|
@ -2,11 +2,10 @@ package mage.cache;
|
|||
|
||||
import mage.Constants;
|
||||
import mage.cards.Card;
|
||||
import mage.cards.ExpansionSet;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author noxx
|
||||
|
|
@ -14,7 +13,9 @@ import java.util.TreeSet;
|
|||
public class CacheService {
|
||||
|
||||
private static final Logger log = Logger.getLogger(CacheService.class);
|
||||
|
||||
|
||||
private static final String CARDS_CACHE_OBJECT_NAME = "cards";
|
||||
private static final String CARDS_KEY = "cards_key";
|
||||
private static final String NAMES_CACHE_OBJECT_NAME = "card_names";
|
||||
private static final String NAMES_KEY = "card_names_key";
|
||||
private static final String CARD_COUNT_KEY = "card_count_key";
|
||||
|
|
@ -25,6 +26,25 @@ public class CacheService {
|
|||
|
||||
private static final int CACHE_VERSION = 1;
|
||||
|
||||
public static List<Card> loadCards(Collection<ExpansionSet> sets) {
|
||||
Cache cache = CacheDataHelper.getCachedObject(CARDS_CACHE_OBJECT_NAME);
|
||||
List<Card> cards = new ArrayList<Card>();
|
||||
if (cache == null || cache.getVersion() != CACHE_VERSION) {
|
||||
for (ExpansionSet set : sets) {
|
||||
cards.addAll(set.getCards());
|
||||
}
|
||||
cache = new Cache(CARDS_CACHE_OBJECT_NAME, CACHE_VERSION);
|
||||
cache.getCacheObjects().put(CARDS_KEY, cards);
|
||||
cache.getCacheObjects().put(CARD_COUNT_KEY, cards.size());
|
||||
CacheDataHelper.cacheObject(cache, CARDS_CACHE_OBJECT_NAME);
|
||||
} else {
|
||||
cards = (List<Card>) cache.getCacheObjects().get(CARDS_KEY);
|
||||
log.debug("Loaded cards from cache.");
|
||||
}
|
||||
|
||||
return cards;
|
||||
}
|
||||
|
||||
public static Set<String> loadCardNames(List<Card> cards) {
|
||||
Cache cache = CacheDataHelper.getCachedObject(NAMES_CACHE_OBJECT_NAME);
|
||||
Set<String> names = new TreeSet<String>();
|
||||
|
|
|
|||
|
|
@ -59,6 +59,8 @@ public class Sets extends HashMap<String, ExpansionSet> {
|
|||
private static Map<String, Card> cardMap;
|
||||
protected static Random rnd = new Random();
|
||||
|
||||
private boolean useCachedCards = false;
|
||||
|
||||
public static Sets getInstance() {
|
||||
return fINSTANCE;
|
||||
}
|
||||
|
|
@ -117,6 +119,9 @@ public class Sets extends HashMap<String, ExpansionSet> {
|
|||
this.addSet(Weatherlight.getInstance());
|
||||
this.addSet(Worldwake.getInstance());
|
||||
this.addSet(Zendikar.getInstance());
|
||||
if (useCachedCards) {
|
||||
cards = CacheService.loadCards(this.values());
|
||||
}
|
||||
names = CacheService.loadCardNames(cards);
|
||||
creatureTypes = CacheService.loadCreatureTypes(cards);
|
||||
nonLandNames = CacheService.loadNonLandNames(cards);
|
||||
|
|
@ -124,7 +129,9 @@ public class Sets extends HashMap<String, ExpansionSet> {
|
|||
|
||||
private void addSet(ExpansionSet set) {
|
||||
this.put(set.getCode(), set);
|
||||
cards.addAll(set.getCards());
|
||||
if (!useCachedCards) { // cards will be read from cache later
|
||||
cards.addAll(set.getCards());
|
||||
}
|
||||
}
|
||||
|
||||
public static Set<String> getCardNames() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue