@@ -44,12 +44,8 @@ public function get_classname() {
44
44
* @return array $obecjts
45
45
*/
46
46
public static function get_by_ids ($ ids ) {
47
- $ result = [];
48
-
49
47
// Preserve the order of the ids
50
- foreach ($ ids as $ id ) {
51
- $ result [$ id ] = null ;
52
- }
48
+ $ result = array_fill_keys ($ ids , null );
53
49
54
50
if (get_called_class ()::trait_cache_enabled ()) {
55
51
$ prefix = get_called_class ()::trait_get_cache_prefix ();
@@ -59,7 +55,11 @@ public static function get_by_ids($ids) {
59
55
}
60
56
$ cached_objects = get_called_class ()::cache_multi_get ($ cache_keys );
61
57
58
+ // we use it to avoid repeatedly iterating if some cache is missing
59
+ $ cached_objects_map = [];
60
+
62
61
foreach ($ cached_objects as $ cached_object ) {
62
+ $ cached_objects_map [$ cached_object ->id ] = $ cached_object ;
63
63
unset($ cache_keys [$ cached_object ->id ]);
64
64
}
65
65
@@ -68,11 +68,10 @@ public static function get_by_ids($ids) {
68
68
}
69
69
70
70
foreach ($ ids as $ id ) {
71
- foreach ($ cached_objects as $ cached_object ) {
72
- if ($ cached_object ->id === $ id ) {
73
- $ result [$ id ] = $ cached_object ;
74
- }
71
+ if (isset ($ cached_objects_map [$ id ]) === false ) {
72
+ continue ;
75
73
}
74
+ $ result [$ id ] = $ cached_objects_map [$ id ];
76
75
}
77
76
}
78
77
@@ -81,7 +80,7 @@ public static function get_by_ids($ids) {
81
80
if ($ value !== null ) {
82
81
continue ;
83
82
}
84
- $ return [$ id ] = self ::get_by_id ($ id );
83
+ $ result [$ id ] = self ::get_by_id ($ id );
85
84
}
86
85
87
86
return $ result ;
0 commit comments