My question is how should I cache images that are loaded by an path on the DB.
I simply get and return the image like:
/**
* Render an image
*
* @param string $slug
* @return Response
*/
public function renderSlug($slug)
{
try {
$image = Image::where('slug', '=', $slug)->firstOrFail();
$file = File::get($image->getFullPath());
}
catch ( \Exception $e ) {
return Response::json('{}', 404);
}
return Response::make($file, 200, ['content-type' => 'image/jpg']);
}
So I'm fetching the path from DB, and I'm wondering if I should cache path, image or cache nothing at all.
I've looked into the docs about caching, I though doing it like this:
/**
* Render an image
*
* @param string $slug
* @return Response
*/
public function renderSlug($slug)
{
try {
$cacheKey = 'image.' . $slug;
if(! Cache::has($cacheKey)) {
$image = Image::where('slug', '=', $slug)->firstOrFail();
Cache::put($cacheKey, 'value', 10);
}
$file = File::get(Cache::get($cacheKey));
}
catch ( \Exception $e ) {
return Response::json('{}', 404);
}
return Response::make($file, 200, ['content-type' => 'image/jpg']);
}
Questions:
- Should I cache anything? (image, path, or nothing)
- How should I cache it?
Aucun commentaire:
Enregistrer un commentaire