From 2481b246c69378c582bdfc14e393ed9eab1ebf5b Mon Sep 17 00:00:00 2001 From: Sebastian Marcet Date: Mon, 13 Mar 2017 21:28:33 -0300 Subject: [PATCH] Updated cache evict policy on data updates Change-Id: I2b4ea68e7b9b198239c1055099b0d7159db4291c --- .../SummitEntityEventSerializer.php | 4 +++ .../EntityEvents/Types/EntityEventType.php | 36 +++++++++++++++++++ .../Types/EntityEventTypeFactory.php | 2 ++ .../Types/GenericSummitEntityEventType.php | 5 ++- .../PresentationCategoryEntityEventType.php | 1 + ...esentationCategoryGroupEntityEventType.php | 1 + .../PresentationMaterialEntityEventType.php | 1 + .../PresentationSpeakerEntityEventType.php | 1 + .../Types/SummitEntityEventType.php | 1 + .../Types/SummitEventEntityEventType.php | 1 + .../Types/SummitEventTypeEntityEventType.php | 1 + .../Types/SummitLocationEntityEventType.php | 1 + .../Types/SummitLocationImageEventType.php | 1 + .../Types/SummitTicketTypeEntityEventType.php | 1 + .../Types/SummitVenueFloorEntityEventType.php | 1 + .../SummitWIFIConnectionEntityEventType.php | 1 + tests/OAuth2SummitApiTest.php | 10 +++--- 17 files changed, 62 insertions(+), 7 deletions(-) diff --git a/app/ModelSerializers/SummitEntityEventSerializer.php b/app/ModelSerializers/SummitEntityEventSerializer.php index f97800b6..172ff9ef 100644 --- a/app/ModelSerializers/SummitEntityEventSerializer.php +++ b/app/ModelSerializers/SummitEntityEventSerializer.php @@ -48,6 +48,10 @@ final class SummitEntityEventSerializer extends SilverStripeSerializer $params ); } + + if($values['class_name'] == 'PresentationType') + $values['class_name'] = 'SummitEventType'; + return $values; } } \ No newline at end of file diff --git a/app/Models/Foundation/Summit/EntityEvents/Types/EntityEventType.php b/app/Models/Foundation/Summit/EntityEvents/Types/EntityEventType.php index 6ca93a7c..9fe0cec2 100644 --- a/app/Models/Foundation/Summit/EntityEvents/Types/EntityEventType.php +++ b/app/Models/Foundation/Summit/EntityEvents/Types/EntityEventType.php @@ -12,8 +12,11 @@ * limitations under the License. **/ +use Illuminate\Support\Facades\Log; use models\summit\SummitEntityEvent; use models\utils\IEntity; +use LaravelDoctrine\ORM\Facades\Registry; +use Doctrine\ORM\EntityManager; /** * Class EntityEventType @@ -21,6 +24,8 @@ use models\utils\IEntity; */ abstract class EntityEventType implements IEntityEventType { + const EntityManager = 'ss'; + /** * @var SummitEntityEvent */ @@ -46,4 +51,35 @@ abstract class EntityEventType implements IEntityEventType * @return IEntity|null */ abstract protected function registerEntity(); + + /** + * @return EntityManager + */ + protected function getEM(){ + return Registry::getManager(self::EntityManager); + } + + protected function getLocalClassName(){ + $class_name = $this->entity_event->getEntityClassName(); + switch ($class_name){ + case 'MySchedule': + case 'MyFavorite': + return 'models\summit\SummitEvent'; + break; + case 'PresentationType': + return 'models\summit\SummitEventType'; + break; + } + return sprintf('models\summit\%s',$class_name); + } + + protected function evictEntity(){ + $cache = $this->getEM()->getCache(); + $class_name = $this->getLocalClassName(); + + if(!is_null($cache) && !empty($class_name) && $cache->containsEntity($class_name, $this->entity_event->getEntityId())) { + $cache->evictEntity($class_name, $this->entity_event->getEntityId()); + Log::debug(sprintf("class_name % - id %s evicted from 2nd level cache", $class_name, $this->entity_event->getEntityId())); + } + } } \ No newline at end of file diff --git a/app/Models/Foundation/Summit/EntityEvents/Types/EntityEventTypeFactory.php b/app/Models/Foundation/Summit/EntityEvents/Types/EntityEventTypeFactory.php index aa894ddc..e4b01baf 100644 --- a/app/Models/Foundation/Summit/EntityEvents/Types/EntityEventTypeFactory.php +++ b/app/Models/Foundation/Summit/EntityEvents/Types/EntityEventTypeFactory.php @@ -79,6 +79,7 @@ final class EntityEventTypeFactory } break; case 'SummitEventType': + case 'PresentationType': { return new SummitEventTypeEntityEventType($e, $ctx); } @@ -121,6 +122,7 @@ final class EntityEventTypeFactory } break; case 'PresentationCategoryGroup': + case 'PrivatePresentationCategoryGroup': { return new PresentationCategoryGroupEntityEventType($e, $ctx); } diff --git a/app/Models/Foundation/Summit/EntityEvents/Types/GenericSummitEntityEventType.php b/app/Models/Foundation/Summit/EntityEvents/Types/GenericSummitEntityEventType.php index 14fe4504..72f78008 100644 --- a/app/Models/Foundation/Summit/EntityEvents/Types/GenericSummitEntityEventType.php +++ b/app/Models/Foundation/Summit/EntityEvents/Types/GenericSummitEntityEventType.php @@ -1,4 +1,4 @@ -process_ctx->registerEntityEvent($this->entity_event); } + } \ No newline at end of file diff --git a/app/Models/Foundation/Summit/EntityEvents/Types/PresentationCategoryEntityEventType.php b/app/Models/Foundation/Summit/EntityEvents/Types/PresentationCategoryEntityEventType.php index 9889098e..f4985b62 100644 --- a/app/Models/Foundation/Summit/EntityEvents/Types/PresentationCategoryEntityEventType.php +++ b/app/Models/Foundation/Summit/EntityEvents/Types/PresentationCategoryEntityEventType.php @@ -24,6 +24,7 @@ final class PresentationCategoryEntityEventType extends GenericSummitEntityEvent */ protected function registerEntity() { + $this->evictEntity(); $entity = $this->entity_event->getSummit()->getPresentationCategory($this->entity_event->getEntityId()); if(is_null($entity)) return null; $this->entity_event->registerEntity($entity); diff --git a/app/Models/Foundation/Summit/EntityEvents/Types/PresentationCategoryGroupEntityEventType.php b/app/Models/Foundation/Summit/EntityEvents/Types/PresentationCategoryGroupEntityEventType.php index 33244758..7c4f84fb 100644 --- a/app/Models/Foundation/Summit/EntityEvents/Types/PresentationCategoryGroupEntityEventType.php +++ b/app/Models/Foundation/Summit/EntityEvents/Types/PresentationCategoryGroupEntityEventType.php @@ -26,6 +26,7 @@ final class PresentationCategoryGroupEntityEventType extends GenericSummitEntity */ protected function registerEntity() { + $this->evictEntity(); $entity = $this->entity_event->getSummit()->getCategoryGroup($this->entity_event->getEntityId()); if(is_null($entity)) return null; $this->entity_event->registerEntity($entity); diff --git a/app/Models/Foundation/Summit/EntityEvents/Types/PresentationMaterialEntityEventType.php b/app/Models/Foundation/Summit/EntityEvents/Types/PresentationMaterialEntityEventType.php index c0f5b8d1..de948387 100644 --- a/app/Models/Foundation/Summit/EntityEvents/Types/PresentationMaterialEntityEventType.php +++ b/app/Models/Foundation/Summit/EntityEvents/Types/PresentationMaterialEntityEventType.php @@ -29,6 +29,7 @@ final class PresentationMaterialEntityEventType extends GenericSummitEntityEvent if(!isset($metadata['presentation_id'])) return null; $presentation = $this->entity_event->getSummit()->getScheduleEvent(intval($metadata['presentation_id'])); if (is_null($presentation)) return null; + $this->evictEntity(); $material = $presentation->getMaterial($this->entity_event->getEntityId()); if(is_null($material)) return null; $this->entity_event->registerEntity($material); diff --git a/app/Models/Foundation/Summit/EntityEvents/Types/PresentationSpeakerEntityEventType.php b/app/Models/Foundation/Summit/EntityEvents/Types/PresentationSpeakerEntityEventType.php index f4f8b693..dd19e271 100644 --- a/app/Models/Foundation/Summit/EntityEvents/Types/PresentationSpeakerEntityEventType.php +++ b/app/Models/Foundation/Summit/EntityEvents/Types/PresentationSpeakerEntityEventType.php @@ -26,6 +26,7 @@ final class PresentationSpeakerEntityEventType extends GenericSummitEntityEventT */ protected function registerEntity() { + $this->evictEntity(); $entity = $this->entity_event->getSummit()->getSpeaker($this->entity_event->getEntityId()); if(!is_null($entity)) $this->entity_event->registerEntity($entity); diff --git a/app/Models/Foundation/Summit/EntityEvents/Types/SummitEntityEventType.php b/app/Models/Foundation/Summit/EntityEvents/Types/SummitEntityEventType.php index cfcb7278..aad16dfd 100644 --- a/app/Models/Foundation/Summit/EntityEvents/Types/SummitEntityEventType.php +++ b/app/Models/Foundation/Summit/EntityEvents/Types/SummitEntityEventType.php @@ -26,6 +26,7 @@ final class SummitEntityEventType extends GenericSummitEntityEventType */ protected function registerEntity() { + $this->evictEntity(); $summit = $this->entity_event->getSummit(); if(is_null($summit)) return null; if($summit->getId() !== $this->entity_event->getEntityId()) return null; diff --git a/app/Models/Foundation/Summit/EntityEvents/Types/SummitEventEntityEventType.php b/app/Models/Foundation/Summit/EntityEvents/Types/SummitEventEntityEventType.php index ed15e858..d4d9b407 100644 --- a/app/Models/Foundation/Summit/EntityEvents/Types/SummitEventEntityEventType.php +++ b/app/Models/Foundation/Summit/EntityEvents/Types/SummitEventEntityEventType.php @@ -33,6 +33,7 @@ abstract class SummitEventEntityEventType extends EntityEventType * @return SummitEvent|null */ protected function getEntity(){ + $this->evictEntity(); return $this->entity_event->getSummit()->getScheduleEvent($this->entity_event->getEntityId()); } } \ No newline at end of file diff --git a/app/Models/Foundation/Summit/EntityEvents/Types/SummitEventTypeEntityEventType.php b/app/Models/Foundation/Summit/EntityEvents/Types/SummitEventTypeEntityEventType.php index 987a8600..f946f53b 100644 --- a/app/Models/Foundation/Summit/EntityEvents/Types/SummitEventTypeEntityEventType.php +++ b/app/Models/Foundation/Summit/EntityEvents/Types/SummitEventTypeEntityEventType.php @@ -25,6 +25,7 @@ final class SummitEventTypeEntityEventType extends GenericSummitEntityEventType */ protected function registerEntity() { + $this->evictEntity(); $entity = $this->entity_event->getSummit()->getEventType($this->entity_event->getEntityId()); if(!is_null($entity)) $this->entity_event->registerEntity($entity); diff --git a/app/Models/Foundation/Summit/EntityEvents/Types/SummitLocationEntityEventType.php b/app/Models/Foundation/Summit/EntityEvents/Types/SummitLocationEntityEventType.php index 214526f1..bc4dd1b0 100644 --- a/app/Models/Foundation/Summit/EntityEvents/Types/SummitLocationEntityEventType.php +++ b/app/Models/Foundation/Summit/EntityEvents/Types/SummitLocationEntityEventType.php @@ -30,6 +30,7 @@ final class SummitLocationEntityEventType extends GenericSummitEntityEventType $type = $this->entity_event->getType(); // if there is an insert in place, skip it if($type === 'UPDATE' && $this->process_ctx->existsInsertOp($this->entity_event)) return null; + $this->evictEntity(); $entity = $this->entity_event->getSummit()->getLocation($this->entity_event->getEntityId()); if(is_null($entity)) return null; return $this->entity_event->registerEntity($entity); diff --git a/app/Models/Foundation/Summit/EntityEvents/Types/SummitLocationImageEventType.php b/app/Models/Foundation/Summit/EntityEvents/Types/SummitLocationImageEventType.php index 3da2fb69..f7c363ef 100644 --- a/app/Models/Foundation/Summit/EntityEvents/Types/SummitLocationImageEventType.php +++ b/app/Models/Foundation/Summit/EntityEvents/Types/SummitLocationImageEventType.php @@ -29,6 +29,7 @@ final class SummitLocationImageEventType extends GenericSummitEntityEventType if(!isset($metadata['location_id'])) return null; $location = $this->entity_event->getSummit()->getLocation(intval($metadata['location_id'])); if(is_null($location)) return null; + $this->evictEntity(); $entity = $location->getImage($this->entity_event->getEntityId()); if(is_null($entity)) return null; $this->entity_event->registerEntity($entity); diff --git a/app/Models/Foundation/Summit/EntityEvents/Types/SummitTicketTypeEntityEventType.php b/app/Models/Foundation/Summit/EntityEvents/Types/SummitTicketTypeEntityEventType.php index 3119c3f1..74a95fbc 100644 --- a/app/Models/Foundation/Summit/EntityEvents/Types/SummitTicketTypeEntityEventType.php +++ b/app/Models/Foundation/Summit/EntityEvents/Types/SummitTicketTypeEntityEventType.php @@ -25,6 +25,7 @@ final class SummitTicketTypeEntityEventType extends GenericSummitEntityEventType */ protected function registerEntity() { + $this->evictEntity(); $entity = $this->entity_event->getSummit()->getTicketType($this->entity_event->getEntityId()); if(!is_null($entity)) $this->entity_event->registerEntity($entity); diff --git a/app/Models/Foundation/Summit/EntityEvents/Types/SummitVenueFloorEntityEventType.php b/app/Models/Foundation/Summit/EntityEvents/Types/SummitVenueFloorEntityEventType.php index b0c34f2e..196b644f 100644 --- a/app/Models/Foundation/Summit/EntityEvents/Types/SummitVenueFloorEntityEventType.php +++ b/app/Models/Foundation/Summit/EntityEvents/Types/SummitVenueFloorEntityEventType.php @@ -27,6 +27,7 @@ final class SummitVenueFloorEntityEventType extends GenericSummitEntityEventType { $metadata = $this->entity_event->getMetadata(); if(!isset($metadata['venue_id'])) return null; + $this->evictEntity(); $location = $this->entity_event->getSummit()->getLocation(intval($metadata['venue_id'])); if (is_null($location)) return null; $floor = $location->getFloor($this->entity_event->getEntityId()); diff --git a/app/Models/Foundation/Summit/EntityEvents/Types/SummitWIFIConnectionEntityEventType.php b/app/Models/Foundation/Summit/EntityEvents/Types/SummitWIFIConnectionEntityEventType.php index 02b13fa7..a20709d3 100644 --- a/app/Models/Foundation/Summit/EntityEvents/Types/SummitWIFIConnectionEntityEventType.php +++ b/app/Models/Foundation/Summit/EntityEvents/Types/SummitWIFIConnectionEntityEventType.php @@ -26,6 +26,7 @@ class SummitWIFIConnectionEntityEventType extends GenericSummitEntityEventType */ protected function registerEntity() { + $this->evictEntity(); $entity = $this->entity_event->getSummit()->getWifiConnection($this->entity_event->getEntityId()); if(!is_null($entity)) $this->entity_event->registerEntity($entity); diff --git a/tests/OAuth2SummitApiTest.php b/tests/OAuth2SummitApiTest.php index 000ef7c0..e0b1dd8c 100644 --- a/tests/OAuth2SummitApiTest.php +++ b/tests/OAuth2SummitApiTest.php @@ -134,13 +134,13 @@ final class OAuth2SummitApiTest extends ProtectedApiTest $this->assertResponseStatus(200); } - public function testGetCurrentSummit() + public function testGetCurrentSummit($summit_id = 22) { $params = array ( 'expand' => 'schedule', - 'id' => 6 + 'id' => $summit_id ); $headers = array("HTTP_Authorization" => " Bearer " . $this->access_token); @@ -1214,11 +1214,13 @@ final class OAuth2SummitApiTest extends ProtectedApiTest public function testGetEntityEventsFromCurrentSummit() { + //$this->testGetCurrentSummit(22); + $params = array ( - 'id' => '7', + 'id' => '22', 'from_date' => 1460148342, - 'limit' => 100 + 'limit' => 100 ); $headers = array