Skip to content

Commit

Permalink
Merge pull request #34 from Laragear/feat/custom-hash
Browse files Browse the repository at this point in the history
[4.x] Adds support for custom query hasher function.
  • Loading branch information
DarkGhostHunter authored Dec 15, 2024
2 parents 377a870 + 9dafdc4 commit 49495f6
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,29 @@ CacheQuery::forget('latest_articles');

> This functionality does not use cache tags, so it will work on any cache store you set, even the `file` driver!
## Custom Hash Function

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.

This can be done in the `register()` method of your `AppServiceProvider`.

```php
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Laragear\CacheQuery\CacheAwareConnectionProxy;

class AppServiceProvider extends ServiceProvider
{
public function register()
{
CacheAwareConnectionProxy::$queryHasher = function ($connection, $query, $bindings) {
// ...
}
}
}
```

## Configuration

To further configure the package, publish the configuration file:
Expand Down
12 changes: 11 additions & 1 deletion src/CacheAwareConnectionProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laragear\CacheQuery;

use Closure;
use DateInterval;
use DateTimeInterface;
use Illuminate\Cache\NoLock;
Expand All @@ -24,6 +25,13 @@

class CacheAwareConnectionProxy extends Connection
{
/**
* The Query Hasher closure.
*
* @var (\Closure(\Illuminate\Database\ConnectionInterface, string, array): string)|null
*/
public static ?Closure $queryHasher = null;

/**
* Create a new Cache Aware Connection Proxy instance.
*
Expand Down Expand Up @@ -105,7 +113,9 @@ public function selectOne($query, $bindings = [], $useReadPdo = true)
*/
protected function getQueryHash(string $query, array $bindings): string
{
return rtrim(base64_encode(md5($this->connection->getDatabaseName().$query.implode('', $bindings), true)), '=');
return isset(static::$queryHasher)
? (static::$queryHasher)($this->connection, $query, $bindings)
: rtrim(base64_encode(md5($this->connection->getDatabaseName().$query.implode('', $bindings), true)), '=');
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/CacheAwareConnectionProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ protected function setUp(): void
'title' => $this->faker->text(20),
'user_id' => (int) floor(max(1, $i / 2)),
])->toArray());

CacheAwareConnectionProxy::$queryHasher = null;
});

parent::setUp();
Expand Down Expand Up @@ -670,6 +672,25 @@ public function test_select_one_uses_cache(): void

static::assertSame($results, $retrieved);
}

public function test_sets_custom_query_hasher(): void
{
CacheAwareConnectionProxy::$queryHasher = function (
ConnectionInterface $connection,
string $query,
array $bindings
) use (&$args) {
static::assertSame(':memory:', $connection->getDatabaseName());
static::assertSame('select * from "users" where "users"."id" = ? limit 1', $query);
static::assertSame([0 => 1], $bindings);

return 'test_hash';
};

User::query()->cache('foo')->whereKey(1)->first();

static::assertTrue($this->app->make('cache')->has('cache-query|test_hash'));
}
}

class User extends Authenticatable
Expand Down

0 comments on commit 49495f6

Please sign in to comment.