Permalink
Browse files

add cache groups attribute to models; invalidate Post model cache gro…

…ups when model is saved
  • Loading branch information...
1 parent 0c9baa3 commit a86ee5d0f07bab33095836de67a9dde4d437ef75 Ryan Nagle committed Nov 27, 2012
Showing with 11 additions and 5 deletions.
  1. +2 −0 models.php
  2. +9 −5 wp/models.php
View
@@ -37,6 +37,8 @@ class Model {
public $json_fields = array();
// Attributes that can be updated over the wire
public $editable_json_fields = array();
+ // WP Object Cache groups this model is associated with
+ public $cache_groups = array();
public static $collection = 'mtv\models\Collection';
View
@@ -80,6 +80,10 @@
* Returns a generated excerpt. Simliar to how WordPress makes excerpts in The Loop.
**/
class Post extends Model {
+
+ public $cache_groups = array(
+ 'posts', 'post_meta', 'post_ancestors', 'post_format_relationships');
+
public function __toString() {
return $this->attributes['post_title'];
}
@@ -130,12 +134,12 @@ public function save() {
restore_current_blog();
$this->id = $postid;
- $this->fetch(); // We refresh the post in case any filters changed the content
- # TODO: WordPress seems to cache the post format and will give us
- # a stale value when we fetch
- if ( isset($post_format) )
- $this->post_format = $post_format;
+ # Invalidate cached data for this Post
+ foreach ( $this->cache_groups as $cache_group )
+ wp_cache_delete($this->id, $cache_group);
+
+ $this->fetch(); // We refresh the post in case any filters changed the content
}
public function fetch() {

0 comments on commit a86ee5d

Please sign in to comment.