Skip to content

Commit ebaa212

Browse files
committed
Change cache key build up to avoid needless repeats
1 parent ab03317 commit ebaa212

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

src/Data/CachedDataSource.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private function get_cache_key( ...$arguments ): string {
8484
$arguments[] = $this->inner->id();
8585

8686
try {
87-
return md5( json_encode( $arguments, JSON_THROW_ON_ERROR ) );
87+
return md5( json_encode( array_values( array_filter( $arguments ) ), JSON_THROW_ON_ERROR ) );
8888
} catch ( JsonException $e ) {
8989
throw new InvalidArgumentException(
9090
'The cache key could not be generated based on the provide arguments',
@@ -105,8 +105,7 @@ private function get_cache_key( ...$arguments ): string {
105105
*/
106106
private function get_filter_aware_cache_key( ...$arguments ): string {
107107
$arguments[] = $this->filters ? $this->filters->to_array() : null;
108-
$arguments[] = $this->sort ? $this->sort->to_array() : null;
109-
$arguments[] = $this->search;
108+
$arguments[] = (string) $this->search;
110109

111110
return $this->get_cache_key( ...$arguments );
112111
}
@@ -141,7 +140,12 @@ private function fetch( string $cache_key, callable $retrieve_result ) {
141140
* @since $ver$
142141
*/
143142
public function get_data_ids( int $limit = 20, int $offset = 0 ): array {
144-
$key = $this->get_filter_aware_cache_key( __FUNCTION__, $limit, $offset );
143+
$key = $this->get_filter_aware_cache_key(
144+
__FUNCTION__,
145+
$this->sort ? $this->sort->to_array() : null,
146+
$limit,
147+
$offset
148+
);
145149

146150
return $this->fetch(
147151
$key,

tests/Data/CachedDataSourceTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,17 @@ public function testCount(): void {
172172
self::assertCount( 2, $ds );
173173
self::assertSame( 2, $ds->count() ); // second call
174174

175-
self::assertCount( 1, $this->trace->get_calls() );
175+
$asc_sort = $ds->sort_by( Sort::asc( 'name' ) );
176+
self::assertSame( 2, $asc_sort->count() );
177+
178+
$desc_sort = $ds->sort_by( Sort::desc( 'name' ) );
179+
self::assertSame( 2, $desc_sort->count() );
180+
181+
// Count should be called only once on the trace, as the sorting does not influence the cache key.
182+
self::assertSame(
183+
[ 'count', 'sort_by', 'sort_by' ],
184+
array_column( $this->trace->get_calls(), 0 )
185+
);
176186
}
177187

178188
/**

tests/Data/TraceableDataSource.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
* @since $ver$
1717
*/
1818
final class TraceableDataSource implements MutableDataSource {
19-
2019
/**
2120
* The decorated data source.
2221
*
@@ -59,7 +58,7 @@ public function id() : string {
5958
* @since $ver$
6059
*/
6160
public function get_data_by_id( string $id ) : array {
62-
$this->calls[] = [ __METHOD__, ...func_get_args() ];
61+
$this->calls[] = [ __FUNCTION__, ...func_get_args() ];
6362

6463
return $this->inner->get_data_by_id( $id );
6564
}
@@ -69,7 +68,7 @@ public function get_data_by_id( string $id ) : array {
6968
* @since $ver$
7069
*/
7170
public function get_fields() : array {
72-
$this->calls[] = [ __METHOD__ ];
71+
$this->calls[] = [ __FUNCTION__ ];
7372

7473
return $this->inner->get_fields();
7574
}
@@ -79,7 +78,7 @@ public function get_fields() : array {
7978
* @since $ver$
8079
*/
8180
public function get_data_ids( int $limit = 20, int $offset = 0 ) : array {
82-
$this->calls[] = [ __METHOD__, ...func_get_args() ];
81+
$this->calls[] = [ __FUNCTION__, ...func_get_args() ];
8382

8483
return $this->inner->get_data_ids( $limit, $offset );
8584
}
@@ -89,7 +88,7 @@ public function get_data_ids( int $limit = 20, int $offset = 0 ) : array {
8988
* @since $ver$
9089
*/
9190
public function filter_by( ?Filters $filters ) : self {
92-
$this->calls[] = [ __METHOD__, ...func_get_args() ];
91+
$this->calls[] = [ __FUNCTION__, ...func_get_args() ];
9392

9493
$this->inner = $this->inner->filter_by( $filters );
9594

@@ -101,7 +100,7 @@ public function filter_by( ?Filters $filters ) : self {
101100
* @since $ver$
102101
*/
103102
public function sort_by( ?Sort $sort ) : self {
104-
$this->calls[] = [ __METHOD__, ...func_get_args() ];
103+
$this->calls[] = [ __FUNCTION__, ...func_get_args() ];
105104

106105
$this->inner = $this->inner->sort_by( $sort );
107106

@@ -113,7 +112,7 @@ public function sort_by( ?Sort $sort ) : self {
113112
* @since $ver$
114113
*/
115114
public function search_by( ?Search $search ) : self {
116-
$this->calls[] = [ __METHOD__, ...func_get_args() ];
115+
$this->calls[] = [ __FUNCTION__, ...func_get_args() ];
117116
$this->inner = $this->inner->search_by( $search );
118117

119118
return $this;
@@ -143,7 +142,7 @@ public function reset() : void {
143142
* @since $ver$
144143
*/
145144
public function count() : int {
146-
$this->calls[] = [ __METHOD__ ];
145+
$this->calls[] = [ __FUNCTION__ ];
147146

148147
return $this->inner->count();
149148
}
@@ -156,7 +155,7 @@ public function can_delete() : bool {
156155
if ( ! $this->inner instanceof MutableDataSource ) {
157156
return false;
158157
}
159-
$this->calls[] = [ __METHOD__ ];
158+
$this->calls[] = [ __FUNCTION__ ];
160159

161160
return $this->inner->can_delete();
162161
}
@@ -170,7 +169,7 @@ public function delete_data_by_id( string ...$ids ) : void {
170169
return;
171170
}
172171

173-
$this->calls[] = [ __METHOD__ ];
172+
$this->calls[] = [ __FUNCTION__ ];
174173

175174
$this->inner->delete_data_by_id( ...$ids );
176175
}

0 commit comments

Comments
 (0)