Commit e2a002b9943700804b45968eab8dc1cbd33f08b5

Authored by Benjamin Renard
1 parent b4fbe992

Save parsed timeRestriction in cache

Showing 1 changed file with 20 additions and 17 deletions   Show diff stats
php/classes/UserMgr.php
... ... @@ -161,7 +161,7 @@ class UserMgr
161 161 $restrictionFilePath = datasetsTimeRestrictionJson;
162 162 if (file_exists($restrictionFilePath)) {
163 163 // Re-use the cache file if it's not older than 3600s.
164   - if (abs(time() - filemtime($restrictionFilePath)) > 3600) {
  164 + if (abs(time() - filemtime($restrictionFilePath)) < 3600) {
165 165 $fileContent = file_get_contents($restrictionFilePath);
166 166 if (!empty($fileContent)) {
167 167 $this->datasetsTimeRestriction = json_decode($fileContent, TRUE);
... ... @@ -177,6 +177,25 @@ class UserMgr
177 177 $this->datasetsTimeRestriction = $this->amdaClient->getDatasetsWithTimeRestriction();
178 178  
179 179 if ($this->datasetsTimeRestriction !== FALSE) {
  180 + foreach ($this->datasetsTimeRestriction as &$datasetTimeRestriction) {
  181 + $timeRestriction = $datasetTimeRestriction['restriction'];
  182 + //Parse time restriction
  183 + $n = sscanf($timeRestriction, "%04d-%02d-%02d", $yy, $mm, $dd);
  184 + if ($n != 3) {
  185 + $n = sscanf($timeRestriction, "%d", $ndays);
  186 + if ($n != 1) {
  187 + //Cannot parse the time restriction
  188 + $timeRestriction = '';
  189 + }
  190 + else {
  191 + $timeRestriction = date("Y-m-d", strtotime("-$ndays days"))."T23:59:59.999Z";
  192 + }
  193 + }
  194 + else {
  195 + $timeRestriction .= "T23:59:59.999Z";
  196 + }
  197 + $datasetTimeRestriction['restriction'] = $timeRestriction;
  198 + }
180 199 $content = json_encode($this->datasetsTimeRestriction);
181 200 $this->datasetsTimeRestrictionCheckSum = md5($content);
182 201 file_put_contents($restrictionFilePath, $content);
... ... @@ -715,22 +734,6 @@ class UserMgr
715 734 if ($nodes->length > 0) {
716 735 foreach ($nodes as $node) {
717 736 $timeRestriction = $datasetTimeRestriction['restriction'];
718   - //Parse time restriction
719   - $n = sscanf($timeRestriction, "%04d-%02d-%02d", $yy, $mm, $dd);
720   - if ($n != 3) {
721   - $n = sscanf($timeRestriction, "%d", $ndays);
722   - if ($n != 1) {
723   - //Cannot parse the time restriction
724   - $timeRestriction = '';
725   - }
726   - else {
727   - $timeRestriction = date("Y-m-d", strtotime("-$ndays days"))."T23:59:59.999Z";
728   - }
729   - }
730   - else {
731   - $timeRestriction .= "T23:59:59.999Z";
732   - }
733   -
734 737 if (!empty($timeRestriction)) {
735 738 $node->setAttribute('timeRestriction', $timeRestriction);
736 739 }
... ...