-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Significantly improve client and server side performance.
Introduces caches at busy places.
- Loading branch information
1 parent
9e3b679
commit 4c783e2
Showing
5 changed files
with
156 additions
and
42 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package evilcraft.core.algorithm; | ||
|
||
/** | ||
* A generic single object cache. | ||
* @param <K> The key type. | ||
* @param <V> The value type. | ||
* @author rubensworks | ||
*/ | ||
public class SingleCache<K, V> { | ||
|
||
private boolean initialized = false; | ||
private K key = null; | ||
private V value = null; | ||
private ICacheUpdater<K, V> updater; | ||
|
||
public SingleCache(ICacheUpdater<K, V> updater) { | ||
this.updater = updater; | ||
} | ||
|
||
public V get(K key) { | ||
if(!this.initialized || !this.updater.isKeyEqual(this.key, key)) { | ||
this.value = this.updater.getNewValue(key); | ||
this.key = key; | ||
this.initialized = true; | ||
} | ||
return this.value; | ||
} | ||
|
||
/** | ||
* This is responsible for fetching new updates when the cache desires this. | ||
* @param <K> The key type. | ||
* @param <V> The value type. | ||
*/ | ||
public static interface ICacheUpdater<K, V> { | ||
|
||
public V getNewValue(K key); | ||
public boolean isKeyEqual(K cacheKey, K newKey); | ||
|
||
} | ||
|
||
} |
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
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
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
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