Skip to content

Commit

Permalink
Add a configurable default ttl and add forever cache variants
Browse files Browse the repository at this point in the history
  • Loading branch information
Kurt Friars committed Jun 10, 2024
1 parent e8a871f commit 5262790
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 16 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

:warning: Package is under active development. Wait for v1.0.0 for production use. :warning:


## Table of Contents

- [Installation](#installation)
Expand All @@ -36,10 +35,10 @@

## Configuration

The package's configuration file is located at `config/publisher.php`. If you did not publish the config file during installation, you can publish the configuration file using the following command:
The package's configuration file is located at `config/model-cache.php`. If you did not publish the config file during installation, you can publish the configuration file using the following command:
```bash
php artisan vendor:publish --provider="Plank\Publisher\PublisherServiceProvider" --tag="config"
php artisan vendor:publish --provider="Plank\ModelCache\ModelCacheServiceProvider" --tag="config"
```
 
Expand Down
4 changes: 2 additions & 2 deletions config/model-cache.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

// config for Plank/ModelCache
return [

'enabled' => env('MODEL_CACHE_ENABLED', true),
'ttl' => env('MODEL_CACHE_TTL', 3600),
];
69 changes: 65 additions & 4 deletions src/Contracts/ManagesCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,18 @@ public static function rememberWithPermissions(
?Permissable $user,
callable $callable,
array $tags = [],
int $ttl = 3600
?int $ttl = null,
): mixed;

/**
* Cache a value forever that will be invalidated whenever any model of this
* type changes, and take into account a user's permissions
*/
public static function rememberWithPermissionsForever(
string $key,
?Permissable $user,
callable $callable,
array $tags = []
): mixed;

/**
Expand All @@ -28,19 +39,69 @@ public static function remember(
string $key,
callable $callable,
array $tags = [],
int $ttl = 3600,
?int $ttl = null,
): mixed;

/**
* Cache something that will be invalidated whenever this particular model changes
* Cache something forever that will be invalidated whenever
* any model of this type changes
*/
public static function rememberForever(
string $key,
callable $callable,
array $tags = [],
): mixed;

/**
* Cache something that will be invalidated whenever this particular
* model changes and take into account a user's permissions
*
* @return mixed
*/
public function rememberOnSelfWithPermissions(
string $key,
?Permissable $user,
callable $callable,
array $tags = [],
?int $ttl = null,
);

/**
* Cache something that will be invalidated whenever this particular
* model changes and take into account a user's permissions
*
* @return mixed
*/
public function rememberOnSelfWithPermissionsForever(
string $key,
?Permissable $user,
callable $callable,
array $tags = []
);

/**
* Cache something that will be invalidated whenever this
* particular model changes
*
* @return mixed
*/
public function rememberOnSelf(
string $key,
callable $callable,
array $tags = [],
int $ttl = 3600
?int $ttl = null,
);

/**
* Cache something forever that will be invalidated whenever
* this particular model changes
*
* @return mixed
*/
public function rememberOnSelfForever(
string $key,
callable $callable,
array $tags = [],
);

/**
Expand Down
98 changes: 91 additions & 7 deletions src/Traits/ModelCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function rememberOnSelfWithPermissions(
?Permissable $user,
Closure $callable,
array $tags = [],
int $ttl = 3600
?int $ttl = null
) {
$permissionsKey = $user instanceof Permissable ? $user->permissionsKey() : 'guest';

Expand All @@ -47,20 +47,34 @@ public function rememberOnSelf(
string $key,
Closure $callable,
array $tags = [],
int $ttl = 3600
?int $ttl = null
) {
return static::remember($key.'-'.$this->getKey(), $callable, [
$ttl = $ttl ?? config()->get('model-cache.ttl', 3600);

if (static::modelCacheDisabled()) {
return $callable();
}

if (! static::cacheSupportsTags()) {
return Cache::remember(static::withCacheKeyPrefix($key), $ttl, $callable);
}

$tags = array_merge(
static::defaultTags(),
$this->instanceCacheTag(),
...$tags,
], $ttl);
array_map([static::class, 'handleTag'], $tags),
);

return Cache::tags($tags)
->remember(static::withCacheKeyPrefix($key), $ttl, $callable);
}

public static function rememberWithPermissions(
string $key,
?Permissable $user,
Closure $callable,
array $tags = [],
int $ttl = 3600
?int $ttl = null
): mixed {
$permissionsKey = $user instanceof Permissable ? $user->permissionsKey() : 'guest';

Expand All @@ -71,8 +85,10 @@ public static function remember(
string $key,
Closure $callable,
array $tags = [],
int $ttl = 3600
?int $ttl = null
): mixed {
$ttl = $ttl ?? config()->get('model-cache.ttl', 3600);

if (static::modelCacheDisabled()) {
return $callable();
}
Expand All @@ -91,6 +107,74 @@ public static function remember(
->remember(static::withCacheKeyPrefix($key), $ttl, $callable);
}

public function rememberOnSelfWithPermissionsForever(
string $key,
?Permissable $user,
Closure $callable,
array $tags = [],
) {
$permissionsKey = $user instanceof Permissable ? $user->permissionsKey() : 'guest';

return $this->rememberOnSelfForever($key.'-'.$permissionsKey, $callable, $tags);
}

public function rememberOnSelfForever(
string $key,
Closure $callable,
array $tags = [],
) {
if (static::modelCacheDisabled()) {
return $callable();
}

if (! static::cacheSupportsTags()) {
return Cache::rememberForever(static::withCacheKeyPrefix($key), $callable);
}

$tags = array_merge(
static::defaultTags(),
$this->instanceCacheTag(),
array_map([static::class, 'handleTag'], $tags),
);

return Cache::tags($tags)
->rememberForever(static::withCacheKeyPrefix($key), $callable);
}

public static function rememberWithPermissionsForever(
string $key,
?Permissable $user,
Closure $callable,
array $tags = [],
): mixed {
$permissionsKey = $user instanceof Permissable ? $user->permissionsKey() : 'guest';

return static::rememberForever($key.'-'.$permissionsKey, $callable, $tags);
}

public static function rememberForever(
string $key,
Closure $callable,
array $tags = [],
): mixed {
if (static::modelCacheDisabled()) {
return $callable();
}

if (! static::cacheSupportsTags()) {
return Cache::rememberForever(static::withCacheKeyPrefix($key), $callable);
}

$tags = array_merge(
static::defaultTags(),
static::modelCacheTag(),
array_map([static::class, 'handleTag'], $tags),
);

return Cache::tags($tags)
->rememberForever(static::withCacheKeyPrefix($key), $callable);
}

/**
* Flush the model's entire cache
*/
Expand Down

0 comments on commit 5262790

Please sign in to comment.