Connecting caches to clearing mechanism.

This commit is contained in:
marcins78@gmail.com
2012-12-16 19:14:48 +00:00
parent b5d6741e4d
commit 4d1b661d41
11 changed files with 90 additions and 17 deletions

View File

@@ -0,0 +1,17 @@
package com.gempukku.lotro.cache;
import java.util.HashSet;
import java.util.Set;
public class CacheManager {
private Set<Cached> _caches = new HashSet<Cached>();
public void addCache(Cached cached) {
_caches.add(cached);
}
public void clearCaches() {
for (Cached cache : _caches)
cache.clearCache();
}
}

View File

@@ -0,0 +1,5 @@
package com.gempukku.lotro.cache;
public interface Cached {
public void clearCache();
}

View File

@@ -1,5 +1,6 @@
package com.gempukku.lotro.collection;
import com.gempukku.lotro.cache.Cached;
import com.gempukku.lotro.db.CollectionDAO;
import com.gempukku.lotro.game.CardCollection;
import org.apache.commons.collections.map.LRUMap;
@@ -9,7 +10,7 @@ import java.sql.SQLException;
import java.util.Collections;
import java.util.Map;
public class CachedCollectionDAO implements CollectionDAO {
public class CachedCollectionDAO implements CollectionDAO, Cached {
private CollectionDAO _delegate;
private Map<String, CardCollection> _playerCollections = Collections.synchronizedMap(new LRUMap(100));
@@ -17,6 +18,11 @@ public class CachedCollectionDAO implements CollectionDAO {
_delegate = delegate;
}
@Override
public void clearCache() {
_playerCollections.clear();
}
@Override
public CardCollection getPlayerCollection(int playerId, String type) throws SQLException, IOException {
String key = constructCacheKey(playerId, type);

View File

@@ -1,5 +1,6 @@
package com.gempukku.lotro.collection;
import com.gempukku.lotro.cache.Cached;
import com.gempukku.lotro.game.CardCollection;
import java.util.Collections;
@@ -7,7 +8,7 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class CachedTransferDAO implements TransferDAO {
public class CachedTransferDAO implements TransferDAO, Cached {
private TransferDAO _delegate;
private Set<String> _playersWithoutDelivery = Collections.synchronizedSet(new HashSet<String>());
@@ -15,6 +16,12 @@ public class CachedTransferDAO implements TransferDAO {
_delegate = delegate;
}
@Override
public void clearCache() {
_playersWithoutDelivery.clear();
}
public boolean hasUndeliveredPackages(String player) {
if (_playersWithoutDelivery.contains(player))
return false;

View File

@@ -1,5 +1,6 @@
package com.gempukku.lotro.db;
import com.gempukku.lotro.cache.Cached;
import com.gempukku.lotro.game.Player;
import com.gempukku.lotro.logic.vo.LotroDeck;
import org.apache.commons.collections.map.LRUMap;
@@ -9,7 +10,7 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class CachedDeckDAO implements DeckDAO {
public class CachedDeckDAO implements DeckDAO, Cached {
private DeckDAO _delegate;
private Map<String, Set<String>> _playerDeckNames = Collections.synchronizedMap(new LRUMap(100));
private Map<String, LotroDeck> _decks = Collections.synchronizedMap(new LRUMap(100));
@@ -18,6 +19,12 @@ public class CachedDeckDAO implements DeckDAO {
_delegate = delegate;
}
@Override
public void clearCache() {
_playerDeckNames.clear();
_decks.clear();
}
@Override
public LotroDeck buildDeckFromContents(String deckName, String contents) {
return _delegate.buildDeckFromContents(deckName, contents);

View File

@@ -1,12 +1,13 @@
package com.gempukku.lotro.db;
import com.gempukku.lotro.cache.Cached;
import org.apache.commons.collections.map.LRUMap;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
public class CachedMerchantDAO implements MerchantDAO {
public class CachedMerchantDAO implements MerchantDAO, Cached {
private MerchantDAO _delegate;
private Map<String, Transaction> _blueprintIdLastTransaction = Collections.synchronizedMap(new LRUMap(500));
@@ -14,6 +15,11 @@ public class CachedMerchantDAO implements MerchantDAO {
_delegate = delegate;
}
@Override
public void clearCache() {
_blueprintIdLastTransaction.clear();
}
@Override
public void addTransaction(String blueprintId, float price, Date date, TransactionType transactionType) {
_delegate.addTransaction(blueprintId, price, date, transactionType);

View File

@@ -1,5 +1,6 @@
package com.gempukku.lotro.db;
import com.gempukku.lotro.cache.Cached;
import com.gempukku.lotro.game.Player;
import org.apache.commons.collections.map.LRUMap;
@@ -7,7 +8,7 @@ import java.sql.SQLException;
import java.util.Collections;
import java.util.Map;
public class CachedPlayerDAO implements PlayerDAO {
public class CachedPlayerDAO implements PlayerDAO, Cached {
private PlayerDAO _delegate;
private Map<Integer, Player> _playerById = Collections.synchronizedMap(new LRUMap(500));
private Map<String, Player> _playerByName = Collections.synchronizedMap(new LRUMap(500));
@@ -16,6 +17,12 @@ public class CachedPlayerDAO implements PlayerDAO {
_delegate = delegate;
}
@Override
public void clearCache() {
_playerById.clear();
_playerByName.clear();
}
@Override
public Player getPlayer(int id) {
Player player = (Player) _playerById.get(id);

View File

@@ -1,5 +1,6 @@
package com.gempukku.lotro.league;
import com.gempukku.lotro.cache.Cached;
import com.gempukku.lotro.db.LeagueMatchDAO;
import com.gempukku.lotro.db.vo.LeagueMatchResult;
import org.apache.commons.collections.map.LRUMap;
@@ -11,7 +12,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class CachedLeagueMatchDAO implements LeagueMatchDAO {
public class CachedLeagueMatchDAO implements LeagueMatchDAO, Cached {
private LeagueMatchDAO _leagueMatchDAO;
private ReadWriteLock _readWriteLock = new ReentrantReadWriteLock();
@@ -21,6 +22,16 @@ public class CachedLeagueMatchDAO implements LeagueMatchDAO {
_leagueMatchDAO = leagueMatchDAO;
}
@Override
public void clearCache() {
_readWriteLock.writeLock().lock();
try {
_cachedMatches.clear();
} finally {
_readWriteLock.writeLock().unlock();
}
}
@Override
public Collection<LeagueMatchResult> getLeagueMatches(String leagueId) {
_readWriteLock.readLock().lock();
@@ -64,13 +75,4 @@ public class CachedLeagueMatchDAO implements LeagueMatchDAO {
_readWriteLock.writeLock().unlock();
}
}
public void clearCache() {
_readWriteLock.writeLock().lock();
try {
_cachedMatches.clear();
} finally {
_readWriteLock.writeLock().unlock();
}
}
}

View File

@@ -1,5 +1,6 @@
package com.gempukku.lotro.league;
import com.gempukku.lotro.cache.Cached;
import com.gempukku.lotro.db.LeagueParticipationDAO;
import com.gempukku.lotro.game.Player;
import org.apache.commons.collections.map.LRUMap;
@@ -12,7 +13,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class CachedLeagueParticipationDAO implements LeagueParticipationDAO {
public class CachedLeagueParticipationDAO implements LeagueParticipationDAO, Cached {
private LeagueParticipationDAO _leagueParticipationDAO;
private ReadWriteLock _readWriteLock = new ReentrantReadWriteLock();
@@ -64,6 +65,7 @@ public class CachedLeagueParticipationDAO implements LeagueParticipationDAO {
return leagueParticipants;
}
@Override
public void clearCache() {
_readWriteLock.writeLock().lock();
try {

View File

@@ -1,6 +1,7 @@
package com.gempukku.lotro.server;
import com.gempukku.lotro.DateUtils;
import com.gempukku.lotro.cache.CacheManager;
import com.gempukku.lotro.collection.CollectionsManager;
import com.gempukku.lotro.db.DeckDAO;
import com.gempukku.lotro.db.LeagueDAO;
@@ -54,6 +55,9 @@ public class AdminResource extends AbstractResource {
@Context
private LotroFormatLibrary _formatLibrary;
@Context
private CacheManager _cacheManager;
@Path("/clearCache")
@GET
public String clearCache(@Context HttpServletRequest request) throws Exception {
@@ -62,6 +66,8 @@ public class AdminResource extends AbstractResource {
_leagueService.clearCache();
_tournamentService.clearCache();
_cacheManager.clearCaches();
return "OK";
}

View File

@@ -1,5 +1,6 @@
package com.gempukku.lotro.server.provider;
import com.gempukku.lotro.cache.CacheManager;
import com.gempukku.lotro.collection.CachedCollectionDAO;
import com.gempukku.lotro.collection.CachedTransferDAO;
import com.gempukku.lotro.collection.CollectionSerializer;
@@ -41,7 +42,6 @@ public class DaoBuilder {
CachedCollectionDAO collectionDao = new CachedCollectionDAO(dbCollectionDao);
objectMap.put(CollectionDAO.class, collectionDao);
DbPlayerDAO dbPlayerDao = new DbPlayerDAO(dbAccess);
CachedPlayerDAO playerDao = new CachedPlayerDAO(dbPlayerDao);
objectMap.put(PlayerDAO.class, playerDao);
@@ -49,5 +49,13 @@ public class DaoBuilder {
DbTransferDAO dbTransferDao = new DbTransferDAO(dbAccess);
CachedTransferDAO transferDao = new CachedTransferDAO(dbTransferDao);
objectMap.put(TransferDAO.class, transferDao);
CacheManager cacheManager = new CacheManager();
cacheManager.addCache(merchantDao);
cacheManager.addCache(deckDao);
cacheManager.addCache(collectionDao);
cacheManager.addCache(playerDao);
cacheManager.addCache(transferDao);
objectMap.put(CacheManager.class, cacheManager);
}
}