From 8701ddfc840fd608b8ed1a3c5192e5507c21f671 Mon Sep 17 00:00:00 2001
From: Fabian Franz <github@fabian-franz.de>
Date: Thu, 10 Jul 2014 21:21:20 +0200
Subject: [PATCH] Issue #2299547 by Fabianx: [Performance] Implement
 render_cache module for comments.

---
 drupalorg/drupalorg.module | 51 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/drupalorg/drupalorg.module b/drupalorg/drupalorg.module
index 129ae42..35a24f4 100755
--- a/drupalorg/drupalorg.module
+++ b/drupalorg/drupalorg.module
@@ -2818,3 +2818,54 @@ function drupalorg_user_update(&$edit, $account, $category) {
     }
   }
 }
+
+
+/**
+ * Implements hook_render_cache_entity_default_cache_info_alter().
+ */
+function drupalorg_render_cache_entity_default_cache_info_alter(&$cache_info, $context) {
+  // Disable entity_view() render caching by default to avoid side effects.
+  $cache_info['granularity'] = DRUPAL_NO_CACHE;
+}
+
+/**
+ * Implements hook_render_cache_entity_cache_info_alter().
+ */
+function drupalorg_render_cache_entity_cache_info_alter(&$cache_info, $entity, $context) {
+  // Allow to disable render caching via variable.
+  if (!variable_get('drupalorg_render_cache_enabled', TRUE)) {
+    return;
+  }
+
+  // Always render_to_markup if possible.
+  // Yes, please save rendered data.
+  $cache_info['render_cache_render_to_markup'] = TRUE;
+
+  // Check if render caching should be enabled for comments.
+  if (variable_get('drupalorg_render_cache_comment_enabled', TRUE)
+      && $context['entity_type'] == 'comment'
+      && $context['view_mode'] == 'full') {
+    $cache_info['granularity'] = DRUPAL_CACHE_PER_ROLE;
+    // Store that this needs special comment processing.
+    $cache_info['drupalorg_comment_processing'] = TRUE;
+  }
+}
+
+/**
+ * Implements hook_render_cache_entity_hash_alter().
+ */
+function drupalorg_render_cache_entity_hash_alter(&$hash, $entity, $cache_info, $context) {
+  if (!empty($cache_info['drupalorg_comment_processing'])) {
+    // Comment is displaying users, so need to add this to the hash, entity is already added.
+    $hash['comment_uid'] = $entity->uid;
+    $account = user_load($entity->uid);
+    if (!empty($account->uid)) {
+      $hash['comment_uid_modified'] = entity_modified_last('user', $account);
+    }
+
+    $hash['is_viewer'] = $entity->uid == $GLOBALS['user']->uid;
+
+    $node = node_load($entity->nid);
+    $hash['is_author'] = $entity->uid == $node->uid;
+  }
+}
-- 
1.8.5.4

