Skip to content

Commit 93c6a63

Browse files
Cache user image path
1 parent 3678fa0 commit 93c6a63

File tree

1 file changed

+77
-15
lines changed

1 file changed

+77
-15
lines changed

app/Functions/functions.php

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,110 @@
11
<?php
22

3+
use Illuminate\Support\Facades\Cache;
4+
use Illuminate\Support\Facades\Redis;
5+
6+
if (!function_exists('preloadDirectoryFiles')) {
7+
/**
8+
* Preload all files in a directory and optionally cache in Redis or Laravel cache.
9+
*
10+
* @param string $directory
11+
* @param string $cacheKey Unique cache key
12+
* @param int $ttl Cache time in seconds
13+
* @return array Array of filenames
14+
*/
15+
function preloadDirectoryFiles(string $directory, string $cacheKey, int $ttl = 3600): array
16+
{
17+
static $memoryCache = [];
18+
19+
// Return from memory if already loaded
20+
if (isset($memoryCache[$directory])) {
21+
return $memoryCache[$directory];
22+
}
23+
24+
$files = [];
25+
$fingerprint = md5(json_encode(scandir($directory)));
26+
27+
// Try Redis first
28+
if (class_exists('Redis')) {
29+
try {
30+
$cached = Redis::get($cacheKey);
31+
if ($cached) {
32+
$cached = json_decode($cached, true);
33+
if (isset($cached['fingerprint']) && $cached['fingerprint'] === $fingerprint) {
34+
$memoryCache[$directory] = $cached['files'];
35+
return $cached['files'];
36+
}
37+
}
38+
} catch (\Exception $e) {
39+
// Redis not available, fallback to local memory
40+
}
41+
}
42+
43+
// Scan directory and cache
44+
$files = scandir($directory);
45+
46+
// Cache in Redis if possible
47+
$data = [
48+
'fingerprint' => $fingerprint,
49+
'files' => $files
50+
];
51+
if (class_exists('Redis')) {
52+
try {
53+
Redis::setex($cacheKey, $ttl, json_encode($data));
54+
} catch (\Exception $e) {
55+
// fallback silently
56+
}
57+
} else {
58+
// Laravel cache fallback
59+
Cache::put($cacheKey, $data, $ttl);
60+
}
61+
62+
$memoryCache[$directory] = $files;
63+
64+
return $files;
65+
}
66+
}
67+
368
function findFile($name)
469
{
570
$directory = base_path("/assets/linkstack/images/");
6-
$files = scandir($directory);
7-
$pathinfo = "error.error";
71+
$files = preloadDirectoryFiles($directory, 'linkstack_images_files');
72+
873
$pattern = '/^' . preg_quote($name, '/') . '(_\w+)?\.\w+$/i';
974
foreach ($files as $file) {
1075
if (preg_match($pattern, $file)) {
11-
$pathinfo = $file;
12-
break;
76+
return $file;
1377
}
1478
}
15-
return $pathinfo;
79+
return "error.error";
1680
}
1781

1882
function findAvatar($name)
1983
{
2084
$directory = base_path("assets/img");
21-
$files = scandir($directory);
22-
$pathinfo = "error.error";
85+
$files = preloadDirectoryFiles($directory, 'assets_img_files');
86+
2387
$pattern = '/^' . preg_quote($name, '/') . '(_\w+)?\.\w+$/i';
2488
foreach ($files as $file) {
2589
if (preg_match($pattern, $file)) {
26-
$pathinfo = "assets/img/" . $file;
27-
break;
90+
return "assets/img/" . $file;
2891
}
2992
}
30-
return $pathinfo;
93+
return "error.error";
3194
}
3295

3396
function findBackground($name)
3497
{
3598
$directory = base_path("assets/img/background-img/");
36-
$files = scandir($directory);
37-
$pathinfo = "error.error";
99+
$files = preloadDirectoryFiles($directory, 'assets_img_background_files');
100+
38101
$pattern = '/^' . preg_quote($name, '/') . '(_\w+)?\.\w+$/i';
39102
foreach ($files as $file) {
40103
if (preg_match($pattern, $file)) {
41-
$pathinfo = $file;
42-
break;
104+
return $file;
43105
}
44106
}
45-
return $pathinfo;
107+
return "error.error";
46108
}
47109

48110
function analyzeImageBrightness($file) {

0 commit comments

Comments
 (0)