Unload unused regions after 5 minutes

This commit is contained in:
Shoghi Cervantes 2015-01-06 18:59:21 +01:00
parent 4b73dbd9f8
commit 042a143dd6
No known key found for this signature in database
GPG Key ID: 78464DB0A7837F89
6 changed files with 29 additions and 0 deletions

View File

@ -2269,6 +2269,8 @@ class Level implements ChunkManager, Metadatable{
}
}
$this->provider->doGarbageCollection();
$this->timings->doChunkGC->stopTiming();
}

View File

@ -218,6 +218,8 @@ interface LevelProvider{
*/
public function getLoadedChunks();
public function doGarbageCollection();
/**
* @return Level
*/

View File

@ -50,6 +50,8 @@ class RegionLoader extends \pocketmine\level\format\mcregion\RegionLoader{
}else{
$this->loadLocationTable();
}
$this->lastUsed = time();
}
public function readChunk($x, $z, $generate = true, $forward = false){
@ -58,6 +60,8 @@ class RegionLoader extends \pocketmine\level\format\mcregion\RegionLoader{
return null;
}
$this->lastUsed = time();
if(!$this->isChunkGenerated($index)){
if($generate === true){
//Allocate space

View File

@ -105,6 +105,10 @@ abstract class BaseLevelProvider implements LevelProvider{
$this->levelData->SpawnZ = new Int("SpawnZ", (int) $pos->z);
}
public function doGarbageCollection(){
}
/**
* @return Compound
*/

View File

@ -174,6 +174,16 @@ class McRegion extends BaseLevelProvider{
}
}
public function doGarbageCollection(){
$limit = time() - 300;
foreach($this->regions as $index => $region){
if($region->lastUsed <= $limit){
$region->close();
unset($this->regions[$index]);
}
}
}
public function loadChunk($chunkX, $chunkZ, $create = false){
$index = Level::chunkHash($chunkX, $chunkZ);
if(isset($this->chunks[$index])){

View File

@ -50,6 +50,8 @@ class RegionLoader{
protected $levelProvider;
protected $locationTable = [];
public $lastUsed;
public function __construct(LevelProvider $level, $regionX, $regionZ){
$this->x = $regionX;
$this->z = $regionZ;
@ -65,6 +67,8 @@ class RegionLoader{
}else{
$this->loadLocationTable();
}
$this->lastUsed = time();
}
public function __destruct(){
@ -84,6 +88,8 @@ class RegionLoader{
return null;
}
$this->lastUsed = time();
if(!$this->isChunkGenerated($index)){
if($generate === true){
//Allocate space
@ -197,6 +203,7 @@ class RegionLoader{
}
public function writeChunk(FullChunk $chunk){
$this->lastUsed = time();
$chunkData = $chunk->toBinary();
if($chunkData !== false){
$this->saveChunk($chunk->getX() - ($this->getX() * 32), $chunk->getZ() - ($this->getZ() * 32), $chunkData);