Skip to content

Commit e947657

Browse files
authored
Merge pull request #57 from topcoder-platform/issues-350
Issues-350: cache role resources and challenge resources
2 parents 4f92892 + 695ce56 commit e947657

File tree

1 file changed

+96
-4
lines changed

1 file changed

+96
-4
lines changed

Topcoder/class.topcoder.plugin.php

Lines changed: 96 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class TopcoderPlugin extends Gdn_Plugin {
3232
/** Cache key. */
3333
const CACHE_KEY_TOPCODER_PROFILE = 'topcoder.{UserID}';
3434
const CACHE_TOPCODER_KEY_TOPCODER_PROFILE = 'topcoder.{Handle}';
35+
const CACHE_TOPCODER_KEY_TOPCODER_ROLE_RESOURCES = 'topcoder.roleresources';
36+
const CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE_RESOURCES = 'topcoder.challenge.{ChallengeID}.resources';
3537

3638
const ROLE_TYPE_TOPCODER = 'topcoder';
3739
const ROLE_TOPCODER_CONNECT_ADMIN = 'Connect Admin';
@@ -1320,6 +1322,31 @@ public static function getM2MToken()
13201322
* @return mixed|null
13211323
*/
13221324
public function getRoleResources() {
1325+
$roleResources = self::getRoleResourcesFromCache();
1326+
if ($roleResources) {
1327+
return $roleResources;
1328+
}
1329+
1330+
$roleResources = self::loadTopcoderRoleResources();
1331+
if(Gdn_Cache::activeEnabled() && $roleResources) {
1332+
self::topcoderRoleResourcesCache($roleResources);
1333+
}
1334+
return $roleResources;
1335+
}
1336+
1337+
1338+
private static function topcoderRoleResourcesCache($roleResources) {
1339+
return Gdn::cache()->store(self::CACHE_TOPCODER_KEY_TOPCODER_ROLE_RESOURCES,
1340+
$roleResources, [
1341+
Gdn_Cache::FEATURE_EXPIRY => 3600
1342+
]);
1343+
}
1344+
1345+
/**
1346+
* Load Topcoder Resource roles
1347+
* @return mixed|null
1348+
*/
1349+
private static function loadTopcoderRoleResources() {
13231350
$token = TopcoderPlugin::getM2MToken();
13241351
if ($token) {
13251352
$resourceRolesURI = c('Plugins.Topcoder.ResourceRolesApiURI');
@@ -1344,12 +1371,78 @@ public function getRoleResources() {
13441371
return null;
13451372
}
13461373

1374+
13471375
/**
1348-
* Get Topcoder Challenge Resources
1376+
* Get Role Resources from cache
1377+
* @return false|mixed
1378+
*/
1379+
private static function getRoleResourcesFromCache() {
1380+
if(!Gdn_Cache::activeEnabled()) {
1381+
return false;
1382+
}
1383+
1384+
if(!Gdn::cache()->exists(self::CACHE_TOPCODER_KEY_TOPCODER_ROLE_RESOURCES)) {
1385+
return false;
1386+
}
1387+
$roleResources = Gdn::cache()->get(self::CACHE_TOPCODER_KEY_TOPCODER_ROLE_RESOURCES);
1388+
if ($roleResources === Gdn_Cache::CACHEOP_FAILURE) {
1389+
return false;
1390+
}
1391+
return $roleResources;
1392+
}
1393+
1394+
/**
1395+
* Get Topcoder Challenge Resources by ChallengeId
13491396
* @param $challengeId
13501397
* @return mixed|null
13511398
*/
13521399
public function getChallengeResources($challengeId) {
1400+
$challengeResources = self::getChallengeResourcesFromCache($challengeId);
1401+
if ($challengeResources) {
1402+
return $challengeResources;
1403+
}
1404+
1405+
$challengeResources = self::loadChallengeResources($challengeId);
1406+
if(Gdn_Cache::activeEnabled() && $challengeResources) {
1407+
self::topcoderChallengeResourcesCache( $challengeId, $challengeResources);
1408+
}
1409+
return $challengeResources;
1410+
}
1411+
1412+
/**
1413+
* Load challenge resources from cache
1414+
* @param $challengeID
1415+
* @return false|mixed
1416+
*/
1417+
private static function getChallengeResourcesFromCache($challengeID) {
1418+
if(!Gdn_Cache::activeEnabled()) {
1419+
return false;
1420+
}
1421+
1422+
$handleKey = formatString(self::CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE_RESOURCES, ['ChallengeID' => $challengeID]);
1423+
if(!Gdn::cache()->exists($handleKey)) {
1424+
return false;
1425+
}
1426+
$challengeResources = Gdn::cache()->get($handleKey);
1427+
if ($challengeResources === Gdn_Cache::CACHEOP_FAILURE) {
1428+
return false;
1429+
}
1430+
return $challengeResources;
1431+
}
1432+
1433+
private static function topcoderChallengeResourcesCache($challengeID, $challengeResources) {
1434+
$challengeKey = formatString(self::CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE_RESOURCES, ['ChallengeID' => $challengeID]);
1435+
return Gdn::cache()->store($challengeKey , $challengeResources, [
1436+
Gdn_Cache::FEATURE_EXPIRY => 3600
1437+
]);
1438+
}
1439+
1440+
/**
1441+
* Load Topcoder Challenge Resources by Challenge ID
1442+
* @param $challengeId
1443+
* @return mixed|null
1444+
*/
1445+
private static function loadChallengeResources($challengeId) {
13531446
$token = TopcoderPlugin::getM2MToken();
13541447
if ($token) {
13551448
$resourcesURI = c('Plugins.Topcoder.ResourcesApiURI');
@@ -1585,8 +1678,6 @@ private function setTopcoderProjectData($sender, $challengeID) {
15851678
$currentProjectRoles = $this->getTopcoderProjectRoles(Gdn::session()->User, $resources, $roleResources);
15861679
if($currentProjectRoles) {
15871680
$currentProjectRoles = array_map('strtolower',$currentProjectRoles);
1588-
} else {
1589-
15901681
}
15911682

15921683
$sender->Data['ChallengeResources'] = $resources;
@@ -1614,7 +1705,8 @@ private function getTopcoderProjectRoles($user, $resources = null, $roleResource
16141705
$roles = [];
16151706
if (isset($resources) && isset($roleResources)) {
16161707
$allResourcesByMember = array_filter($resources, function ($k) use ($topcoderUsername) {
1617-
return $k->memberHandle == $topcoderUsername;
1708+
$memberHandle = val('memberHandle', $k, null);
1709+
return $memberHandle && $memberHandle == $topcoderUsername;
16181710
});
16191711
foreach ($allResourcesByMember as $resource) {
16201712
$roleResource = array_filter($roleResources, function ($k) use ($resource) {

0 commit comments

Comments
 (0)