Browse Source

Figured out how to cache last posts; we had to load topic and user before we cached cuz they were dynamically loaded relationships.

RJackson 11 years ago
parent
commit
9e9148b48e
2 changed files with 39 additions and 13 deletions
  1. 30 10
      flaskbb/forum/models.py
  2. 9 3
      flaskbb/user/models.py

+ 30 - 10
flaskbb/forum/models.py

@@ -197,33 +197,47 @@ class Topic(db.Model):
             filter(Post.topic_id == self.id).\
             count()
 
-    #@cache.memoize(timeout=sys.maxint)  # TODO:  DetachedInstanceError if we return a Flask-SQLAlchemy model.
+    @cache.memoize(timeout=sys.maxint)
     def get_first_post(self):
         """
         Returns the first post within the current topic.
         """
-        return Post.query.\
+
+        post = Post.query.\
             filter(Post.topic_id == self.id).\
             order_by(Post.date_created.asc()).\
             first()
 
-    #@cache.memoize(timeout=sys.maxint)  # TODO:  DetachedInstanceError if we return a Flask-SQLAlchemy model.
+        # Load the topic and user before we cache
+        post.topic
+        post.user
+
+        return post
+
+    @cache.memoize(timeout=sys.maxint)
     def get_last_post(self):
         """
         Returns the latest post within the current topic.
         """
-        return Post.query.\
+
+        post = Post.query.\
             filter(Post.topic_id == self.id).\
             order_by(Post.date_created.desc()).\
             first()
 
+        # Load the topic and user before we cache
+        post.topic
+        post.user
+
+        return post
+
     def invalidate_cache(self):
         """
         Invalidates this objects cached metadata.
         """
         cache.delete_memoized(self.get_post_count, self)
-        #cache.delete_memoized(self.get_first_post, self) # TODO:  Cannot use til we can cache this object.
-        #cache.delete_memoized(self.get_last_post, self) # TODO:  Cannot use til we can cache this object.
+        cache.delete_memoized(self.get_first_post, self)
+        cache.delete_memoized(self.get_last_post, self)
 
 
 class Forum(db.Model):
@@ -338,7 +352,7 @@ class Forum(db.Model):
                 filter(Topic.forum_id == self.id).\
                 count()
 
-    #@cache.memoize(timeout=sys.maxint)  # TODO:  DetachedInstanceError if we return a Flask-SQLAlchemy model.
+    @cache.memoize(timeout=sys.maxint)
     def get_last_post(self, include_children=True):
         """
         Returns the latest post within the current forum or it's children.
@@ -346,18 +360,24 @@ class Forum(db.Model):
         """
 
         if include_children:
-            return Post.query.\
+            post = Post.query.\
                 filter(Post.topic_id == Topic.id). \
                 filter(Topic.forum_id.in_(get_forum_ids(self))). \
                 order_by(Post.date_created.desc()). \
                 first()
         else:
-            return Post.query.\
+            post = Post.query.\
                 filter(Post.topic_id == Topic.id).\
                 filter(Topic.forum_id == self.id).\
                 order_by(Post.date_created.desc()).\
                 first()
 
+        # Load the topic and user before we cache
+        post.topic
+        post.user
+
+        return post
+
     def invalidate_cache(self):
         """
         Invalidates this objects, and it's parents', cached metadata.
@@ -366,7 +386,7 @@ class Forum(db.Model):
         while _forum.parent:
             cache.delete_memoized(self.get_post_count, _forum)
             cache.delete_memoized(self.get_topic_count, _forum)
-            #cache.delete_memoized(self.get_last_post, _forum) # TODO:  Cannot use til we can cache this object.
+            cache.delete_memoized(self.get_last_post, _forum)
             _forum = _forum.parent
 
     # Class methods

+ 9 - 3
flaskbb/user/models.py

@@ -307,20 +307,26 @@ class User(db.Model, UserMixin):
         return Post.query.filter(Post.user_id == self.id).\
             count()
 
-    # @cache.memoize(timeout=sys.maxint)  # TODO:  DetachedInstanceError if we return a Flask-SQLAlchemy model.
+    @cache.memoize(timeout=sys.maxint)
     def get_last_post(self):
         """
         Returns the latest post from the user
         """
-        return Post.query.filter(Post.user_id == self.id).\
+        post = Post.query.filter(Post.user_id == self.id).\
             order_by(Post.date_created.desc()).first()
 
+        # Load the topic and user before we cache
+        post.topic
+        post.user
+
+        return post
+
     def invalidate_cache(self):
         """
         Invalidates this objects cached metadata.
         """
         cache.delete_memoized(self.get_post_count, self)
-        #cache.delete_memoized(self.get_last_post, self) # TODO:  Cannot use til we can cache this object.
+        cache.delete_memoized(self.get_last_post, self)
 
 
 class Guest(AnonymousUserMixin):