Skip to content

Commit 49495f6

Browse files
Merge pull request #34 from Laragear/feat/custom-hash
[4.x] Adds support for custom query hasher function.
2 parents 377a870 + 9dafdc4 commit 49495f6

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,29 @@ CacheQuery::forget('latest_articles');
164164

165165
> This functionality does not use cache tags, so it will work on any cache store you set, even the `file` driver!
166166
167+
## Custom Hash Function
168+
169+
You can set your own function to hash the incoming SQL Query. Just register your function in the `$queryHasher` static property of the `CacheAwareConnectionProxy` class. The function should receive the database Connection, the query string, and the SQL bindings in form of an array.
170+
171+
This can be done in the `register()` method of your `AppServiceProvider`.
172+
173+
```php
174+
namespace App\Providers;
175+
176+
use Illuminate\Support\ServiceProvider;
177+
use Laragear\CacheQuery\CacheAwareConnectionProxy;
178+
179+
class AppServiceProvider extends ServiceProvider
180+
{
181+
public function register()
182+
{
183+
CacheAwareConnectionProxy::$queryHasher = function ($connection, $query, $bindings) {
184+
// ...
185+
}
186+
}
187+
}
188+
```
189+
167190
## Configuration
168191

169192
To further configure the package, publish the configuration file:

src/CacheAwareConnectionProxy.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Laragear\CacheQuery;
44

5+
use Closure;
56
use DateInterval;
67
use DateTimeInterface;
78
use Illuminate\Cache\NoLock;
@@ -24,6 +25,13 @@
2425

2526
class CacheAwareConnectionProxy extends Connection
2627
{
28+
/**
29+
* The Query Hasher closure.
30+
*
31+
* @var (\Closure(\Illuminate\Database\ConnectionInterface, string, array): string)|null
32+
*/
33+
public static ?Closure $queryHasher = null;
34+
2735
/**
2836
* Create a new Cache Aware Connection Proxy instance.
2937
*
@@ -105,7 +113,9 @@ public function selectOne($query, $bindings = [], $useReadPdo = true)
105113
*/
106114
protected function getQueryHash(string $query, array $bindings): string
107115
{
108-
return rtrim(base64_encode(md5($this->connection->getDatabaseName().$query.implode('', $bindings), true)), '=');
116+
return isset(static::$queryHasher)
117+
? (static::$queryHasher)($this->connection, $query, $bindings)
118+
: rtrim(base64_encode(md5($this->connection->getDatabaseName().$query.implode('', $bindings), true)), '=');
109119
}
110120

111121
/**

tests/CacheAwareConnectionProxyTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ protected function setUp(): void
4242
'title' => $this->faker->text(20),
4343
'user_id' => (int) floor(max(1, $i / 2)),
4444
])->toArray());
45+
46+
CacheAwareConnectionProxy::$queryHasher = null;
4547
});
4648

4749
parent::setUp();
@@ -670,6 +672,25 @@ public function test_select_one_uses_cache(): void
670672

671673
static::assertSame($results, $retrieved);
672674
}
675+
676+
public function test_sets_custom_query_hasher(): void
677+
{
678+
CacheAwareConnectionProxy::$queryHasher = function (
679+
ConnectionInterface $connection,
680+
string $query,
681+
array $bindings
682+
) use (&$args) {
683+
static::assertSame(':memory:', $connection->getDatabaseName());
684+
static::assertSame('select * from "users" where "users"."id" = ? limit 1', $query);
685+
static::assertSame([0 => 1], $bindings);
686+
687+
return 'test_hash';
688+
};
689+
690+
User::query()->cache('foo')->whereKey(1)->first();
691+
692+
static::assertTrue($this->app->make('cache')->has('cache-query|test_hash'));
693+
}
673694
}
674695

675696
class User extends Authenticatable

0 commit comments

Comments
 (0)