Skip to content

Commit c2b8150

Browse files
committed
add transient cache for extended statistics endpoint
1 parent b5ac92a commit c2b8150

File tree

1 file changed

+87
-28
lines changed

1 file changed

+87
-28
lines changed

inc/class-statify-api.php

Lines changed: 87 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -174,42 +174,101 @@ public static function get_stats( $request ) {
174174
* @since 2.0.0
175175
*/
176176
public static function get_extended( $request ) {
177+
// Verify scope.
177178
$scope = $request->get_param( 'scope' );
179+
if ( ! in_array( $scope, array( 'year', 'month', 'day' ) ) ) {
180+
return new WP_REST_Response(
181+
array( 'error' => 'invalid scope (allowed: year, month, day)' ),
182+
400
183+
);
184+
}
185+
186+
// Retrieve from cache, if data is not post-specific.
178187
$post = $request->get_param( 'post' );
188+
$stats = false;
189+
if ( ! $post ) {
190+
$stats = self::from_cache( $scope );
191+
}
179192

180-
$status = 200;
181-
if ( 'year' === $scope ) {
182-
$stats = Statify_Evaluation::get_views_for_all_years( $post );
183-
} elseif ( 'month' === $scope ) {
184-
$visits = Statify_Evaluation::get_views_for_all_months( $post );
185-
$stats = array( 'visits' => array() );
186-
$last_ym = 0;
187-
foreach ( $visits as $ym => $v ) {
188-
$ym = explode( '-', $ym );
189-
$year = intval( $ym[0] );
190-
$month = intval( $ym[1] );
191-
$year_month = $year * 12 + $month;
192-
for ( $ym = $last_ym + 1; $last_ym > 0 && $ym < $year_month; $ym ++ ) {
193-
// Fill gaps.
194-
$y = intval( $ym / 12 );
195-
if ( ! isset( $stats['visits'][ $y ] ) ) {
196-
$stats['visits'][ $y ] = array();
193+
if ( ! $stats ) {
194+
if ( 'year' === $scope ) {
195+
if ( ! $post ) {
196+
$stats = self::from_cache( $scope );
197+
}
198+
199+
if ( ! $stats ) {
200+
$stats = Statify_Evaluation::get_views_for_all_years( $post );
201+
}
202+
} elseif ( 'month' === $scope ) {
203+
if ( ! $post ) {
204+
$stats = self::from_cache( $scope );
205+
}
206+
207+
if ( ! $stats ) {
208+
$visits = Statify_Evaluation::get_views_for_all_months( $post );
209+
$stats = array( 'visits' => array() );
210+
$last_ym = 0;
211+
foreach ( $visits as $ym => $v ) {
212+
$ym = explode( '-', $ym );
213+
$year = intval( $ym[0] );
214+
$month = intval( $ym[1] );
215+
$year_month = $year * 12 + $month;
216+
for ( $ym = $last_ym + 1; $last_ym > 0 && $ym < $year_month; $ym ++ ) {
217+
// Fill gaps.
218+
$y = intval( $ym / 12 );
219+
if ( ! isset( $stats['visits'][ $y ] ) ) {
220+
$stats['visits'][ $y ] = array();
221+
}
222+
$stats['visits'][ $y ][ $ym % 12 ] = 0;
223+
}
224+
if ( ! isset( $stats['visits'][ $year ] ) ) {
225+
$stats['visits'][ $year ] = array();
226+
}
227+
$stats['visits'][ $year ][ $month ] = $v;
228+
$last_ym = $year_month;
197229
}
198-
$stats['visits'][ $y ][ $ym % 12 ] = 0;
199230
}
200-
if ( ! isset( $stats['visits'][ $year ] ) ) {
201-
$stats['visits'][ $year ] = array();
231+
} elseif ( 'day' === $scope ) {
232+
if ( ! $post ) {
233+
$stats = self::from_cache( $scope );
234+
}
235+
236+
if ( ! $stats ) {
237+
$stats = Statify_Evaluation::get_views_for_all_days( $post );
202238
}
203-
$stats['visits'][ $year ][ $month ] = $v;
204-
$last_ym = $year_month;
205239
}
206-
} elseif ( 'day' === $scope ) {
207-
$stats = Statify_Evaluation::get_views_for_all_days( $post );
208-
} else {
209-
$stats = array( 'error' => 'invalid scope (allowed: year, month, day)' );
210-
$status = 400;
240+
241+
// Update cache, if data is not post-specific.
242+
if ( ! $post ) {
243+
self::update_cache( $scope, $stats );
244+
}
211245
}
212246

213-
return new WP_REST_Response( $stats, $status );
247+
return new WP_REST_Response( $stats );
248+
}
249+
250+
/**
251+
* Retrieve data from cache.
252+
*
253+
* @param string $scope Scope (year, month, day).
254+
*
255+
* @return array|false Transient data or FALSE.
256+
*/
257+
private static function from_cache( $scope ) {
258+
return get_transient( 'statify_data_' . $scope );
259+
}
260+
261+
/**
262+
* Update data cache.
263+
*
264+
* @param string $scope Scope (year, month, day).
265+
* @param array $data Data.
266+
*/
267+
private static function update_cache( $scope, $data ) {
268+
set_transient(
269+
'statify_data_' . $scope,
270+
$data,
271+
30 * MINUTE_IN_SECONDS
272+
);
214273
}
215274
}

0 commit comments

Comments
 (0)