[TASK] Allow generated BE user avatars to be cached 67/51467/7
authorClaus Due <claus@namelesscoder.net>
Mon, 30 Jan 2017 11:32:45 +0000 (12:32 +0100)
committerBenni Mack <benni@typo3.org>
Tue, 7 Feb 2017 16:54:10 +0000 (17:54 +0100)
Given the assumption that this particular resource is not
one that changes very often one can benefit from storing
this in the cache_runtime in order to avoid fetching
the same avator more than one in a request.

Change-Id: Idc39c59dc8213f861ef4da7c9da4f5a95cfab9ac
Resolves: #79547
Releases: master
Reviewed-on: https://review.typo3.org/51467
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Tested-by: Joerg Boesche <typo3@joergboesche.de>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/backend/Classes/Backend/Avatar/Avatar.php

index 4b4a4ae..a4ec7ef 100644 (file)
@@ -14,6 +14,8 @@ namespace TYPO3\CMS\Backend\Backend\Avatar;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Cache\CacheManager;
+use TYPO3\CMS\Core\Cache\Frontend\VariableFrontend;
 use TYPO3\CMS\Core\Imaging\Icon;
 use TYPO3\CMS\Core\Imaging\IconFactory;
 use TYPO3\CMS\Core\Service\DependencyOrderingService;
@@ -34,20 +36,6 @@ class Avatar
     protected $avatarProviders = [];
 
     /**
-     * Construct
-     */
-    public function __construct()
-    {
-        $this->validateSortAndInitiateAvatarProviders();
-        $this->view = $this->getFluidTemplateObject();
-    }
-
-    /**
-     * @var StandaloneView
-     */
-    protected $view;
-
-    /**
      * Render avatar tag
      *
      * @param array $backendUser be_users record
@@ -57,26 +45,43 @@ class Avatar
      */
     public function render(array $backendUser = null, int $size = 32, bool $showIcon = false)
     {
+
         if (!is_array($backendUser)) {
             $backendUser = $this->getBackendUser()->user;
         }
 
-        // Icon
-        $icon = '';
-        if ($showIcon) {
-            $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
-            $icon = $iconFactory->getIconForRecord('be_users', $backendUser, Icon::SIZE_SMALL)->render();
-        }
+        $cacheId = 'avatar_' . md5(
+                $backendUser['uid'] . '/' .
+                (string)$size . '/' .
+                (string)$showIcon);
+
+        $avatar = static::getCache()->get($cacheId);
 
-        $image = $this->getImgTag($backendUser, $size);
+        if (!$avatar) {
 
-        $this->view->assignMultiple([
-                'image' => $image,
-                'icon' => $icon
-            ]
-        );
+            $this->validateSortAndInitiateAvatarProviders();
+            $view = $this->getFluidTemplateObject();
 
-        return $this->view->render();
+            // Icon
+            $icon = '';
+            if ($showIcon) {
+                $iconFactory = GeneralUtility::makeInstance(IconFactory::class);
+                $icon = $iconFactory->getIconForRecord('be_users', $backendUser, Icon::SIZE_SMALL)->render();
+            }
+
+            $image = $this->getImgTag($backendUser, $size);
+
+            $view->assignMultiple(
+                [
+                    'image' => $image,
+                    'icon' => $icon
+                ]
+            );
+            $avatar = $view->render();
+            static::getCache()->set($cacheId, $avatar);
+        }
+
+        return $avatar;
     }
 
     /**
@@ -196,4 +201,14 @@ class Avatar
         $view->getRequest()->setControllerExtensionName('Backend');
         return $view;
     }
+
+    /**
+     * @return VariableFrontend
+     */
+    protected function getCache()
+    {
+        /** @var CacheManager $manager */
+        $manager = GeneralUtility::makeInstance(CacheManager::class);
+        return $manager->getCache('cache_runtime');
+    }
 }