Query Cache

Added caching to some recurrent queries
* get current active summmit
* get api endpoint scopes

Change-Id: I6f5a9618581bcf29218c60390b48150e91be34fc
This commit is contained in:
Sebastian Marcet 2016-11-15 23:54:46 -03:00
parent 7a3ba45d5f
commit bb20ca1388
5 changed files with 55 additions and 14 deletions

View File

@ -133,8 +133,10 @@ class OAuth2BearerAccessTokenRequestValidator
}
$token_info = $this->token_service->get($access_token_value);
if(!is_null($token_info))
Log::debug(sprintf("token lifetime %s", $token_info->getLifetime()));
//check lifetime
if (is_null($token_info)) {
throw new InvalidGrantTypeException(OAuth2Protocol::OAuth2Protocol_Error_InvalidToken);
@ -157,7 +159,11 @@ class OAuth2BearerAccessTokenRequestValidator
}
//check scopes
Log::debug('checking token scopes ...');
$endpoint_scopes = explode(' ', $endpoint->getScope());
$endpoint_scopes = $endpoint->getScope();
Log::debug(sprintf("endpoint scopes %s", $endpoint_scopes));
Log::debug(sprintf("token scopes %s", $token_info->getScope()));
$endpoint_scopes = explode(' ', $endpoint_scopes);
$token_scopes = explode(' ', $token_info->getScope());
//check token available scopes vs. endpoint scopes

View File

@ -14,6 +14,8 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping AS ORM;
use Illuminate\Support\Facades\Cache as CacheFacade;
use Illuminate\Support\Facades\Config;
/**
* @ORM\Entity(repositoryClass="repositories\resource_server\DoctrineApiEndpointRepository")
@ -248,17 +250,22 @@ class ApiEndpoint extends ResourceServerEntity implements IApiEndpoint
*/
public function getScope()
{
$scope = '';
foreach ($this->scopes as $s)
{
if (!$s->isActive())
{
continue;
}
$scope = $scope .$s->getName().' ';
}
$scope = trim($scope);
return $scope;
return CacheFacade::remember
(
'endpoint_scopes_'.$this->id,
Config::get("cache_regions.region_api_scopes_lifetime", 1140),
function() {
$scope = '';
foreach ($this->scopes as $s) {
if (!$s->isActive()) {
continue;
}
$scope = $scope . $s->getName() . ' ';
}
$scope = trim($scope);
return $scope;
}
);
}
/**

View File

@ -40,7 +40,10 @@ final class DoctrineSummitRepository extends SilverStripeDoctrineRepository impl
->from(\models\summit\Summit::class, "s")
->where('s.active = 1')
->orderBy('s.begin_date','DESC')
->getQuery()->getResult();
->setCacheable(true)
->setCacheRegion('current_summit_region')
->getQuery()
->getResult();
if(count($res) == 0) return null;
return $res[0];
}

17
config/cache_regions.php Normal file
View File

@ -0,0 +1,17 @@
<?php
/**
* Copyright 2016 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
return [
'region_api_scopes_lifetime' => env('CACHE_REGION_API_SCOPES_LIFETIME', 1140),
];

View File

@ -221,7 +221,15 @@ return [
'summit_event_feedback_region' => [
'lifetime' => 300,
'lock_lifetime' => 60
]
],
'current_summit_region' => [
'lifetime' => 600,
'lock_lifetime' => 60
],
'resource_server_region' => [
'lifetime' => 3600,
'lock_lifetime' => 60
],
],
'log_enabled' => true,
'file_lock_region_directory' => '/tmp'