Skip to content

Commit

Permalink
Remove cache
Browse files Browse the repository at this point in the history
  • Loading branch information
BadJacky committed May 20, 2024
1 parent f135952 commit 9a023d6
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 122 deletions.
25 changes: 2 additions & 23 deletions src/Exceptions/ConstantException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace EverForge\ThinkConstant\Exceptions;

use Psr\SimpleCache\CacheInterface;
use ReflectionClass;
use think\Exception;
use think\helper\Str;
Expand Down Expand Up @@ -42,15 +41,9 @@ protected static function getConstantMap(): array

public function getErrorMessage(int $code): string
{
$cache = static::getCache();
if ($cache->has($cacheKey = static::getCacheKey())) {
return $cache->get($cacheKey)[$code] ?? 'Unknown error code';
} else {
$constantMap = static::getConstantMap();
$cache->set($cacheKey, $constantMap);
$constantMap = static::getConstantMap();
return $constantMap[$code] ?? 'Unknown error code';

return $constantMap[$code] ?? 'Unknown error code';
}
}

private static function parseAnnotationsFromDocComment($docComment): array
Expand All @@ -64,18 +57,4 @@ private static function parseAnnotationsFromDocComment($docComment): array

return $parsed_annotations;
}

/**
* @return CacheInterface
*/
protected static function getCache(): CacheInterface
{
/* @var \think\Cache $cache */
return \app()->get('cache');
}

protected static function getCacheKey(): string
{
return Str::snake(class_basename(static::class));
}
}
4 changes: 0 additions & 4 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@
use EverForge\ThinkConstant\Tests\Support\SimpleCache;

require \dirname(__DIR__, 1) . '/vendor/topthink/framework/src/helper.php';

\app()->bind('cache', function () {
return new SimpleCache();
});
83 changes: 0 additions & 83 deletions tests/Support/SimpleCache.php

This file was deleted.

13 changes: 1 addition & 12 deletions tests/TestExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,4 @@
expect($e)
->toBeInstanceOf(ConstantException::class)
->and($e->getMessage())->toBe('Unknown error code');
});

test('cache is useful', function () {
try {
throw new TestException(TestConstant::SERVER_ERROR_FAILED_PARSED);
} catch (TestException $e) {
}
$cache_data = \app()->get('cache')->get('test_exception');
expect($cache_data)->toBeArray()
->and($cache_data[TestConstant::SERVER_ERROR])->toBe('失败')
->and($cache_data[TestConstant::SERVER_ERROR_FAILED_PARSED])->toBe('Unknown error code');
});
});

0 comments on commit 9a023d6

Please sign in to comment.