Skip to content

Commit

Permalink
flatfile implementation, refactor some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Plancke committed Jan 5, 2017
1 parent 7ab74d7 commit 755a8eb
Show file tree
Hide file tree
Showing 12 changed files with 348 additions and 237 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea/*
tests/cache

### Composer template
composer.phar
Expand Down
12 changes: 6 additions & 6 deletions src/HypixelPHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Closure;
use Plancke\HypixelPHP\cache\CacheHandler;
use Plancke\HypixelPHP\cache\CacheTimes;
use Plancke\HypixelPHP\cache\impl\flat\FlatFileCacheHandler;
use Plancke\HypixelPHP\cache\impl\FlatFileCacheHandler;
use Plancke\HypixelPHP\classes\HypixelObject;
use Plancke\HypixelPHP\exceptions\ExceptionCodes;
use Plancke\HypixelPHP\exceptions\HypixelPHPException;
Expand Down Expand Up @@ -128,7 +128,7 @@ public function getLogger() {
*/
public function setLogger(Logger $logger) {
$this->logger = $logger;
$this->loggerGetter = function ($HypixelAPI) use ($logger) {
$this->loggerGetter = function () use ($logger) {
return $logger;
};
return $this;
Expand Down Expand Up @@ -161,7 +161,7 @@ public function getFetcher() {
*/
public function setFetcher(Fetcher $fetcher) {
$this->fetcher = $fetcher;
$this->fetcherGetter = function ($HypixelAPI) use ($fetcher) {
$this->fetcherGetter = function () use ($fetcher) {
return $fetcher;
};
return $this;
Expand Down Expand Up @@ -194,7 +194,7 @@ public function getCacheHandler() {
*/
public function setCacheHandler(CacheHandler $cacheHandler) {
$this->cacheHandler = $cacheHandler;
$this->cacheHandlerGetter = function ($HypixelAPI) use ($cacheHandler) {
$this->cacheHandlerGetter = function ($HypixelPHP) use ($cacheHandler) {
return $cacheHandler;
};
return $this;
Expand Down Expand Up @@ -227,7 +227,7 @@ public function getProvider() {
*/
public function setProvider(Provider $provider) {
$this->provider = $provider;
$this->providerGetter = function ($HypixelAPI) use ($provider) {
$this->providerGetter = function () use ($provider) {
return $provider;
};
return $this;
Expand Down Expand Up @@ -260,7 +260,7 @@ public function getResourceManager() {
*/
public function setResourceManager(ResourceManager $resourceManager) {
$this->resourceManager = $resourceManager;
$this->resourceManagerGetter = function ($HypixelAPI) use ($resourceManager) {
$this->resourceManagerGetter = function () use ($resourceManager) {
return $resourceManager;
};
return $this;
Expand Down
62 changes: 59 additions & 3 deletions src/cache/CacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Plancke\HypixelPHP\cache;

use Closure;
use Plancke\HypixelPHP\classes\HypixelObject;
use Plancke\HypixelPHP\classes\Module;
use Plancke\HypixelPHP\exceptions\ExceptionCodes;
Expand Down Expand Up @@ -92,7 +93,7 @@ public function setCacheTime($for, $int) {
* @param HypixelObject $hypixelObject
* @throws HypixelPHPException
*/
function _setCache($hypixelObject) {
public function _setCache($hypixelObject) {
if ($hypixelObject instanceof Player) {
$this->setCachedPlayer($hypixelObject);
} elseif ($hypixelObject instanceof Guild) {
Expand All @@ -114,35 +115,90 @@ function _setCache($hypixelObject) {
}
}

//@formatter:off
protected function objToArray($obj) {
if ($obj instanceof HypixelObject) {
return $obj->getRaw();
}
return $obj;
}

protected function wrapProvider(Closure $provider, $data) {
if ($data == null) {
return null;
}
return $provider($this->getHypixelPHP(), $data);
}

abstract function setCachedPlayer(Player $player);

/**
* @param $uuid
* @return Player
*/
abstract function getCachedPlayer($uuid);

abstract function setPlayerUUID($username, $obj);

abstract function getUUID($username);

abstract function setCachedGuild(Guild $guild);

/**
* @param $id
* @return Guild
*/
abstract function getCachedGuild($id);

abstract function setGuildIDForUUID($uuid, $obj);

abstract function getGuildIDForUUID($uuid);

abstract function setGuildIDForName($name, $obj);

abstract function getGuildIDForName($name);

abstract function setCachedFriends(Friends $friends);

/**
* @param $uuid
* @return Friends
*/
abstract function getCachedFriends($uuid);

abstract function setCachedSession(Session $session);

/**
* @param $uuid
* @return Session
*/
abstract function getCachedSession($uuid);

abstract function setCachedKeyInfo(KeyInfo $keyInfo);

/**
* @param $key
* @return KeyInfo
*/
abstract function getCachedKeyInfo($key);

abstract function setCachedLeaderboards(Leaderboards $leaderboards);

/**
* @return Leaderboards
*/
abstract function getCachedLeaderboards();

abstract function setCachedBoosters(Boosters $boosters);

/**
* @return Boosters
*/
abstract function getCachedBoosters();

abstract function setCachedWatchdogStats(WatchdogStats $watchdogStats);

/**
* @return WatchdogStats
*/
abstract function getCachedWatchdogStats();
//@formatter:on
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Plancke\HypixelPHP\cache\impl\mongo;
namespace Plancke\HypixelPHP\cache;

abstract class CollectionNames {
abstract class CacheTypes {
const PLAYERS = 'players';
const PLAYER_UUID = 'player_uuid';

Expand All @@ -15,6 +15,8 @@ abstract class CollectionNames {
const SESSIONS = 'sessions';
const API_KEYS = 'api_keys';

// used for things that require just a single object to be saved
const SINGLE_SAVE = 'single_save';
// single saves
const LEADERBOARDS = 'leaderboards';
const BOOSTERS = 'boosters';
const WATCHDOG_STATS = 'watchdogStats';
}
202 changes: 202 additions & 0 deletions src/cache/impl/FlatFileCacheHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
<?php

namespace Plancke\HypixelPHP\cache\impl;

use Plancke\HypixelPHP\cache\CacheHandler;
use Plancke\HypixelPHP\cache\CacheTimes;
use Plancke\HypixelPHP\cache\CacheTypes;
use Plancke\HypixelPHP\classes\HypixelObject;
use Plancke\HypixelPHP\responses\booster\Boosters;
use Plancke\HypixelPHP\responses\friend\Friends;
use Plancke\HypixelPHP\responses\guild\Guild;
use Plancke\HypixelPHP\responses\KeyInfo;
use Plancke\HypixelPHP\responses\Leaderboards;
use Plancke\HypixelPHP\responses\player\Player;
use Plancke\HypixelPHP\responses\Session;
use Plancke\HypixelPHP\responses\WatchdogStats;
use Plancke\HypixelPHP\util\CacheUtil;
use Plancke\HypixelPHP\util\Utilities;

/**
* Implementation for CacheHandler, stores data flat filed
*
* Class FlatFileCacheHandler
* @package HypixelPHP
*/
class FlatFileCacheHandler extends CacheHandler {

protected $baseDirectory = "cache" . DIRECTORY_SEPARATOR . "HypixelPHP" . DIRECTORY_SEPARATOR;

/**
* @return string
*/
public function getBaseDirectory() {
return $this->baseDirectory;
}

/**
* @param string $baseDirectory
* @return $this
*/
public function setBaseDirectory($baseDirectory) {
$this->baseDirectory = $baseDirectory;
return $this;
}

/**
* @param $filename
* @param HypixelObject $obj
*/
protected function setObjCache($filename, HypixelObject $obj) {
$this->setCache($filename, $this->objToArray($obj));
}

/**
* @param $filename
* @param $obj
*/
protected function setCache($filename, $obj) {
Utilities::setFileContent($this->baseDirectory . DIRECTORY_SEPARATOR . $filename . '.json', json_encode($obj));
}

/**
* @param $filename
* @return array|null
*/
protected function getCache($filename) {
$content = Utilities::getFileContent($this->baseDirectory . DIRECTORY_SEPARATOR . $filename . '.json');
if ($content == null) {
return null;
}
return json_decode($content, true);
}

function setCachedPlayer(Player $player) {
$this->setObjCache(CacheTypes::PLAYERS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($player->getUUID()), $player);
}

function getCachedPlayer($uuid) {
return $this->wrapProvider(
$this->getHypixelPHP()->getProvider()->getPlayer(),
$data = $this->getCache(CacheTypes::PLAYERS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($uuid))
);
}

function setPlayerUUID($username, $obj) {
$this->setCache(CacheTypes::PLAYER_UUID . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($username), $obj);
}

function getUUID($username) {
$data = $this->getCache(CacheTypes::PLAYER_UUID . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($username));
if ($data != null) {
if (isset($data['uuid']) && $data['uuid'] != null && $data['uuid'] != '') {
$cacheTime = $this->getCacheTime(CacheTimes::UUID);
} else {
$cacheTime = $this->getCacheTime(CacheTimes::UUID_NOT_FOUND);
}
$timestamp = array_key_exists('timestamp', $data) ? $data['timestamp'] : 0;
$diff = time() - $cacheTime - $timestamp;

$this->getHypixelPHP()->getLogger()->log("Found name match in PLAYER_UUID! '$diff'");

if ($diff < 0) {
return $data['uuid'];
}
}

return null;
}

function setCachedGuild(Guild $guild) {
$this->setObjCache(CacheTypes::GUILDS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($guild->getID()), $guild);
}

function getCachedGuild($id) {
return $this->wrapProvider(
$this->getHypixelPHP()->getProvider()->getGuild(),
$data = $this->getCache(CacheTypes::GUILDS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($id))
);
}

function setGuildIDForUUID($uuid, $obj) {
$this->setCache(CacheTypes::GUILDS_UUID . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($uuid), $obj);
}

function getGuildIDForUUID($uuid) {
return $this->getCache(CacheTypes::GUILDS_UUID . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($uuid));
}

function setGuildIDForName($name, $obj) {
$this->setCache(CacheTypes::GUILDS_NAME . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($name), $obj);
}

function getGuildIDForName($name) {
return $this->getCache(CacheTypes::GUILDS_NAME . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($name));
}

function setCachedFriends(Friends $friends) {
$this->setObjCache(CacheTypes::FRIENDS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($friends->getUUID()), $friends);
}

function getCachedFriends($uuid) {
return $this->wrapProvider(
$this->getHypixelPHP()->getProvider()->getFriends(),
$this->getCache(CacheTypes::FRIENDS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($uuid))
);
}

function setCachedSession(Session $session) {
$this->setObjCache(CacheTypes::SESSIONS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($session->getUUID()), $session);
}

function getCachedSession($uuid) {
return $this->wrapProvider(
$this->getHypixelPHP()->getProvider()->getSession(),
$this->getCache(CacheTypes::SESSIONS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($uuid))
);
}

function setCachedKeyInfo(KeyInfo $keyInfo) {
$this->setObjCache(CacheTypes::API_KEYS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($keyInfo->getKey()), $keyInfo);
}

function getCachedKeyInfo($key) {
return $this->wrapProvider(
$this->getHypixelPHP()->getProvider()->getKeyInfo(),
$this->getCache(CacheTypes::SESSIONS . DIRECTORY_SEPARATOR . CacheUtil::getCacheFileName($key))
);
}

function setCachedLeaderboards(Leaderboards $leaderboards) {
$this->setObjCache(CacheTypes::LEADERBOARDS, $leaderboards);
}

function getCachedLeaderboards() {
return $this->wrapProvider(
$this->getHypixelPHP()->getProvider()->getLeaderboards(),
$this->getCache(CacheTypes::LEADERBOARDS)
);
}

function setCachedBoosters(Boosters $boosters) {
$this->setObjCache(CacheTypes::BOOSTERS, $boosters);
}

function getCachedBoosters() {
return $this->wrapProvider(
$this->getHypixelPHP()->getProvider()->getBoosters(),
$this->getCache(CacheTypes::BOOSTERS)
);
}

function setCachedWatchdogStats(WatchdogStats $watchdogStats) {
$this->setObjCache(CacheTypes::WATCHDOG_STATS, $watchdogStats);
}

function getCachedWatchdogStats() {
return $this->wrapProvider(
$this->getHypixelPHP()->getProvider()->getWatchdogStats(),
$this->getCache(CacheTypes::WATCHDOG_STATS)
);
}
}
Loading

0 comments on commit 755a8eb

Please sign in to comment.