mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
perf: cache geo and audit attribution lookups
This commit is contained in:
1 parent
e9b5b58282
commit
5f92312419
3 files changed
+53
-2
No files matched your search
@@ -30,6 +30,8 @@ final class Audit extends Base
|
||||
'$id' => $this->project->getId(),
|
||||
'$sequence' => $this->project->getSequence(),
|
||||
'database' => $this->project->getAttribute('database', ''),
|
||||
'teamId' => $this->project->getAttribute('teamId', ''),
|
||||
'teamInternalId' => $this->project->getAttribute('teamInternalId', ''),
|
||||
],
|
||||
'user' => $this->user->getArrayCopy(),
|
||||
'impersonatorUser' => $this->impersonatorUser->getArrayCopy(),
|
||||
|
||||
@@ -11,7 +11,7 @@ use Utopia\Locale\Locale;
|
||||
class Geo
|
||||
{
|
||||
public const CACHE_SIZE = 10_000;
|
||||
public const CACHE_VALUE_SIZE = 512;
|
||||
public const CACHE_VALUE_SIZE = 1024;
|
||||
|
||||
public function __construct(
|
||||
private ?Client $client,
|
||||
@@ -77,7 +77,8 @@ class Geo
|
||||
'weatherCode' => $record['weatherCode'] ?? null,
|
||||
'postalCode' => $record['postalCode'] ?? null,
|
||||
'city' => $record['city']['en'] ?? null,
|
||||
'state' => $record['subdivision']['en'] ?? $record['subdivisions'][0]['en'] ?? null,
|
||||
'state' => $record['subdivision']['en'] ?? $record['subdivisions'][0]['en'] ?? $record['subdivisions'][0]['names']['en'] ?? null,
|
||||
'subdivisions' => $record['subdivisions'] ?? null,
|
||||
'autonomousSystemNumber' => $autonomousSystemNumber === null ? null : (string) $autonomousSystemNumber,
|
||||
'autonomousSystemOrganization' => $record['autonomousSystemOrganization'] ?? null,
|
||||
'connectionType' => $record['connection'] ?? null,
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit\Geo;
|
||||
|
||||
use Appwrite\Geo\Client;
|
||||
use Appwrite\Geo\Geo;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Swoole\Table;
|
||||
use Utopia\Config\Config;
|
||||
use Utopia\Locale\Locale;
|
||||
|
||||
final class GeoTest extends TestCase
|
||||
{
|
||||
public function testCachePreservesPremiumGeoAttributes(): void
|
||||
{
|
||||
Config::setParam('locale-eu', []);
|
||||
Config::setParam('locale-currencies', []);
|
||||
Locale::setLanguageFromArray('en', []);
|
||||
|
||||
$client = $this->createMock(Client::class);
|
||||
$client->expects($this->once())
|
||||
->method('lookup')
|
||||
->with('203.0.113.10')
|
||||
->willReturn([
|
||||
'countryCode' => 'DE',
|
||||
'continentCode' => 'EU',
|
||||
'city' => ['en' => 'Frankfurt'],
|
||||
'subdivisions' => [
|
||||
['iso_code' => 'HE', 'names' => ['en' => 'Hesse']],
|
||||
],
|
||||
'isp' => 'Example ISP',
|
||||
]);
|
||||
|
||||
$cache = new Table(16);
|
||||
$cache->column('value', Table::TYPE_STRING, Geo::CACHE_VALUE_SIZE);
|
||||
$cache->create();
|
||||
|
||||
$geo = new Geo($client, new Locale('en'), $cache);
|
||||
$geo->get('203.0.113.10');
|
||||
$cached = $geo->get('203.0.113.10');
|
||||
|
||||
$this->assertSame('Frankfurt', $cached->getAttribute('city'));
|
||||
$this->assertSame('Hesse', $cached->getAttribute('subdivisions')[0]['names']['en']);
|
||||
$this->assertSame('Example ISP', $cached->getAttribute('isp'));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user