Skip to content

Commit b2a6b06

Browse files
authored
Merge pull request #139 from BeAPI/issue/57061
Add object cache to current_object_is_synchronized method
2 parents be647ea + c1e4ff2 commit b2a6b06

File tree

2 files changed

+361
-176
lines changed

2 files changed

+361
-176
lines changed

classes/models/relations.php

Lines changed: 121 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22

33
class BEA_CSF_Relations {
4+
5+
const BEA_CSF_RELATIONS_CACHE_GROUP = 'relations-cache-group';
6+
47
/**
58
* @return int|mixed
69
*
@@ -14,14 +17,14 @@ class BEA_CSF_Relations {
1417
public static function merge( $type, $emitter_blog_id, $emitter_id, $receiver_blog_id, $receiver_id, $strict_mode = false ) {
1518
// Test with right emitter/receiver direction
1619
$relation_id = self::exists( $type, $emitter_blog_id, $emitter_id, $receiver_blog_id, $receiver_id );
17-
if ( $relation_id != false ) {
20+
if ( false != $relation_id ) {
1821
return $relation_id;
1922
}
2023

2124
if ( false === $strict_mode ) {
2225
// Test also on reverse direction emitter/receiver, allow to not create duplicate relations on 2 directions
2326
$relation_id = self::exists( $type, $receiver_blog_id, $receiver_id, $emitter_blog_id, $emitter_id );
24-
if ( $relation_id != false ) {
27+
if ( false != $relation_id ) {
2528
return $relation_id;
2629
}
2730
}
@@ -71,6 +74,16 @@ public static function delete( $id ) {
7174

7275
/** @var WPDB $wpdb */
7376

77+
// Clean cache before delete
78+
$relations = $wpdb->get_results(
79+
$wpdb->prepare( "SELECT * FROM $wpdb->bea_csf_relations WHERE id = %d", $id )
80+
);
81+
if ( ! empty( $relations ) ) {
82+
foreach ( $relations as $relation ) {
83+
self::delete_relation_cache( $relation );
84+
}
85+
}
86+
7487
return $wpdb->delete( $wpdb->bea_csf_relations, array( 'id' => $id ), array( '%d' ) );
7588
}
7689

