From 85db7b9ef14e9ff7c54c0d30f14bfdd8002d1ac7 Mon Sep 17 00:00:00 2001 From: Florian Thoma Date: Wed, 18 Dec 2019 09:48:26 +1100 Subject: [PATCH 1/2] move registration of classes to dev/build using cache --- _config/config.yml | 6 +++ src/Extensions/Attributable.php | 11 ---- src/Extensions/Attribute.php | 5 -- src/Model/Attribution.php | 95 ++++++++++++++++++++++++++------- 4 files changed, 81 insertions(+), 36 deletions(-) diff --git a/_config/config.yml b/_config/config.yml index 6dbbba5..eab2020 100644 --- a/_config/config.yml +++ b/_config/config.yml @@ -1,3 +1,9 @@ --- Name: fromholdio-attributable --- + +SilverStripe\Core\Injector\Injector: + Psr\SimpleCache\CacheInterface.AttributionCache: + factory: SilverStripe\Core\Cache\CacheFactory + constructor: + namespace: "AttributionCache" diff --git a/src/Extensions/Attributable.php b/src/Extensions/Attributable.php index 5b3eb45..df15e1c 100644 --- a/src/Extensions/Attributable.php +++ b/src/Extensions/Attributable.php @@ -26,17 +26,6 @@ class Attributable extends DataExtension 'Attributions' ]; - public static function add_to_class($class, $extensionClass, $args = null) - { - $classInst = $class::singleton(); - if ($classInst->hasMethod('isAttributeFilterOnly')) { - if ($classInst->isAttributeFilterOnly()) { - return; - } - } - Attribution::register_object($class); - } - public function getAttributes($attrClass, $scopeObject = null) { if (!$this->owner->ID) { diff --git a/src/Extensions/Attribute.php b/src/Extensions/Attribute.php index 22e2cc9..a3e9002 100644 --- a/src/Extensions/Attribute.php +++ b/src/Extensions/Attribute.php @@ -22,11 +22,6 @@ class Attribute extends DataExtension 'AttrAttributions' => Attribution::class . '.Attribute' ]; - public static function add_to_class($class, $extensionClass, $args = null) - { - Attribution::register_attribute($class); - } - public function updateCMSFields(FieldList $fields) { $fields->removeByName('AttrAttributions'); diff --git a/src/Model/Attribution.php b/src/Model/Attribution.php index ae064f2..75f7277 100644 --- a/src/Model/Attribution.php +++ b/src/Model/Attribution.php @@ -4,12 +4,15 @@ use Fromholdio\Attributable\Extensions\Attributable; use Fromholdio\Attributable\Extensions\Attribute; +use Psr\SimpleCache\CacheInterface; use SilverStripe\Core\ClassInfo; +use SilverStripe\Core\Flushable; +use SilverStripe\Core\Injector\Injector; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObjectInterface; use SilverStripe\Versioned\Versioned; -class Attribution extends DataObject +class Attribution extends DataObject implements Flushable { private static $table_name = 'Attribution'; private static $singular_name = 'Attribution'; @@ -29,25 +32,15 @@ class Attribution extends DataObject 'Object' => DataObject::class ]; - protected static $attributes = []; - protected static $objects = []; - - public static function register_attribute($class) - { - self::validate_attribute($class); - self::$attributes[$class] = $class; - } - - public static function register_object($class) - { - self::validate_object($class); - self::$objects[$class] = $class; - } - public static function get_attributes() { - $classes = self::$attributes; - return $classes; + // retrieve from cache + $cache = self::get_cache(); + $cacheKey = self::get_attributes_cache_key(); + if ($cache->has($cacheKey)) { + return $cache->get($cacheKey); + } + return null; } public static function get_attributes_for_dropdown($usePlural = false) @@ -80,8 +73,13 @@ public static function get_attribute_by_url_segment($urlSegment) public static function get_objects() { - $classes = self::$objects; - return $classes; + // retrieve from cache + $cache = self::get_cache(); + $cacheKey = self::get_objects_cache_key(); + if ($cache->has($cacheKey)) { + return $cache->get($cacheKey); + } + return null; } public static function validate_object($class) @@ -189,4 +187,61 @@ public function onBeforeWrite() $this->AttributeKey = $this->AttributeClass . '|' . $this->AttributeID; $this->ObjectKey = $this->ObjectClass . '|' . $this->ObjectID; } + + /** + * This function is triggered early in the request if the "flush" query + * parameter has been set. Each class that implements Flushable implements + * this function which looks after it's own specific flushing functionality. + * + * @see FlushMiddleware + */ + public static function flush() + { + self::get_cache()->clear(); + + // build attributes cache + $attributes = []; + $classes = ClassInfo::subclassesFor(DataObject::class); + foreach ($classes as $class) { + if ($class::has_extension(Attribute::class)) { + self::validate_attribute($class); + $attributes[$class] = $class; + } + } + $cache = self::get_cache(); + $cacheKey = self::get_attributes_cache_key(); + $cache->set($cacheKey, $attributes); + + // build objects cache + $objects = []; + $classes = ClassInfo::subclassesFor(DataObject::class); + foreach ($classes as $class) { + if ($class::has_extension(Attributable::class)) { + $classInst = $class::singleton(); + if ($classInst->hasMethod('isAttributeFilterOnly')) { + if ($classInst->isAttributeFilterOnly()) { + break; + } + } + self::validate_object($class); + $objects[$class] = $class; + } + } + $cache = self::get_cache(); + $cacheKey = self::get_objects_cache_key(); + $cache->set($cacheKey, $objects); + + } + + private static function get_cache() { + return Injector::inst()->get(CacheInterface::class . '.AttributionCache'); + } + + private static function get_attributes_cache_key() { + return implode('-', ['Attributes']); + } + + private static function get_objects_cache_key() { + return implode('-', ['Objects']); + } } From 2a54c1a1bf05d676a0d1d1f590d059cc09fc739f Mon Sep 17 00:00:00 2001 From: Florian Thoma Date: Thu, 19 Dec 2019 09:10:20 +1100 Subject: [PATCH 2/2] add fallback for building of cache --- src/Model/Attribution.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Model/Attribution.php b/src/Model/Attribution.php index 75f7277..e88326d 100644 --- a/src/Model/Attribution.php +++ b/src/Model/Attribution.php @@ -5,6 +5,7 @@ use Fromholdio\Attributable\Extensions\Attributable; use Fromholdio\Attributable\Extensions\Attribute; use Psr\SimpleCache\CacheInterface; +use SilverStripe\Control\Middleware\FlushMiddleware; use SilverStripe\Core\ClassInfo; use SilverStripe\Core\Flushable; use SilverStripe\Core\Injector\Injector; @@ -37,6 +38,9 @@ public static function get_attributes() // retrieve from cache $cache = self::get_cache(); $cacheKey = self::get_attributes_cache_key(); + if (!$cache->has($cacheKey)) { + self::build_cache(); + } if ($cache->has($cacheKey)) { return $cache->get($cacheKey); } @@ -76,6 +80,9 @@ public static function get_objects() // retrieve from cache $cache = self::get_cache(); $cacheKey = self::get_objects_cache_key(); + if (!$cache->has($cacheKey)) { + self::build_cache(); + } if ($cache->has($cacheKey)) { return $cache->get($cacheKey); } @@ -198,7 +205,10 @@ public function onBeforeWrite() public static function flush() { self::get_cache()->clear(); - + self::build_cache(); + } + + private static function build_cache() { // build attributes cache $attributes = []; $classes = ClassInfo::subclassesFor(DataObject::class); @@ -230,7 +240,6 @@ public static function flush() $cache = self::get_cache(); $cacheKey = self::get_objects_cache_key(); $cache->set($cacheKey, $objects); - } private static function get_cache() {