Skip to content

Commit 481e26c

Browse files
authored
Ready to come out of beta (#2)
* sync clearing cache with github updater * upgrade utility * adjust rest table columns * fixes GHU issues with caching vs transients * have rest cache admin delete function use new utility * add transient disabler to ghu plugin * version bump * Update changelog.md
1 parent a66c2cd commit 481e26c

File tree

6 files changed

+61
-43
lines changed

6 files changed

+61
-43
lines changed

admin/class-wrc-admin.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static function add_menu_page() {
5555
static function options_page() {
5656

5757
if ( ! empty( $_REQUEST['wrc-entry-delete'] ) ) {
58-
$deleted = static::delete_by_md5( $_REQUEST['wrc-entry-delete'] );
58+
$deleted = WRC_Utility::clear_cache_by( 'rest_md5', $_REQUEST['wrc-entry-delete'] );
5959

6060
if ( false !== $deleted ) {
6161
echo '<div class="updated"><p>The following entry was deleted:';
@@ -277,34 +277,6 @@ static function query_rest_table( $limit = 10, $domain, $path ) {
277277
return false;
278278
}
279279

280-
static function delete_by_md5( $md5 ) {
281-
global $wpdb;
282-
283-
$select = '
284-
SELECT rest_md5, rest_domain, rest_path
285-
FROM ' . REST_CACHE_TABLE . '
286-
WHERE rest_md5 = "' . esc_attr( $md5 ) . '"
287-
LIMIT 1;
288-
';
289-
290-
$select_result = $wpdb->get_results( $select, ARRAY_A );
291-
292-
if ( ! empty( $select_result ) ) {
293-
$delete = '
294-
DELETE
295-
FROM ' . REST_CACHE_TABLE . '
296-
WHERE rest_md5 = "' . esc_attr( $md5 ) . '"
297-
LIMIT 1;
298-
';
299-
300-
$delete_it = $wpdb->get_results( $delete, ARRAY_A );
301-
302-
return $select_result[0];
303-
}
304-
305-
return false;
306-
}
307-
308280
} // END class
309281

310282
} // END if(!class_exists())

changelog.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#### 1.0.0
2+
* Tested in production at scale for several days, ready to call plugin released and out of beta
3+
* Fixed github updater double caching issues and removed transients from GHU to reduce transient cache overhead, especially useful for reducing VARNISH / REDIS bandwidth issues.
4+
15
#### 0.10.0
26
* Major bugfix: Cached values were never properly updating. Fixed with better code organization
37
and a clearer update-via-cron process!
@@ -34,4 +38,4 @@
3438
* Hooking into HTTP class actions
3539

3640
#### 0.1
37-
* Initial plugin as boilerplate from https://github.com/scarstens/worpress-plugin-boilerplate-redux
41+
* Initial plugin as boilerplate from https://github.com/scarstens/worpress-plugin-boilerplate-redux

inc/class-wrc-caching.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ class WRC_Caching {
1717
static function init() {
1818
// ensure our filters don't run during crons
1919
if ( ! defined( 'DOING_CRON' ) ) {
20-
add_filter( 'pre_http_request', array( get_called_class(), 'pre_http_request' ), 2, 3 );
20+
add_filter( 'pre_http_request', array( get_called_class(), 'pre_http_request' ), 9, 3 );
2121
// If it gets past pre_http_request and to the http response filter,
2222
// check if we should create/update the data via store_data
23-
add_filter( 'http_response', array( get_called_class(), 'store_data' ), 1, 3 );
23+
add_filter( 'http_response', array( get_called_class(), 'store_data' ), 9, 3 );
2424
}
2525
}
2626

@@ -187,7 +187,7 @@ static function maybe_return_requested_data( $url, $args ) {
187187
return maybe_unserialize( $cached_request['rest_response'] );
188188
}
189189

190-
// false is returned because it tells the `pre_http_request` filter that it needs to move on to theactual http request
190+
// false is returned because it tells the `pre_http_request` filter that it needs to move on to the actual http request
191191
return false;
192192
}
193193

@@ -261,4 +261,4 @@ protected static function check_for_expired_result( $data, $args ) {
261261

262262
}
263263

264-
}
264+
}

inc/class-wrc-filters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static function always_filters() {
2727
* Admin only filters are registered
2828
*/
2929
static function admin_filters() {
30-
30+
add_filter( 'ghu_use_remote_call_transients', '__return_false' );
3131
}
3232

3333
/**

inc/class-wrc-utility.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* Class WRC_Utility
5+
*
6+
* Utility functions used to help and simplify clearing out cache in different ways
7+
*
8+
*/
9+
class WRC_Utility {
10+
11+
/**
12+
* Utility to easily delete cache by exact column value
13+
*
14+
* @param $column
15+
* @param $value
16+
*
17+
* @return false|int
18+
*/
19+
public static function clear_cache_by( $column, $value ) {
20+
global $wpdb;
21+
22+
return $wpdb->delete( $wpdb->base_prefix . WP_Rest_Cache::$table, array( $column => $value ) );
23+
}
24+
25+
/**
26+
* Deletes all github api calls from the rest cache to operate with afragen/github-updater plugin
27+
*
28+
* @return false|int number of deleted cache rows
29+
*/
30+
public static function clear_ghu_cache( $type = '' ) {
31+
// only run on plugins since that action happens first
32+
// if it runs on themes it will delete plugin cache twice
33+
if ( ! empty( $type ) && 'plugin' === $type ) {
34+
return self::clear_cache_by( 'rest_domain', 'https://api.github.com' );
35+
}
36+
37+
return false;
38+
}
39+
40+
}

wp-rest-cache.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Plugin URI: https://github.com/WordPress-Phoenix/wordpress-rest-cache
55
* Description: A solution to caching REST data calls without relying on transients or wp_options tables. Note: for multisite "Network Activate", table may need manually created before activation.
66
* Author: scarstens
7-
* Version: 0.10.0
7+
* Version: 1.0.0
88
* Author URI: http://github.com/scarstens
99
* License: GPL V2
1010
* Text Domain: rest_cache
@@ -64,6 +64,8 @@ public function __construct() {
6464
// nesting actions/filters within the init action was causing the transport filter to run too late in some cases
6565
$this->init();
6666

67+
add_action( 'before_ghu_delete_all_transients', array( 'WRC_Utility', 'clear_ghu_cache') );
68+
6769
// init for use with logged in users, see this::authenticated_init for more details
6870
add_action( 'init', array( $this, 'authenticated_init' ) );
6971

@@ -129,14 +131,14 @@ public static function activate() {
129131
// create our table if it doesn't already exist
130132

131133
$sql = "CREATE TABLE " . REST_CACHE_TABLE . " (
132-
`rest_md5` char(32) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
133-
`rest_domain` varchar(1055) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
134-
`rest_path` varchar(1055) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
135-
`rest_response` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
136-
`rest_expires` datetime DEFAULT NULL,
137134
`rest_last_requested` date NOT NULL,
138-
`rest_tag` varchar(1055) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
135+
`rest_expires` datetime DEFAULT NULL,
136+
`rest_domain` varchar(1055) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
137+
`rest_path` varchar(1055) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
138+
`rest_response` longtext COLLATE utf8mb4_unicode_ci NOT NULL,
139+
`rest_tag` varchar(1055) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
139140
`rest_to_update` tinyint(1) DEFAULT '0',
141+
`rest_md5` char(32) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
140142
`rest_args` longtext COLLATE utf8mb4_unicode_ci,
141143
PRIMARY KEY (`rest_md5`),
142144
KEY `rest_domain` (`rest_domain`(191),`rest_path`(191)),
@@ -211,4 +213,4 @@ public static function is_dev() {
211213
// instantiate the plugin class, which should never be instantiated more then once
212214
global $WP_Rest_Cache;
213215
$WP_Rest_Cache = new WP_Rest_Cache();
214-
}
216+
}

0 commit comments

Comments
 (0)