@@ -104,6 +117,16 @@ public static function delete_by_blog_id( $blog_id ) {
104117

105118
/** @var WPDB $wpdb */
106119

120+
// Clean cache before delete
121+
$relations = $wpdb->get_results(
122+
$wpdb->prepare( "SELECT * FROM $wpdb->bea_csf_relations WHERE emitter_blog_id = %d", $blog_id )
123+
);
124+
if ( ! empty( $relations ) ) {
125+
foreach ( $relations as $relation ) {
126+
self::delete_relation_cache( $relation );
127+
}
128+
}
129+
107130
$wpdb->delete(
108131
$wpdb->bea_csf_relations,
109132
array(
@@ -112,6 +135,16 @@ public static function delete_by_blog_id( $blog_id ) {
112135
array( '%d' )
113136
);
114137

138+
// Clean cache before delete
139+
$relations = $wpdb->get_results(
140+
$wpdb->prepare( "SELECT * FROM $wpdb->bea_csf_relations WHERE receiver_blog_id = %d", receiver_blog_id )
141+
);
142+
if ( ! empty( $relations ) ) {
143+
foreach ( $relations as $relation ) {
144+
self::delete_relation_cache( $relation );
145+
}
146+
}
147+
115148
$wpdb->delete(
116149
$wpdb->bea_csf_relations,
117150
array(
@@ -136,6 +169,21 @@ public static function delete_by_emitter( $type, $emitter_blog_id, $emitter_id )
136169

137170
/** @var WPDB $wpdb */
138171

172+
// Clean cache before delete
173+
$relations = $wpdb->get_results(
174+
$wpdb->prepare(
175+
"SELECT * FROM $wpdb->bea_csf_relations WHERE type = %s, emitter_blog_id = %d, emitter_id = %d",
176+
$type,
177+
$emitter_blog_id,
178+
$emitter_id
179+
)
180+
);
181+
if ( ! empty( $relations ) ) {
182+
foreach ( $relations as $relation ) {
183+
self::delete_relation_cache( $relation );
184+
}
185+
}
186+
139187
return $wpdb->delete(
140188
$wpdb->bea_csf_relations,
141189
array(
@@ -160,6 +208,21 @@ public static function delete_by_receiver( $type, $receiver_blog_id, $receiver_i
160208

161209
/** @var WPDB $wpdb */
162210

211+
// Clean cache before delete
212+
$relations = $wpdb->get_results(
213+
$wpdb->prepare(
214+
"SELECT * FROM $wpdb->bea_csf_relations WHERE type = %s, receiver_blog_id = %d, receiver_id = %d",
215+
$type,
216+
$receiver_blog_id,
217+
$receiver_id
218+
)
219+
);
220+
if ( ! empty( $relations ) ) {
221+
foreach ( $relations as $relation ) {
222+
self::delete_relation_cache( $relation );
223+
}
224+
}
225+
163226
return $wpdb->delete(
164227
$wpdb->bea_csf_relations,
165228
array(
@@ -186,6 +249,23 @@ public static function delete_by_emitter_and_receiver( $type, $emitter_blog_id,
186249

187250
/** @var WPDB $wpdb */
188251

252+
// Clean cache before delete
253+
$relations = $wpdb->get_results(
254+
$wpdb->prepare(
255+
"SELECT * FROM $wpdb->bea_csf_relations WHERE type = %s, emitter_blog_id = %d, emitter_id = %d, receiver_blog_id = %d, receiver_id = %d",
256+
$type,
257+
$emitter_blog_id,
258+
$emitter_id,
259+
$receiver_blog_id,
260+
$receiver_id
261+
)
262+
);
263+
if ( ! empty( $relations ) ) {
264+
foreach ( $relations as $relation ) {
265+
self::delete_relation_cache( $relation );
266+
}
267+
}
268+
189269
return $wpdb->delete(
190270
$wpdb->bea_csf_relations,
191271
array(
@@ -236,7 +316,7 @@ public static function get_object_for_any( $type, $emitter_blog_id, $receiver_bl
236316
return $result_id;
237317
}
238318

239-
if ( $direct === true ) {
319+
if ( true === $direct ) {
240320
return false;
241321
}
242322

@@ -351,15 +431,24 @@ public static function get_object_id_for_emitter( $types, $emitter_blog_id, $rec
351431
*/
352432
public static function current_object_is_synchronized( $types, $receiver_blog_id, $receiver_id ) {
353433
global $wpdb;
434+
$cache_id = self::get_cache_id( $types, $receiver_blog_id, $receiver_id );
435+
$relation = wp_cache_get( $cache_id, self::BEA_CSF_RELATIONS_CACHE_GROUP, false, $found );
436+
if ( $found ) {
437+
return $relation;
438+
}
354439

355440
/** @var WPDB $wpdb */
356-
return $wpdb->get_row(
441+
$result = $wpdb->get_row(
357442
$wpdb->prepare(
358443
"SELECT * FROM $wpdb->bea_csf_relations WHERE type IN ( " . self::get_sql_in_types( $types ) . ' ) AND receiver_blog_id = %d AND receiver_id = %d',
359444
$receiver_blog_id,
360445
$receiver_id
361446
)
362447
);
448+
if ( null !== $result ) {
449+
wp_cache_set( $cache_id, $result, self::BEA_CSF_RELATIONS_CACHE_GROUP );
450+
}
451+
return $result;
363452
}
364453

365454
/**
@@ -484,4 +573,32 @@ function ( $v ) {
484573

485574
return implode( ', ', $types );
486575
}
576+
577+
/**
578+
* Get a unique relation cache_id
579+
*
580+
* @param string|array $types
581+
* @param int $receiver_blog_id
582+
* @param int $receiver_id
583+
* @return string
584+
*/
585+
private static function get_cache_id( $types, int $receiver_blog_id, int $receiver_id ): string {
586+
$type = 'post';
587+
if ( 'taxonomy' === $types || ( is_array( $types ) && in_array( 'taxonomy', $types, true ) ) ) {
588+
$type = 'taxonomy';
589+
}
590+
return $receiver_blog_id . '-' . $receiver_id . '-' . $type;
591+
}
592+
593+
/**
594+
* Delete all relation caches matching query results
595+
*
596+
* @param stdClass $relation
597+
* @return void
598+
*/
599+
public static function delete_relation_cache( stdClass $relation ) {
600+
$cache_id = self::get_cache_id( $relation->type, $relation->receiver_blog_id, $relation->receiver_id );
601+
wp_cache_delete( $cache_id, self::BEA_CSF_RELATIONS_CACHE_GROUP );
602+
603+
}
487604
}

0 commit comments

Comments
 (0)