PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.4
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.4
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / inc / Statistics / StatisticsCache.php

StatisticsCache.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.4.4, at inc/Statistics/StatisticsCache.php

83 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class StatisticsCache
4 *
5 * @package LearnPress/Classes/Statistics
6 * @since 4.4.2
7 */
8
9 namespace LearnPress\Statistics;
10
11 use LP_Cache;
12
13 defined( 'ABSPATH' ) || exit();
14
15 /**
16 * Short-lived cache for the heavy dashboard aggregates
17 * (funnel, health checks, instructor performance, watchlist).
18 *
19 * Backed by LP_Cache (WP object cache + optional Thim persistent cache, the
20 * same store LearnPress uses for its expensive course queries) under the
21 * 'learn_press/statistics' group. Keyed by md5 of the bucket + its params;
22 * TTL only, no explicit busting — data self-refreshes after the window.
23 *
24 * Set the TTL filter to 0 to bypass caching entirely (useful while profiling
25 * or debugging queries).
26 *
27 * @since 4.4.2
28 */
29 class StatisticsCache extends LP_Cache {
30 /**
31 * @var string Cache group child → group 'learn_press/statistics'.
32 */
33 protected $key_group_child = 'statistics';
34
35 /**
36 * Persist through the Thim cache layer too, matching LearnPress' own
37 * heavy-query caching, so entries survive across requests where available.
38 */
39 public function __construct( $has_thim_cache = true ) {
40 parent::__construct( $has_thim_cache );
41 }
42
43 /**
44 * Cache lifetime in seconds. 0 (or negative) disables caching.
45 *
46 * @return int
47 */
48 public static function ttl(): int {
49 return (int) apply_filters( 'learn-press/statistics/cache-ttl', 300 );
50 }
51
52 /**
53 * Return the cached array for ( bucket, params ) or compute + store it.
54 *
55 * Only array payloads are trusted on read — a corrupt/false/scalar hit is
56 * treated as a miss and recomputed. All dashboard datasets are arrays.
57 *
58 * @param string $bucket Short logical name of the dataset, e.g. 'funnel'.
59 * @param array $key_params Everything the result depends on (type, value, scope signature, flags).
60 * @param callable $callback Produces the value on a cache miss.
61 * @return array
62 */
63 public static function remember( string $bucket, array $key_params, callable $callback ): array {
64 $ttl = self::ttl();
65 if ( $ttl <= 0 ) {
66 return (array) call_user_func( $callback );
67 }
68
69 $key = $bucket . '_' . md5( (string) wp_json_encode( $key_params ) );
70 $cache = new self();
71
72 $cached = $cache->get_cache( $key );
73 if ( is_array( $cached ) ) {
74 return $cached;
75 }
76
77 $value = (array) call_user_func( $callback );
78 $cache->set_cache( $key, $value, $ttl );
79
80 return $value;
81 }
82 }
83