Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use octane as cache when available #29

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed
- Use [octane](https://laravel.com/docs/master/octane) as cache when available.

## [6.0.0] - 2023-03-01

Expand Down
16 changes: 13 additions & 3 deletions src/TokenCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Butler\Auth;

use Illuminate\Cache\Repository;
use Illuminate\Support\Facades\Cache;

class TokenCache
Expand All @@ -13,16 +14,25 @@ public function key(string $hashedToken): string

public function get(string $hashedToken): ?AccessToken
{
return Cache::get($this->key($hashedToken));
return $this->cache()->get($this->key($hashedToken));
}

public function put(AccessToken $token): bool
{
return Cache::put($this->key($token->token), $token, now()->addDay());
return $this->cache()->put(
$this->key($token->token),
$token,
now()->addDay(),
);
}

public function forget($hashedToken): bool
{
return Cache::forget($this->key($hashedToken));
return $this->cache()->forget($this->key($hashedToken));
}

private function cache(): Repository
{
return Cache::store(getenv('LARAVEL_OCTANE') ? 'octane' : null);
ttrig marked this conversation as resolved.
Show resolved Hide resolved
}
}
11 changes: 5 additions & 6 deletions tests/GuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Butler\Auth\Tests\Models\ConsumerWithoutTokenSupport;
use Butler\Auth\Tests\Models\ConsumerWithTokenSupport;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Queue;
use Mockery;

Expand Down Expand Up @@ -64,7 +63,7 @@ public function test_authentication_is_successful_with_valid_token()

public function test_UpdateAccessTokensLastUsed_job_is_dispatched_with_delay()
{
$this->travelTo(Date::parse('2021-05-25 12:00:00'));
$this->travelTo('2021-05-25 12:00:00');

ConsumerWithTokenSupport::create()
->tokens()
Expand All @@ -79,7 +78,7 @@ public function test_UpdateAccessTokensLastUsed_job_is_dispatched_with_delay()

public function test_access_token_is_cached_correctly_when_found_in_database()
{
$this->travelTo(Date::parse('2021-05-25 12:00:00'));
$this->travelTo('2021-05-25 12:00:00');

$consumer = ConsumerWithTokenSupport::create();
$token = $consumer->tokens()->create([
Expand Down Expand Up @@ -113,12 +112,12 @@ public function test_access_token_is_retrieved_from_cache_when_found_in_cache()
public function test_authentication_is_successful_even_if_cache_throws_exception()
{
$consumer = ConsumerWithTokenSupport::create();
$token = $consumer->tokens()->create(['token' => hash('sha256', 'secret')]);
$consumer->tokens()->create(['token' => hash('sha256', 'secret')]);

$exception = new \Exception('Could not connect to redis');

TokenCache::shouldReceive('get')->once()->andThrow($exception);
TokenCache::shouldReceive('put')->once()->andThrow($exception);
TokenCache::expects('get')->andThrow($exception);
TokenCache::expects('put')->andThrow($exception);

$returnedConsumer = (new Guard())->__invoke($this->makeRequest());

Expand Down
43 changes: 37 additions & 6 deletions tests/TokenCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,26 @@

use Butler\Auth\AccessToken;
use Butler\Auth\TokenCache;
use Illuminate\Cache\Repository;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Date;
use Mockery;

class TokenCacheTest extends TestCase
{
private $repository;

public function setUp(): void
{
parent::setUp();

$this->repository = $this->mock(Repository::class);

Cache::shouldReceive('store')
->with(null)
->andReturn($this->repository)
->byDefault();
}

public function test_key()
{
$this->assertEquals(
Expand All @@ -23,11 +37,11 @@ public function test_get()
$tokenCache = new TokenCache();
$token = new AccessToken(['token' => 'foo']);

Cache::shouldReceive('get')
$this->repository->expects('get')
->with($tokenCache->key('foo'))
->andReturn($token);

Cache::shouldReceive('get')
$this->repository->expects('get')
->with($tokenCache->key('bar'))
->andReturnNull();

Expand All @@ -37,12 +51,12 @@ public function test_get()

public function test_put()
{
$this->travelTo(Date::parse('2021-05-29 12:00:00'));
$this->travelTo('2021-05-29 12:00:00');

$tokenCache = new TokenCache();
$token = new AccessToken(['token' => 'foo']);

Cache::shouldReceive('put')
$this->repository->expects('put')
->with(
$tokenCache->key('foo'),
$token,
Expand All @@ -57,10 +71,27 @@ public function test_forget()
{
$tokenCache = new TokenCache();

Cache::shouldReceive('forget')
$this->repository->expects('forget')
->with($tokenCache->key('foo'))
->andReturnTrue();

$this->assertTrue($tokenCache->forget('foo'));
}

public function test_octane_is_used_if_running()
{
putenv('LARAVEL_OCTANE=1');

$tokenCache = new TokenCache();

Cache::expects('store')->with('octane')->andReturn($this->repository);

$this->repository->expects('forget')
->with($tokenCache->key('foo'))
->andReturnTrue();

$this->assertTrue($tokenCache->forget('foo'));

putenv('LARAVEL_OCTANE=0');
}
}