Connecting caches to clearing mechanism.
This commit is contained in:
17
gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/cache/CacheManager.java
vendored
Normal file
17
gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/cache/CacheManager.java
vendored
Normal 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();
|
||||
}
|
||||
}
|
||||
5
gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/cache/Cached.java
vendored
Normal file
5
gemp-lotr/gemp-lotr-server/src/main/java/com/gempukku/lotro/cache/Cached.java
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
package com.gempukku.lotro.cache;
|
||||
|
||||
public interface Cached {
|
||||
public void clearCache();
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user