Skip to content

Commit

Permalink
Cache network storage, use du to retrieve (fixes #11) (#12)
Browse files Browse the repository at this point in the history
* Cache network storage, use du to retrieve (fixes #11)

* Fix CS error
  • Loading branch information
Ned Zimmerman authored Sep 19, 2018
1 parent 41302e4 commit 7b94ab4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
18 changes: 18 additions & 0 deletions bin/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

set_time_limit( 0 );

// Stats page

$cache_key = \PressbooksStats\Stats\get_admin_page_html_cache_key();

delete_site_transient( $cache_key );
Expand All @@ -20,3 +22,19 @@
} else {
echo "Successfully cached the stats dashboard! \n";
}

// Network storage

$cache_key = 'pb_stats_network_storage';

delete_site_transient( $cache_key );

\PressbooksStats\Stats\cache_network_storage();

$storage = get_site_transient( $cache_key );

if ( empty( $storage ) ) {
echo "Failed to cache network storage data... \n";
} else {
echo "Successfully cached network storage data! \n";
}
23 changes: 19 additions & 4 deletions inc/stats/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,24 @@ function users_with_x_or_more_books( $x ) {

function display_network_storage() {
$storage = get_site_transient( 'pb_stats_network_storage' );
if ( empty( $storage ) ) {
$storage = format_bytes( recurse_dirsize( wp_upload_dir()['basedir'] ) );
set_site_transient( 'pb_stats_network_storage', $storage, DAY_IN_SECONDS );
if ( ! empty( $storage ) ) {
$cached = '<!-- CACHED -->';
} else {
$cached = '';
$storage = calculate_network_storage();
}
printf( '<p>%1$s: %2$s</p>', __( 'Network Storage', 'pressbooks-stats' ), $storage );
printf( '%1$s<p>%2$s: %3$s</p>', $cached, __( 'Network Storage', 'pressbooks-stats' ), $storage );
}

function calculate_network_storage() {
$path = wp_upload_dir()['basedir'];
$storage = format_bytes( rtrim( str_replace( $path, '', `du -b -s $path` ) ) );
return $storage;
}

/**
* Cache the network storage level
*/
function cache_network_storage() {
set_site_transient( 'pb_stats_network_storage', calculate_network_storage() );
}

0 comments on commit 7b94ab4

Please sign in to comment.