]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Cache results for attachments
authorEvan Prodromou <evan@status.net>
Thu, 7 Apr 2011 03:33:35 +0000 (23:33 -0400)
committerEvan Prodromou <evan@status.net>
Thu, 7 Apr 2011 03:33:35 +0000 (23:33 -0400)
classes/Notice.php

index 8e0307b19d7730c2ef44059133353c65f2ef965d..b948e9de733af6708183ab223d49196c25482d40 100644 (file)
@@ -719,18 +719,33 @@ class Notice extends Memcached_DataObject
     }
 
     function attachments() {
-        // XXX: cache this
-        $att = array();
-        $f2p = new File_to_post;
-        $f2p->post_id = $this->id;
-        if ($f2p->find()) {
-            while ($f2p->fetch()) {
-                $f = File::staticGet($f2p->file_id);
-                if ($f) {
-                    $att[] = clone($f);
+
+        $keypart = sprintf('notice:file_ids:%d', $this->id);
+
+        $idstr = self::cacheGet($keypart);
+
+        if ($idstr !== false) {
+            $ids = explode(',', $idstr);
+        } else {
+            $f2p = new File_to_post;
+            $f2p->post_id = $this->id;
+            if ($f2p->find()) {
+                while ($f2p->fetch()) {
+                    $ids[] = $f2p->file_id;
                 }
             }
+            self::cacheSet($keypart, implode(',', $ids));
         }
+
+        $att = array();
+
+        foreach ($ids as $id) {
+            $f = File::staticGet('id', $id);
+            if (!empty($f)) {
+                $att[] = clone($f);
+            }
+        }
+
         return $att;
     }