Title: Stale cache on wp_count_posts
Last modified: December 14, 2023

---

# Stale cache on wp_count_posts

 *  Resolved [alexl75](https://wordpress.org/support/users/alexl75/)
 * (@alexl75)
 * [2 years, 5 months ago](https://wordpress.org/support/topic/stale-cache-on-wp_count_posts/)
 * Hi there,
 * I noticed that the data for post counts gets stale over time on my site. I use
   the Redis Object Cache. Specifically, this is for WooCommerce orders (`shop_order`)
 * If I force a cache invalidation [here](https://github.com/itthinx/groups/blob/b9ffa5b76e3420576b2e525e9333a74205e4d266/lib/access/class-groups-post-access.php#L876)
   in`Groups_Post_Access->wp_count_posts` the counts get recalculated properly.
 * I couldn’t find where the count cache gets cleared when posts transition. It 
   looks like it’s [here](https://github.com/WordPress/WordPress/blob/8361461aa03468d8c8bff53e228aa805c5ab0e0d/wp-includes/post.php#L7504)
   in the core handling.
 * Is this a bug or am I missing something ?
 * Thanks!
   Alex

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [gtsiokos](https://wordpress.org/support/users/gtsiokos/)
 * (@gtsiokos)
 * [2 years, 5 months ago](https://wordpress.org/support/topic/stale-cache-on-wp_count_posts/#post-17291681)
 * Hey Alex,
 * Based on your findings, is it possible that your Redis cache implementation doesn’t
   support event-triggered invalidation? Since Groups finds a valid cache entry 
   for post counts that is not correct and you notice the counts to be wrong over
   time, then I think that the invalidation strategy being used is only time-based,
   meaning that the contents will be emptied after a set period of time.
 * Kind regards,
 * George
 *  Thread Starter [alexl75](https://wordpress.org/support/users/alexl75/)
 * (@alexl75)
 * [2 years, 5 months ago](https://wordpress.org/support/topic/stale-cache-on-wp_count_posts/#post-17291762)
 * Hey [@gtsiokos](https://wordpress.org/support/users/gtsiokos/),
 * We’re using this plugin: [https://github.com/rhubarbgroup/redis-cache/](https://github.com/rhubarbgroup/redis-cache/)
   with AWS Elasticache (Redis 7.1) with no special options. Should be a fairly 
   standard WP + Redis setup given the popularity of that plugin.
 * If you have any pointers as to how Groups clears/updates the counts I’m all ears,
   I can try and troubleshoot directly.
 * Thanks!
   Alex
 *  [gtsiokos](https://wordpress.org/support/users/gtsiokos/)
 * (@gtsiokos)
 * [2 years, 5 months ago](https://wordpress.org/support/topic/stale-cache-on-wp_count_posts/#post-17305399)
 * Hey Alex,
 * The counts are adjusted in the function you already found [here](https://github.com/itthinx/groups/blob/b9ffa5b76e3420576b2e525e9333a74205e4d266/lib/access/class-groups-post-access.php#L873)
   and the cache contents are checked using the [WP Cache API](https://github.com/itthinx/groups/blob/master/lib/core/class-groups-cache.php#L50)
   that’s why I mentioned earlier if and how does the OC you’re using invalidate
   the cache contents.
 * Perhaps it would be useful if you ask the OC developer on their caching strategy
   because since there is data found in the cache, then either something missing
   on the cache setup, or the data won’t invalidate when it should, according to
   your findings.
 * Kind regards,
 * George
 *  Thread Starter [alexl75](https://wordpress.org/support/users/alexl75/)
 * (@alexl75)
 * [2 years, 5 months ago](https://wordpress.org/support/topic/stale-cache-on-wp_count_posts/#post-17305438)
 * Hey [@gtsiokos](https://wordpress.org/support/users/gtsiokos/),
 * What I’m suspecting is that the Groups cache entry for the `wp_count_posts` override
   is never cleared/invalidated at all.
 * Unless the expiry is expressly scheduled by passing a TTL with `wp_cache_set`(
   4th argument, which doesn’t seem to be used in the `Groups_Cache` implementation
   at all) or calling `wp_cache_delete` on the entry, **which the core implementation
   does**, it won’t ever be cleared or updated.
 * The default `WP_Object_Cache` implementation doesn’t persist across pageloads
   so that’s fine. However Redis and Memcached are both persistent so with the way
   the `Groups_Cache` class is implemented currently, any value will persist across
   pageloads forever until it is expressly modified/deleted.
 * Hope this helps,
   Alex
 *  [gtsiokos](https://wordpress.org/support/users/gtsiokos/)
 * (@gtsiokos)
 * [2 years, 5 months ago](https://wordpress.org/support/topic/stale-cache-on-wp_count_posts/#post-17307519)
 * Hey Alex,
 * Just to be sure I’ll try to replicate the setup locally on a test server. If 
   there’s anything specific about the setup of OC plugin that I need to keep in
   mind, please let me know.
 * For the counts I wouldn’t use a TTL expiry because an event-driven expiry has
   a bigger significance in this case, but a test will show us what can be going
   wrong with the cache contents.
 * Kind regards,
 * George
 *  Plugin Author [Kento](https://wordpress.org/support/users/proaktion/)
 * (@proaktion)
 * [2 years, 4 months ago](https://wordpress.org/support/topic/stale-cache-on-wp_count_posts/#post-17377383)
 * [@alexl75](https://wordpress.org/support/users/alexl75/) Using the latest release
   and from tests this seems to be resolved so I’m marking this thread accordingly.
   But should the issue still persist on your side, please let me know.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Stale cache on wp_count_posts’ is closed to new replies.

 * ![](https://ps.w.org/groups/assets/icon-256x256.png?rev=983146)
 * [Groups](https://wordpress.org/plugins/groups/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/groups/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/groups/)
 * [Active Topics](https://wordpress.org/support/plugin/groups/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/groups/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/groups/reviews/)

## Tags

 * [cache](https://wordpress.org/support/topic-tag/cache/)
 * [caching](https://wordpress.org/support/topic-tag/caching/)
 * [count](https://wordpress.org/support/topic-tag/count/)
 * [counts](https://wordpress.org/support/topic-tag/counts/)
 * [group](https://wordpress.org/support/topic-tag/group/)
 * [post](https://wordpress.org/support/topic-tag/post/)

 * 6 replies
 * 3 participants
 * Last reply from: [Kento](https://wordpress.org/support/users/proaktion/)
 * Last activity: [2 years, 4 months ago](https://wordpress.org/support/topic/stale-cache-on-wp_count_posts/#post-17377383)
 * Status: resolved