Skip to content

Commit 6b08be8

Browse files
cursoragentabnegate
andcommitted
Switch to xxh3 hashing for cache keys with PHP version fallback
Co-authored-by: jakeb994 <jakeb994@gmail.com>
1 parent a6054bd commit 6b08be8

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

CACHE_IMPLEMENTATION_SUMMARY.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# Find Method Caching Implementation Summary
22

33
## Overview
4-
This implementation adds efficient caching to the `find` method in the Database class using FNV164 hashing for consistent cache keys and version tracking for O(1) cache invalidation.
4+
This implementation adds efficient caching to the `find` method in the Database class using xxh3 hashing for consistent cache keys and version tracking for O(1) cache invalidation.
55

66
## Key Features
77

8-
### 1. FNV164 Hash Function
8+
### 1. xxh3 Hash Function
99
- **Purpose**: Generate consistent and efficient hash keys for complex query parameters
10-
- **Implementation**: 64-bit FNV-1a hash algorithm with PHP-specific optimizations
11-
- **Location**: `fnv164Hash()` method in Database.php
12-
- **Benefits**: Fast, consistent hashing with good distribution characteristics
10+
- **Implementation**: PHP's built-in xxh3 hash algorithm via `hash()` function (PHP 8.1+)
11+
- **Fallback**: SHA256 for PHP versions < 8.1
12+
- **Location**: `generateCacheHash()` method in Database.php
13+
- **Benefits**: Extremely fast, well-tested hashing with excellent distribution characteristics
1314

1415
### 2. Version Tracking for O(1) Invalidation
1516
- **Purpose**: Enable aggressive cache invalidation without expensive cache scanning
@@ -18,7 +19,7 @@ This implementation adds efficient caching to the `find` method in the Database
1819
- **Benefits**: O(1) invalidation time complexity
1920

2021
### 3. Find Method Caching
21-
- **Cache Key Generation**: Uses FNV164 hash of all query parameters plus collection version
22+
- **Cache Key Generation**: Uses xxh3 hash of all query parameters plus collection version
2223
- **Cache Storage**: Results are stored as arrays and converted back to Document objects on retrieval
2324
- **Cache Validation**: Version-based keys ensure stale data is never returned
2425
- **Safety**: Only caches results without relationships to avoid incomplete data
@@ -33,9 +34,8 @@ This implementation adds efficient caching to the `find` method in the Database
3334

3435
### Constants Added
3536
```php
36-
// FNV-1a 64-bit constants
37-
private const FNV164_PRIME = 0x100000001b3;
38-
private const FNV164_OFFSET_BASIS = 0xcbf29ce484222325;
37+
// Hash algorithm for cache keys
38+
private const CACHE_HASH_ALGO = 'xxh3';
3939
```
4040

4141
### New Properties
@@ -47,7 +47,7 @@ protected array $collectionVersions = [];
4747
```
4848

4949
### New Methods
50-
1. `fnv164Hash(string $data): string` - FNV164 hash implementation
50+
1. `generateCacheHash(string $data): string` - xxh3 hash implementation using PHP's built-in hash function
5151
2. `getFindCacheKey(...)` - Generate cache keys for find queries
5252
3. `getCollectionVersion(string $collectionId): int` - Get/initialize collection version
5353
4. `incrementCollectionVersion(string $collectionId): void` - Increment version for invalidation
@@ -76,7 +76,7 @@ default-cache-:::find:users:7a8b9c2d1e3f4567:v1691234567
7676
- **Network**: Single cache read operation
7777

7878
### Cache Miss Performance
79-
- **Additional Overhead**: FNV164 hash calculation (~O(k) where k is query string length)
79+
- **Additional Overhead**: xxh3/sha256 hash calculation (~O(k) where k is query string length)
8080
- **Cache Write**: Single operation after query execution
8181
- **No degradation**: Database query performance unchanged
8282

@@ -122,6 +122,7 @@ $results3 = $database->find('users', [
122122
- **TTL**: Uses existing `Database::TTL` constant (24 hours)
123123
- **Version Storage**: 1-year TTL for version numbers
124124
- **Cache Backend**: Uses existing cache infrastructure
125+
- **Hash Algorithm**: xxh3 (PHP 8.1+) with SHA256 fallback for older versions
125126

126127
## Monitoring
127128

src/Database/Database.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,8 @@ class Database
108108
// Cache
109109
public const TTL = 60 * 60 * 24; // 24 hours
110110

111-
// FNV-1a 64-bit constants
112-
private const FNV164_PRIME = 0x100000001b3;
113-
private const FNV164_OFFSET_BASIS = 0xcbf29ce484222325;
111+
// Hash algorithm for cache keys
112+
private const CACHE_HASH_ALGO = 'xxh3';
114113

115114
// Events
116115
public const EVENT_ALL = '*';
@@ -6141,7 +6140,7 @@ public function find(string $collection, array $queries = [], string $forPermiss
61416140
$selections = $this->validateSelections($collection, $selects);
61426141
$nestedSelections = $this->processRelationshipQueries($relationships, $queries);
61436142

6144-
// Generate cache key using FNV164 hash
6143+
// Generate cache key using xxh3 hash
61456144
$cacheKey = $this->getFindCacheKey(
61466145
$collection->getId(),
61476146
$queries,
@@ -6983,26 +6982,24 @@ private function processRelationshipQueries(
69836982
}
69846983

69856984
/**
6986-
* Generate FNV164 hash for consistent cache keys
6985+
* Generate xxh3 hash for consistent cache keys
69876986
*
69886987
* @param string $data
69896988
* @return string
69906989
*/
6991-
private function fnv164Hash(string $data): string
6990+
private function generateCacheHash(string $data): string
69926991
{
6993-
$hash = self::FNV164_OFFSET_BASIS;
6994-
$length = \strlen($data);
6995-
6996-
for ($i = 0; $i < $length; $i++) {
6997-
$hash ^= \ord($data[$i]);
6998-
$hash = ($hash * self::FNV164_PRIME) & 0x7FFFFFFFFFFFFFFF; // Keep it within PHP int limits
6992+
// Use xxh3 if available (PHP 8.1+), fallback to sha256 for compatibility
6993+
if (\in_array(self::CACHE_HASH_ALGO, \hash_algos())) {
6994+
return \hash(self::CACHE_HASH_ALGO, $data);
69996995
}
70006996

7001-
return \dechex($hash);
6997+
// Fallback to sha256 for older PHP versions
6998+
return \hash('sha256', $data);
70026999
}
70037000

70047001
/**
7005-
* Generate cache key for find queries using FNV164 hash
7002+
* Generate cache key for find queries using xxh3 hash
70067003
*
70077004
* @param string $collectionId
70087005
* @param array $queries
@@ -7040,7 +7037,7 @@ private function getFindCacheKey(
70407037
];
70417038

70427039
$queryString = \json_encode($queryData, JSON_SORT_KEYS);
7043-
$queryHash = $this->fnv164Hash($queryString);
7040+
$queryHash = $this->generateCacheHash($queryString);
70447041

70457042
if ($this->adapter->getSupportForHostname()) {
70467043
$hostname = $this->adapter->getHostname();

0 commit comments

Comments
 (0)