Skip to content

Commit

Permalink
Rewrite cached resolver logic to allow for cache invalidation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
stancl committed Jun 2, 2020
1 parent b176481 commit 5d94727
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 117 deletions.
24 changes: 12 additions & 12 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.github export-ignore
/art export-ignore
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.styleci.yml export-ignore
/.github export-ignore
/art export-ignore
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.styleci.yml export-ignore
/docker-compose.yml export-ignore
/Dockerfile export-ignore
/fulltest export-ignore
/phpunit.xml export-ignore
/.editorconfig export-ignore
/.coverage.xml export-ignore
/coverage export-ignore
/Dockerfile export-ignore
/test export-ignore
/phpunit.xml export-ignore
/.editorconfig export-ignore
/.coverage.xml export-ignore
/coverage export-ignore
4 changes: 4 additions & 0 deletions src/Contracts/Tenant.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

/**
* @see \Stancl\Tenancy\Database\Models\Tenant
*
* @method mixed __call() IDE support. This will be a model.
* @method mixed __callStatic() IDE support. This will be a model.
* @mixin \Illuminate\Database\Eloquent\Model
*/
interface Tenant
{
Expand Down
28 changes: 28 additions & 0 deletions src/Database/Concerns/InvalidatesResolverCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Stancl\Tenancy\Database\Concerns;

use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Resolvers\Contracts\CachedTenantResolver;
use Stancl\Tenancy\Resolvers;

trait InvalidatesResolverCache
{
public static $resolvers = [
Resolvers\DomainTenantResolver::class,
Resolvers\PathTenantResolver::class,
Resolvers\RequestDataTenantResolver::class,
];

public static function bootInvalidatesResolverCache()
{
static::saved(function (Tenant $tenant) {
foreach (static::$resolvers as $resolver) {
/** @var CachedTenantResolver $resolver */
$resolver = app($resolver);

$resolver->invalidateCache($tenant);
}
});
}
}
32 changes: 32 additions & 0 deletions src/Database/Concerns/InvalidatesTenantsResolverCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Stancl\Tenancy\Database\Concerns;

use Illuminate\Database\Eloquent\Model;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Resolvers\Contracts\CachedTenantResolver;
use Stancl\Tenancy\Resolvers;

/**
* Meant to be used on models that belong to tenants.
*/
trait InvalidatesTenantsResolverCache
{
public static $resolvers = [
Resolvers\DomainTenantResolver::class,
Resolvers\PathTenantResolver::class,
Resolvers\RequestDataTenantResolver::class,
];

public static function bootInvalidatesTenantsResolverCache()
{
static::saved(function (Model $model) {
foreach (static::$resolvers as $resolver) {
/** @var CachedTenantResolver $resolver */
$resolver = app($resolver);

$resolver->invalidateCache($model->tenant);
}
});
}
}
3 changes: 2 additions & 1 deletion src/Database/Models/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
class Domain extends Model implements Contracts\Domain
{
use Concerns\CentralConnection,
Concerns\EnsuresDomainIsNotOccupied;
Concerns\EnsuresDomainIsNotOccupied,
Concerns\InvalidatesTenantsResolverCache;

protected $guarded = [];

Expand Down
3 changes: 2 additions & 1 deletion src/Database/Models/Tenant.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class Tenant extends Model implements Contracts\Tenant
Concerns\GeneratesIds,
Concerns\HasDataColumn,
Concerns\HasInternalKeys,
Concerns\TenantRun;
Concerns\TenantRun,
Concerns\InvalidatesResolverCache;

protected $table = 'tenants';
protected $primaryKey = 'id';
Expand Down
21 changes: 3 additions & 18 deletions src/Middleware/IdentificationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ abstract class IdentificationMiddleware
/** @var callable */
public static $onFail;

/** @var bool */
public static $shouldCache = false;

/** @var int */
public static $cacheTTL = 3600; // seconds

/** @var string|null */
public static $cacheStore = null; // default

/** @var Tenancy */
protected $tenancy;

Expand All @@ -32,15 +23,9 @@ abstract class IdentificationMiddleware
public function initializeTenancy($request, $next, ...$resolverArguments)
{
try {
if (static::$shouldCache) {
app(CachedTenantResolver::class)->resolve(
get_class($this->resolver), $resolverArguments, static::$cacheTTL, static::$cacheStore
);
} else {
$this->tenancy->initialize(
$this->resolver->resolve(...$resolverArguments)
);
}
$this->tenancy->initialize(
$this->resolver->resolve(...$resolverArguments)
);
} catch (TenantCouldNotBeIdentifiedException $e) {
$onFail = static::$onFail ?? function ($e) {
throw $e;
Expand Down
9 changes: 0 additions & 9 deletions src/Middleware/InitializeTenancyByDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ class InitializeTenancyByDomain extends IdentificationMiddleware
/** @var callable|null */
public static $onFail;

/** @var bool */
public static $shouldCache = false;

/** @var int */
public static $cacheTTL = 3600; // seconds

/** @var string|null */
public static $cacheStore = null; // default

/** @var Tenancy */
protected $tenancy;

Expand Down
9 changes: 0 additions & 9 deletions src/Middleware/InitializeTenancyByPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ class InitializeTenancyByPath extends IdentificationMiddleware
/** @var callable|null */
public static $onFail;

/** @var bool */
public static $shouldCache = false;

/** @var int */
public static $cacheTTL = 3600; // seconds

/** @var string|null */
public static $cacheStore = null; // default

/** @var Tenancy */
protected $tenancy;

Expand Down
9 changes: 0 additions & 9 deletions src/Middleware/InitializeTenancyByRequestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware
/** @var callable|null */
public static $onFail;

/** @var bool */
public static $shouldCache = false;

/** @var int */
public static $cacheTTL = 3600; // seconds

/** @var string|null */
public static $cacheStore = null; // default

/** @var Tenancy */
protected $tenancy;

Expand Down
9 changes: 0 additions & 9 deletions src/Middleware/InitializeTenancyBySubdomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ class InitializeTenancyBySubdomain extends InitializeTenancyByDomain
/** @var callable|null */
public static $onFail;

/** @var bool */
public static $shouldCache = false;

/** @var int */
public static $cacheTTL = 3600; // seconds

/** @var string|null */
public static $cacheStore = null; // default

/**
* Handle an incoming request.
*
Expand Down
43 changes: 0 additions & 43 deletions src/Resolvers/CachedTenantResolver.php

This file was deleted.

70 changes: 70 additions & 0 deletions src/Resolvers/Contracts/CachedTenantResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Stancl\Tenancy\Resolvers\Contracts;

use Illuminate\Contracts\Cache\Factory;
use Illuminate\Contracts\Cache\Repository;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Contracts\TenantResolver;
use Stancl\Tenancy\Database\Models\Domain;
use Stancl\Tenancy\Resolvers\DomainTenantResolver;

abstract class CachedTenantResolver implements TenantResolver
{
/** @var bool */
public static $shouldCache = false;

/** @var int */
public static $cacheTTL = 3600; // seconds

/** @var string|null */
public static $cacheStore = null; // default

/** @var Repository */
protected $cache;

public function __construct(Factory $cache)
{
$this->cache = $cache->store(static::$cacheStore);
}

public function resolve(...$args): Tenant
{
if (! static::$shouldCache) {
return $this->resolveWithoutCache(...$args);
}

$key = $this->getCacheKey(...$args);

if ($this->cache->has($key)) {
return $this->cache->get($key);
}

$tenant = $this->resolveWithoutCache(...$args);
$this->cache->put($key, $tenant, static::$cacheTTL);

return $tenant;
}

public function invalidateCache(Tenant $tenant): void
{
foreach ($this->getArgsForTenant($tenant) as $args) {
$this->cache->forget($this->getCacheKey(...$args));
}
}

public function getCacheKey(...$args): string
{
return '_tenancy_resolver:' . static::class . ':' . json_encode($args);
}

abstract public function resolveWithoutCache(...$args): Tenant;

/**
* Get all the arg combinations for resolve() that can be used to find this tenant.
*
* @param Tenant $tenant
* @return array[]
*/
abstract public function getArgsForTenant(Tenant $tenant): array;
}
21 changes: 18 additions & 3 deletions src/Resolvers/PathTenantResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@

use Illuminate\Routing\Route;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Contracts\TenantResolver;
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedByPathException;

class PathTenantResolver implements TenantResolver
class PathTenantResolver extends Contracts\CachedTenantResolver
{
public static $tenantParameterName = 'tenant';

public function resolve(...$args): Tenant
/** @var bool */
public static $shouldCache = false;

/** @var int */
public static $cacheTTL = 3600; // seconds

/** @var string|null */
public static $cacheStore = null; // default

public function resolveWithoutCache(...$args): Tenant
{
/** @var Route $route */
$route = $args[0];
Expand All @@ -28,4 +36,11 @@ public function resolve(...$args): Tenant

throw new TenantCouldNotBeIdentifiedByPathException($id);
}

public function getArgsForTenant(Tenant $tenant): array
{
return [
[$tenant->id],
];
}
}
Loading

0 comments on commit 5d94727

Please sign in to comment.