From 525fca8b379b5852a0d99ec93fbdd7178cf6955c Mon Sep 17 00:00:00 2001
From: Moshe Weitzman <weitzman@tejasa.com>
Date: Sat, 16 Apr 2011 23:46:56 -0400
Subject: [PATCH] Add memory versions of cache_get and cache_set. Reduces some unneeded caching when using DB backend.

---
 includes/cache.inc       |   44 ++++++++++++++++++++++++++++++++++++++++++++
 modules/node/node.module |    4 ++--
 2 files changed, 46 insertions(+), 2 deletions(-)

diff --git a/includes/cache.inc b/includes/cache.inc
index 8666874..1c0feef 100644
--- a/includes/cache.inc
+++ b/includes/cache.inc
@@ -64,6 +64,23 @@ function cache_get_multiple(array &$cids, $bin = 'cache') {
   return _cache_get_object($bin)->getMultiple($cids);
 }
 
+/*
+ * A wrapper around cache_get(). Will only proceed if the active cache class
+ * declares itself as in-memory.
+ *
+ * Use this when you prefer to retrieve from cache but not at the expense of a
+ * fast database query.
+ *
+ * @see cache_get()
+ */
+function cache_get_memory($cid, $bin = 'cache') {
+  return cache_is_memory($bin) ? cache_get($cid, $bin) : FALSE;
+}
+
+function cache_is_memory($bin = 'cache') {
+  return _cache_get_object($bin)->isMemory();
+}
+
 /**
  * Store data in the persistent cache.
  *
@@ -137,6 +154,21 @@ function cache_set($cid, $data, $bin = 'cache', $expire = CACHE_PERMANENT) {
   return _cache_get_object($bin)->set($cid, $data, $expire);
 }
 
+/*
+ * A wrapper around cache_set(). Will only proceed if the active cache class
+ * declares itself as in-memory.
+ *
+ * Use this when you prefer to retrieve from cache but not at the expense of a
+ * fast database query.
+ *
+ * @see cache_set()
+ */
+function cache_set_memory($cid, $data, $bin = 'cache', $expire = CACHE_PERMANENT) {
+  if (cache_is_memory($bin)) {
+    return cache_set($cid, $data, $bin, $expire);
+  }
+}
+
 /**
  * Expire data from the cache.
  *
@@ -255,6 +287,14 @@ interface DrupalCacheInterface {
    function getMultiple(&$cids);
 
   /**
+   * Declare that checking the cache is a virtually free operation (e.g. memcache)
+   *
+   * @return boolean
+   *   TRUE if storage is in-memory.
+   */
+  function isMemory();
+
+  /**
    * Store data in the persistent cache.
    *
    * @param $cid
@@ -350,6 +390,10 @@ class DrupalDatabaseCache implements DrupalCacheInterface {
     }
   }
 
+  function isMemory() {
+    return FALSE;
+  }
+
   /**
    * Garbage collection for get() and getMultiple().
    *
diff --git a/modules/node/node.module b/modules/node/node.module
index a59a5b1..3f576f3 100644
--- a/modules/node/node.module
+++ b/modules/node/node.module
@@ -677,7 +677,7 @@ function _node_types_build($rebuild = FALSE) {
     if (isset($_node_types)) {
       return $_node_types;
     }
-    if ($cache = cache_get($cid)) {
+    if ($cache = cache_get_memory($cid)) {
       $_node_types = $cache->data;
       return $_node_types;
     }
@@ -739,7 +739,7 @@ function _node_types_build($rebuild = FALSE) {
 
   asort($_node_types->names);
 
-  cache_set($cid, $_node_types);
+  cache_set_memory($cid, $_node_types);
 
   return $_node_types;
 }
-- 
1.7.4.1

