Skip to content

Commit d0941f1

Browse files
authored
feat: implement configurable avatar caching (#170)
- Add option to enable/disable caching - Add configurable cache duration (default: 24 hours) - Add cache key prefix to avoid conflicts - Update toBase64() method to respect cache configuration - Document cache options in config file This fixes the issue where avatars were cached forever with no way to configure or disable the cache, making it difficult to see setting changes.
1 parent 9d8a568 commit d0941f1

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

config/config.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,26 @@
1919
*/
2020
'driver' => env('IMAGE_DRIVER', 'gd'),
2121

22+
/*
23+
|--------------------------------------------------------------------------
24+
| Cache Configuration
25+
|--------------------------------------------------------------------------
26+
| Control caching behavior for avatars
27+
|
28+
*/
29+
'cache' => [
30+
// Set to true to enable caching, false to disable
31+
'enabled' => env('AVATAR_CACHE_ENABLED', true),
32+
33+
// Cache prefix to avoid conflicts with other cached items
34+
'key_prefix' => 'avatar_',
35+
36+
// Cache duration in seconds
37+
// Set to null to cache forever, 0 to disable cache
38+
// Default: 86400 (24 hours)
39+
'duration' => env('AVATAR_CACHE_DURATION', 86400),
40+
],
41+
2242
// Initial generator class
2343
'generator' => \Laravolt\Avatar\Generator\DefaultGenerator::class,
2444

src/Avatar.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ class Avatar
7878

7979
protected array $defaultTheme = [];
8080

81+
// Cache configuration properties
82+
protected bool $cacheEnabled = true;
83+
protected string $cacheKeyPrefix = 'avatar_';
84+
protected ?int $cacheDuration = 86400; // 24 hours by default
85+
8186
/**
8287
* Avatar constructor.
8388
*
@@ -93,6 +98,13 @@ public function __construct(array $config = [], ?Repository $cache = null)
9398
$this->applyTheme($this->defaultTheme);
9499
$this->initialGenerator = new DefaultGenerator();
95100

101+
// Set up cache configuration
102+
if (isset($config['cache'])) {
103+
$this->cacheEnabled = $config['cache']['enabled'] ?? true;
104+
$this->cacheKeyPrefix = $config['cache']['key_prefix'] ?? 'avatar_';
105+
$this->cacheDuration = $config['cache']['duration'] ?? 86400; // 24 hours by default
106+
}
107+
96108
// Add any additional themes for further use
97109
$themes = $this->resolveTheme('*', $config['themes'] ?? []);
98110
foreach ($themes as $name => $conf) {
@@ -183,16 +195,31 @@ protected function resolveTheme(array|string|null $theme, array $cfg): array
183195

184196
public function toBase64(): string
185197
{
186-
$key = $this->cacheKey();
198+
if (!$this->cacheEnabled) {
199+
// Skip cache if it's disabled
200+
$this->buildAvatar();
201+
return $this->image->toPng()->toDataUri();
202+
}
203+
204+
$key = $this->cacheKeyPrefix . $this->cacheKey();
205+
206+
// Check if the image is in the cache
187207
if ($base64 = $this->cache->get($key)) {
188208
return $base64;
189209
}
190210

211+
// Generate the avatar
191212
$this->buildAvatar();
192-
193213
$base64 = $this->image->toPng()->toDataUri();
194214

195-
$this->cache->forever($key, $base64);
215+
// Store in cache based on configured duration
216+
if ($this->cacheDuration === null) {
217+
// Cache forever
218+
$this->cache->forever($key, $base64);
219+
} else {
220+
// Cache for specified duration (in seconds)
221+
$this->cache->put($key, $base64, $this->cacheDuration);
222+
}
196223

197224
return $base64;
198225
}

0 commit comments

Comments
 (0)