Skip to content

Commit 566b8c9

Browse files
committed
Issues-521: Added caching Challenge data(ChallengeID,IsNDA)
1 parent bd4e4f4 commit 566b8c9

File tree

1 file changed

+75
-6
lines changed

1 file changed

+75
-6
lines changed

Topcoder/class.topcoder.plugin.php

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class TopcoderPlugin extends Gdn_Plugin {
3333
const CACHE_KEY_TOPCODER_PROFILE = 'topcoder.{UserID}';
3434
const CACHE_TOPCODER_KEY_TOPCODER_PROFILE = 'topcoder.{Handle}';
3535
const CACHE_TOPCODER_KEY_TOPCODER_ROLE_RESOURCES = 'topcoder.roleresources';
36+
const CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE = 'topcoder.challenge.{ChallengeID}';
3637
const CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE_RESOURCES = 'topcoder.challenge.{ChallengeID}.resources';
3738

3839
const CACHE_DEFAULT_EXPIRY_TIME = 60*60*3; //The default expiration time in Memcached is in seconds, 10800 = 3 hours
@@ -1446,11 +1447,11 @@ public function getChallengeResources($challengeId) {
14461447
}
14471448

14481449
$expirationTime = self::CACHE_DEFAULT_EXPIRY_TIME;
1449-
$challenge = self::loadChallenge($challengeId);
1450-
if($challenge && count($challenge) > 0) {
1450+
$challenge = self::getChallenge($challengeId);
1451+
if($challenge) {
14511452
// Set expiration time for Challenge roles
1452-
$endDate = strtotime($challenge[0]->endDate);
1453-
$startDate = strtotime($challenge[0]->startDate);
1453+
$endDate = $challenge['EndDate'];
1454+
$startDate =$challenge['StartDate'];
14541455
// $duration = $endDate > -1 && $startDate > -1 ? $endDate - $startDate: 0;
14551456
// archived
14561457
$isEnded = $endDate > -1 && now() - $endDate > 0;
@@ -1520,6 +1521,43 @@ private static function loadChallengeResources($challengeId) {
15201521
return null;
15211522
}
15221523

1524+
/**
1525+
* Get Topcoder Challenge by ChallengeId
1526+
* @param $challengeId
1527+
* @return mixed|null
1528+
*/
1529+
public function getChallenge($challengeId) {
1530+
$challenge = self::getChallengeFromCache($challengeId);
1531+
if ($challenge) {
1532+
return $challenge;
1533+
}
1534+
1535+
$cachedChallenge = ['ChallengeID' => $challengeId];
1536+
$challenge = self::loadChallenge($challengeId);
1537+
1538+
$expirationTime = self::CACHE_DEFAULT_EXPIRY_TIME;
1539+
if($challenge) {
1540+
// Set expiration time for Challenge roles
1541+
$startDate = strtotime($challenge->startDate);
1542+
$endDate = strtotime($challenge->endDate);
1543+
// archived
1544+
$isEnded = $endDate > -1 && now() - $endDate > 0;
1545+
if(!$isEnded) {
1546+
$expirationTime = self::CACHE_ONE_DAY_EXPIRY_TIME;
1547+
}
1548+
$cachedChallenge['StartDate'] = $startDate;
1549+
$cachedChallenge['EndDate'] = $endDate;
1550+
$termIDs = array_column($challenge->terms, 'id');
1551+
$NDA_UUID = c('Plugins.Topcoder.NDA_UUID');
1552+
$cachedChallenge['IsNDA'] = in_array($NDA_UUID, $termIDs);
1553+
}
1554+
if (Gdn_Cache::activeEnabled()) {
1555+
self::topcoderChallengeCache($challengeId, $cachedChallenge, $expirationTime);
1556+
}
1557+
return $cachedChallenge;
1558+
}
1559+
1560+
15231561
/**
15241562
* Load Topcoder Challenge by Challenge ID
15251563
* @param $challengeId
@@ -1535,7 +1573,7 @@ private static function loadChallenge($challengeId) {
15351573
'header' => 'Authorization: Bearer ' .$token
15361574
));
15371575
$context = stream_context_create($options);
1538-
$data = file_get_contents($topcoderChallengeApiUrl . '?challengeId=' . $challengeId, false, $context);
1576+
$data = file_get_contents($topcoderChallengeApiUrl . $challengeId, false, $context);
15391577
if ($data === false) {
15401578
// Handle errors (e.g. 404 and others)
15411579
self::log('Couldn\'t get challenge: no token', ['headers'=> json_encode($http_response_header)]);
@@ -1549,6 +1587,35 @@ private static function loadChallenge($challengeId) {
15491587
return null;
15501588
}
15511589

1590+
/**
1591+
* Load challenge from cache
1592+
* @param $challengeID
1593+
* @return false|mixed
1594+
*/
1595+
private static function getChallengeFromCache($challengeID) {
1596+
if(!Gdn_Cache::activeEnabled()) {
1597+
return false;
1598+
}
1599+
1600+
$handleKey = formatString(self::CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE, ['ChallengeID' => $challengeID]);
1601+
if(!Gdn::cache()->exists($handleKey)) {
1602+
return false;
1603+
}
1604+
$challenge = Gdn::cache()->get($handleKey);
1605+
if ($challenge === Gdn_Cache::CACHEOP_FAILURE) {
1606+
return false;
1607+
}
1608+
return $challenge;
1609+
}
1610+
1611+
private static function topcoderChallengeCache($challengeID, $challenge, $expirationTime = self::CACHE_DEFAULT_EXPIRY_TIME) {
1612+
$challengeKey = formatString(self::CACHE_TOPCODER_KEY_TOPCODER_CHALLENGE, ['ChallengeID' => $challengeID]);
1613+
return Gdn::cache()->store($challengeKey , $challenge, [
1614+
Gdn_Cache::FEATURE_EXPIRY => $expirationTime
1615+
]);
1616+
}
1617+
1618+
15521619
/**
15531620
* Get a Topcoder Roles
15541621
*
@@ -1761,13 +1828,15 @@ public static function getUserPhotoUrl($user) {
17611828
// Set Topcoder Project Roles Data for a challenge
17621829
private function setTopcoderProjectData($sender, $challengeID) {
17631830
if($challengeID) {
1831+
$challenge = $this->getChallenge($challengeID);
17641832
$resources = $this->getChallengeResources($challengeID);
17651833
$roleResources = $this->getRoleResources();
17661834
$currentProjectRoles = $this->getTopcoderProjectRoles(Gdn::session()->User, $resources, $roleResources);
17671835
if($currentProjectRoles) {
17681836
$currentProjectRoles = array_map('strtolower',$currentProjectRoles);
17691837
}
17701838

1839+
$sender->Data['Challenge'] = $challenge;
17711840
$sender->Data['ChallengeResources'] = $resources;
17721841
$sender->Data['ChallengeRoleResources'] = $roleResources;
17731842
$sender->Data['ChallengeCurrentUserProjectRoles'] = $currentProjectRoles;
@@ -1777,7 +1846,7 @@ private function setTopcoderProjectData($sender, $challengeID) {
17771846
// }
17781847
self::log('setTopcoderProjectData', ['ChallengeID' => $challengeID, 'currentUser' => $currentProjectRoles,
17791848
'Topcoder Resources' => $resources , 'Topcoder RoleResources'
1780-
=> $roleResources,]);
1849+
=> $roleResources, $challenge =>$challenge]);
17811850
}
17821851
}
17831852

0 commit comments

Comments
 (0)