Skip to content

Issues-350: cache role resources and challenge resources #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 96 additions & 4 deletions Topcoder/class.topcoder.plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class TopcoderPlugin extends Gdn_Plugin {
/** Cache key. */
const CACHE_KEY_TOPCODER_PROFILE = 'topcoder.{UserID}';
const CACHE_TOPCODER_KEY_TOPCODER_PROFILE = 'topcoder.{Handle}';
const CACHE_TOPCODER_KEY_TOPCODER_ROLE_RESOURCES = 'topcoder.roleresources';
const CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE_RESOURCES = 'topcoder.challenge.{ChallengeID}.resources';

const ROLE_TYPE_TOPCODER = 'topcoder';
const ROLE_TOPCODER_CONNECT_ADMIN = 'Connect Admin';
Expand Down Expand Up @@ -1320,6 +1322,31 @@ public static function getM2MToken()
* @return mixed|null
*/
public function getRoleResources() {
$roleResources = self::getRoleResourcesFromCache();
if ($roleResources) {
return $roleResources;
}

$roleResources = self::loadTopcoderRoleResources();
if(Gdn_Cache::activeEnabled() && $roleResources) {
self::topcoderRoleResourcesCache($roleResources);
}
return $roleResources;
}


private static function topcoderRoleResourcesCache($roleResources) {
return Gdn::cache()->store(self::CACHE_TOPCODER_KEY_TOPCODER_ROLE_RESOURCES,
$roleResources, [
Gdn_Cache::FEATURE_EXPIRY => 3600
]);
}

/**
* Load Topcoder Resource roles
* @return mixed|null
*/
private static function loadTopcoderRoleResources() {
$token = TopcoderPlugin::getM2MToken();
if ($token) {
$resourceRolesURI = c('Plugins.Topcoder.ResourceRolesApiURI');
Expand All @@ -1344,12 +1371,78 @@ public function getRoleResources() {
return null;
}


/**
* Get Topcoder Challenge Resources
* Get Role Resources from cache
* @return false|mixed
*/
private static function getRoleResourcesFromCache() {
if(!Gdn_Cache::activeEnabled()) {
return false;
}

if(!Gdn::cache()->exists(self::CACHE_TOPCODER_KEY_TOPCODER_ROLE_RESOURCES)) {
return false;
}
$roleResources = Gdn::cache()->get(self::CACHE_TOPCODER_KEY_TOPCODER_ROLE_RESOURCES);
if ($roleResources === Gdn_Cache::CACHEOP_FAILURE) {
return false;
}
return $roleResources;
}

/**
* Get Topcoder Challenge Resources by ChallengeId
* @param $challengeId
* @return mixed|null
*/
public function getChallengeResources($challengeId) {
$challengeResources = self::getChallengeResourcesFromCache($challengeId);
if ($challengeResources) {
return $challengeResources;
}

$challengeResources = self::loadChallengeResources($challengeId);
if(Gdn_Cache::activeEnabled() && $challengeResources) {
self::topcoderChallengeResourcesCache( $challengeId, $challengeResources);
}
return $challengeResources;
}

/**
* Load challenge resources from cache
* @param $challengeID
* @return false|mixed
*/
private static function getChallengeResourcesFromCache($challengeID) {
if(!Gdn_Cache::activeEnabled()) {
return false;
}

$handleKey = formatString(self::CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE_RESOURCES, ['ChallengeID' => $challengeID]);
if(!Gdn::cache()->exists($handleKey)) {
return false;
}
$challengeResources = Gdn::cache()->get($handleKey);
if ($challengeResources === Gdn_Cache::CACHEOP_FAILURE) {
return false;
}
return $challengeResources;
}

private static function topcoderChallengeResourcesCache($challengeID, $challengeResources) {
$challengeKey = formatString(self::CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE_RESOURCES, ['ChallengeID' => $challengeID]);
return Gdn::cache()->store($challengeKey , $challengeResources, [
Gdn_Cache::FEATURE_EXPIRY => 3600
]);
}

/**
* Load Topcoder Challenge Resources by Challenge ID
* @param $challengeId
* @return mixed|null
*/
private static function loadChallengeResources($challengeId) {
$token = TopcoderPlugin::getM2MToken();
if ($token) {
$resourcesURI = c('Plugins.Topcoder.ResourcesApiURI');
Expand Down Expand Up @@ -1585,8 +1678,6 @@ private function setTopcoderProjectData($sender, $challengeID) {
$currentProjectRoles = $this->getTopcoderProjectRoles(Gdn::session()->User, $resources, $roleResources);
if($currentProjectRoles) {
$currentProjectRoles = array_map('strtolower',$currentProjectRoles);
} else {

}

$sender->Data['ChallengeResources'] = $resources;
Expand Down Expand Up @@ -1614,7 +1705,8 @@ private function getTopcoderProjectRoles($user, $resources = null, $roleResource
$roles = [];
if (isset($resources) && isset($roleResources)) {
$allResourcesByMember = array_filter($resources, function ($k) use ($topcoderUsername) {
return $k->memberHandle == $topcoderUsername;
$memberHandle = val('memberHandle', $k, null);
return $memberHandle && $memberHandle == $topcoderUsername;
});
foreach ($allResourcesByMember as $resource) {
$roleResource = array_filter($roleResources, function ($k) use ($resource) {
Expand Down