Ga_Cache.php
214 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Handles request caching. |
| 5 | * |
| 6 | * Created by PhpStorm. |
| 7 | * User: mdn |
| 8 | * Date: 2017-01-27 |
| 9 | * Time: 10:31 |
| 10 | */ |
| 11 | class Ga_Cache { |
| 12 | |
| 13 | /** |
| 14 | * Time until expiration in seconds |
| 15 | */ |
| 16 | const GA_DATA_EXPIRATION_TIME = 86400;// 60 * 60 * 24 = 86400; // 24h |
| 17 | |
| 18 | const GA_TRANSIENT_PREFIX = 'googleanalytics_cache_'; |
| 19 | |
| 20 | const GA_LAST_CACHE_OPTION_NAME = 'googleanalytics_cache_last_cache_time'; |
| 21 | |
| 22 | const GA_LAST_TIME_ATTEMPT_OPTION_NAME = 'googleanalytics_cache_last_time_attempt'; |
| 23 | |
| 24 | const GA_BUFFER_CACHE_OPTION_NAME = 'googleanalytics_cache_buffer'; |
| 25 | |
| 26 | const GA_WAIT_AFTER_ERROR_TIME = 300; // 60 * 5 = 5 min |
| 27 | |
| 28 | public static function add_cache_options() { |
| 29 | add_option( self::GA_LAST_CACHE_OPTION_NAME ); |
| 30 | add_option( self::GA_BUFFER_CACHE_OPTION_NAME ); |
| 31 | add_option( self::GA_LAST_TIME_ATTEMPT_OPTION_NAME ); |
| 32 | } |
| 33 | |
| 34 | public static function delete_cache_options() { |
| 35 | delete_option( self::GA_LAST_CACHE_OPTION_NAME ); |
| 36 | delete_option( self::GA_BUFFER_CACHE_OPTION_NAME ); |
| 37 | delete_option( self::GA_LAST_TIME_ATTEMPT_OPTION_NAME ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Generates transient name. |
| 42 | * |
| 43 | * @param $rest_url |
| 44 | * @param $query_params |
| 45 | * @param string $apendix |
| 46 | * |
| 47 | * @return string |
| 48 | */ |
| 49 | public static function get_transient_name( $rest_url, $query_params, $apendix = '' ) { |
| 50 | |
| 51 | if (is_array($query_params)) { |
| 52 | $query_params = wp_json_encode($query_params); |
| 53 | } |
| 54 | |
| 55 | $name = md5( $rest_url . $query_params ); |
| 56 | |
| 57 | return self::GA_TRANSIENT_PREFIX . $name . '_' . $apendix; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Gets cached data. |
| 62 | * |
| 63 | * @param $name |
| 64 | * |
| 65 | * @return bool|mixed |
| 66 | */ |
| 67 | public static function get_cached_result( $name ) { |
| 68 | |
| 69 | $data = get_option( self::GA_BUFFER_CACHE_OPTION_NAME ); |
| 70 | |
| 71 | // Check if cache exists |
| 72 | if ( ! empty( $data[ $name ] ) ) { // Cache exists |
| 73 | return $data[ $name ]; |
| 74 | } else { |
| 75 | return false; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Sets ne cache value. |
| 81 | * |
| 82 | * @param $name |
| 83 | * @param $result |
| 84 | */ |
| 85 | public static function set_cache( $name, $result ) { |
| 86 | if ( ! empty( $result ) ) { |
| 87 | self::set_last_cache_time( $name ); |
| 88 | self::set_cache_buffer( $name, $result ); |
| 89 | self::delete_last_time_attempt(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Updates the time the response was cached. |
| 95 | * |
| 96 | * @param $name |
| 97 | */ |
| 98 | public static function set_last_cache_time( $name ) { |
| 99 | $data = get_option( self::GA_LAST_CACHE_OPTION_NAME ); |
| 100 | |
| 101 | if ( empty( $data ) ) { |
| 102 | $data = array(); |
| 103 | $data[ $name ] = time(); |
| 104 | } else { |
| 105 | $data = array_merge( $data, array( $name => time() ) ); |
| 106 | } |
| 107 | |
| 108 | update_option( self::GA_LAST_CACHE_OPTION_NAME, $data ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Sets or update data cache. |
| 113 | * |
| 114 | * @param $name |
| 115 | * @param $result |
| 116 | */ |
| 117 | public static function set_cache_buffer( $name, $result ) { |
| 118 | $data = get_option( self::GA_BUFFER_CACHE_OPTION_NAME ); |
| 119 | |
| 120 | if ( empty( $data ) ) { |
| 121 | $data = array(); |
| 122 | $data[ $name ] = $result; |
| 123 | } else { |
| 124 | $data = array_merge( $data, array( $name => $result ) ); |
| 125 | } |
| 126 | |
| 127 | update_option( self::GA_BUFFER_CACHE_OPTION_NAME, $data ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Sets last time attempt option |
| 132 | */ |
| 133 | public static function set_last_time_attempt() { |
| 134 | update_option( self::GA_LAST_TIME_ATTEMPT_OPTION_NAME, time()); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Deletes last time attepmt option |
| 139 | */ |
| 140 | public static function delete_last_time_attempt() { |
| 141 | delete_option( self::GA_LAST_TIME_ATTEMPT_OPTION_NAME ); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Checks if the next rest API request is allowed. |
| 146 | * |
| 147 | * Next api request is allowed when there was an unsuccessful attempt and data_cache is empty. |
| 148 | * |
| 149 | * @param $name |
| 150 | * |
| 151 | * @return bool |
| 152 | */ |
| 153 | public static function is_next_request_allowed( $name ) { |
| 154 | $last_time_attempt = get_option( self::GA_LAST_TIME_ATTEMPT_OPTION_NAME ); |
| 155 | $outdated_last_attempt_time = true; |
| 156 | |
| 157 | if ( empty( $last_time_attempt ) ) { |
| 158 | // If there is no last_time_attempt then return true |
| 159 | return true; |
| 160 | } elseif (!empty($last_time_attempt)) { |
| 161 | $outdated_last_attempt_time = ( $last_time_attempt + self::GA_WAIT_AFTER_ERROR_TIME ) < time(); |
| 162 | } |
| 163 | |
| 164 | return ( ! self::get_cached_result( $name ) && $outdated_last_attempt_time ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Checks whether data cache is outdated. |
| 169 | * @param string $name |
| 170 | * @param string $appendix |
| 171 | * |
| 172 | * @return bool |
| 173 | */ |
| 174 | public static function is_data_cache_outdated($name = '', $appendix = '') { |
| 175 | $last_time = get_option( self::GA_LAST_CACHE_OPTION_NAME ); |
| 176 | $outdated = 0; |
| 177 | if (!empty($last_time)) { |
| 178 | |
| 179 | // Validate cache for given rest name |
| 180 | if ( ! empty( $name ) ) { |
| 181 | |
| 182 | // if appendix is set then check only that cache which concerns given appendix |
| 183 | if ( ! empty( $appendix ) ) { |
| 184 | return ( ! empty( $appendix ) && preg_match( '/' . $appendix . '/', |
| 185 | $name ) && ( $last_time[ $name ] + self::GA_DATA_EXPIRATION_TIME ) < time() ); |
| 186 | } else { |
| 187 | return ! empty( $last_time[ $name ] ) && ( $last_time[ $name ] + self::GA_DATA_EXPIRATION_TIME ) < time(); |
| 188 | } |
| 189 | |
| 190 | } else { // Validate cache for all requests |
| 191 | |
| 192 | // If any of existing caches is outdated |
| 193 | foreach ( $last_time as $item => $time ) { |
| 194 | // if appendix is set then check only entries concerns given appendix |
| 195 | if ( ! empty( $appendix ) ) { |
| 196 | if ( ! empty( $appendix ) && preg_match( '/' . $appendix . '/', $item ) && ( $time + self::GA_DATA_EXPIRATION_TIME ) < time() ) { |
| 197 | $outdated ++; |
| 198 | } |
| 199 | } else { |
| 200 | if ( ( $time + self::GA_DATA_EXPIRATION_TIME ) < time() ) { |
| 201 | $outdated ++; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | return $outdated > 0; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | } |
| 214 |