| @@ -22,8 +22,18 @@ | ||
| 22 | 22 | */ |
| 23 | 23 | class Campaign { |
| 24 | 24 | |
| 25 | 25 | /** |
| 26 | + * Object cache group for campaign rows. | |
| 27 | + */ | |
| 28 | + public const CACHE_GROUP = 'disco_campaigns'; | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * Object cache key holding every campaign row. | |
| 32 | + */ | |
| 33 | + public const ROWS_CACHE_KEY = 'disco_campaign_rows'; | |
| 34 | + | |
| 35 | + /** | |
| 26 | 36 | * Get campaigns from database. |
| 27 | 37 | * |
| 28 | 38 | * @param string $status Campaign status. |
| 29 | 39 | * @param string $intent Campaign intent. |
| @@ -270,8 +280,10 @@ | ||
| 270 | 280 | ) |
| 271 | 281 | ); |
| 272 | 282 | |
| 273 | 283 | if ( $insert ) { |
| 284 | + self::flush_rows_cache(); | |
| 285 | + | |
| 274 | 286 | return $wpdb->insert_id; |
| 275 | 287 | } |
| 276 | 288 | |
| 277 | 289 | return 0; |
| @@ -289,9 +301,9 @@ | ||
| 289 | 301 | global $wpdb; |
| 290 | 302 | |
| 291 | 303 | $table_name = $wpdb->prefix . 'disco_campaigns'; |
| 292 | 304 | |
| 293 | - return $wpdb->update( //phpcs:ignore | |
| 305 | + $updated = $wpdb->update( //phpcs:ignore | |
| 294 | 306 | $table_name, |
| 295 | 307 | array( |
| 296 | 308 | 'intent' => $config['discount_intent'], |
| 297 | 309 | 'status' => $config['status'], |
| @@ -306,8 +318,12 @@ | ||
| 306 | 318 | '%s', |
| 307 | 319 | ), |
| 308 | 320 | array( '%d' ) |
| 309 | 321 | ); |
| 322 | + | |
| 323 | + self::flush_rows_cache(); | |
| 324 | + | |
| 325 | + return $updated; | |
| 310 | 326 | } |
| 311 | 327 | |
| 312 | 328 | /** |
| 313 | 329 | * Delete campaign from database. |
| @@ -320,9 +336,14 @@ | ||
| 320 | 336 | global $wpdb; |
| 321 | 337 | |
| 322 | 338 | $table_name = $wpdb->prefix . 'disco_campaigns'; |
| 323 | 339 | |
| 324 | - return $wpdb->delete( $table_name, array( 'id' => $id ), array( '%d' ) );//phpcs:ignore | |
| 340 | + $deleted = $wpdb->delete( $table_name, array( 'id' => $id ), array( '%d' ) );//phpcs:ignore | |
| 341 | + | |
| 342 | + self::flush_rows_cache(); | |
| 343 | + \Disco\App\Features\UserLimit::flush_cache( $id ); | |
| 344 | + | |
| 345 | + return $deleted; | |
| 325 | 346 | } |
| 326 | 347 | |
| 327 | 348 | /** |
| 328 | 349 | * Get all rows from database. |
| @@ -332,8 +353,15 @@ | ||
| 332 | 353 | * @since 1.0.0 |
| 333 | 354 | */ |
| 334 | 355 | public function get_rows() { |
| 335 | 356 | global $wpdb; |
| 357 | + | |
| 358 | + $cached = wp_cache_get( self::ROWS_CACHE_KEY, self::CACHE_GROUP ); | |
| 359 | + | |
| 360 | + if ( is_array( $cached ) ) { | |
| 361 | + return $cached; | |
| 362 | + } | |
| 363 | + | |
| 336 | 364 | // Get all rows |
| 337 | 365 | $get_campaigns = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}disco_campaigns", OBJECT ); //phpcs:ignore |
| 338 | 366 | $campaigns = array(); |
| 339 | 367 | |
| @@ -340,25 +368,39 @@ | ||
| 340 | 368 | foreach ( $get_campaigns as $result ) { |
| 341 | 369 | $campaigns[ $result->id ] = $result; |
| 342 | 370 | } |
| 343 | 371 | |
| 372 | + wp_cache_set( self::ROWS_CACHE_KEY, $campaigns, self::CACHE_GROUP, MINUTE_IN_SECONDS * 5 ); | |
| 373 | + | |
| 344 | 374 | return $campaigns; |
| 345 | 375 | } |
| 346 | 376 | |
| 377 | + /** | |
| 378 | + * Single Row Query by id. | |
| 379 | + * First check if the row is available in cache. | |
| 380 | + * If not, then fetch from the database. | |
| 381 | + * | |
| 382 | + * @noinspection SqlResolve | |
| 383 | + * @param int $id Campaign id. | |
| 384 | + * @return object|null | |
| 385 | + * @since 1.0.0 | |
| 386 | + */ | |
| 387 | + public function get_row( $id ) { | |
| 388 | + global $wpdb; | |
| 389 | + $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}disco_campaigns WHERE id = %d", $id ); //phpcs:ignore | |
| 390 | + | |
| 391 | + return $wpdb->get_row( $query, OBJECT );//phpcs:ignore | |
| 392 | + } | |
| 393 | + | |
| 347 | 394 | /** |
| 348 | - * Single Row Query by id. | |
| 349 | - * First check if the row is available in cache. | |
| 350 | - * If not, then fetch from the database. | |
| 395 | + * Invalidate the cached campaign rows. | |
| 351 | 396 | * |
| 352 | - * @noinspection SqlResolve | |
| 353 | - * @param int $id Campaign id. | |
| 354 | - * @return object|null | |
| 355 | - * @since 1.0.0 | |
| 397 | + * Called from every write path so the cache can never serve stale campaigns. | |
| 398 | + * | |
| 399 | + * @return void | |
| 400 | + * @since 1.4.5 | |
| 356 | 401 | */ |
| 357 | - public function get_row( $id ) { | |
| 358 | - global $wpdb; | |
| 359 | - $query = $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}disco_campaigns WHERE id = %d", $id ); //phpcs:ignore | |
| 360 | - | |
| 361 | - return $wpdb->get_row( $query, OBJECT );//phpcs:ignore | |
| 402 | + public static function flush_rows_cache() { | |
| 403 | + wp_cache_delete( self::ROWS_CACHE_KEY, self::CACHE_GROUP ); | |
| 362 | 404 | } |
| 363 | 405 | |
| 364 | 406 | } |