Skip to content

Commit 01d02c6

Browse files
author
infinitnet
committed
v1.1: caching logic improved
1 parent 66082ae commit 01d02c6

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ For support, feature requests, or bug reports, please visit the [GitHub issues p
2828
Contributions to the plugin are welcome. Please fork the GitHub repository and submit a pull request.
2929

3030
## Changelog
31+
- **1.1**: Transient caching logic and cache clearing improved.
3132
- **1.0**: Initial release.

relevant-recent-posts.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,15 @@ function infinitnet_relevant_recent_posts_shortcode($atts) {
5757
}
5858
}
5959

60-
$post_id = isset($post) ? $post->ID : 0;
61-
$scope = $atts['scope'];
62-
$date = $atts['date'];
63-
64-
$cache_key = "rrp_{$post_id}_{$scope}_{$date}";
65-
60+
$relevant_atts = array(
61+
'count' => $atts['count'],
62+
'scope' => $atts['scope'],
63+
'date' => $atts['date']
64+
);
65+
$serialized_atts = serialize($relevant_atts);
66+
67+
$cache_key = "rrp_" . md5($serialized_atts);
68+
6669
$output = get_transient($cache_key);
6770

6871
if ($output === false) {
@@ -101,9 +104,9 @@ function infinitnet_relevant_recent_posts_shortcode($atts) {
101104
function infinitnet_clear_recent_relevant_posts_cache($post_id) {
102105
global $wpdb;
103106

104-
$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '%rrp_{$post_id}_%'");
107+
$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '%rrp_%'");
105108
}
106109

107110
add_action('save_post', 'infinitnet_clear_recent_relevant_posts_cache');
108111
add_action('delete_post', 'infinitnet_clear_recent_relevant_posts_cache');
109-
add_action('transition_post_status', 'infinitnet_clear_recent_relevant_posts_cache', 10, 3);
112+
add_action('transition_post_status', 'infinitnet_clear_recent_relevant_posts_cache');

0 commit comments

Comments
 (0)