I used repository in a project that caching all queries.
there's a BaseRepository.
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
class BaseRepository implements BaseRepositoryInterface{
protected $model;
protected int $cacheDuration = 600; //per seconds
public function __construct(Model $model)
{
return $this->model = $model;
}
public function paginate(int $paginate,string $cacheKey)
{
return Cache::remember($cacheKey,$this->cacheDuration , function () use ($paginate) {
return $this->model->latest()->paginate($paginate);
});
}
// other methods ...
}
then i used this repository in my service
PostService:
use Illuminate\Support\Facades\App;
class PostService{
public PostRepositoryInterface $postRepository;
public function __construct()
{
$this->postRepository = App::make(PostRepositoryInterface::class);
}
public function paginate(int $paginate, string $cacheKey)
{
return $this->postRepository->paginate($paginate,$cacheKey);
}
}
finally i using the PostService in my controller
PostController:
class PostController extends Controller{
public PostService $postService;
public function __construct()
{
$this->postService = App::make(PostService::class);
}
public function index()
{
string $cacheKey = "posts.paginate";
return $this->postService->paginate(10);
}
}
the index method will return top 10 latest record correctly. now i need to create a unique CacheKey for all Repository queries. for example
TableName concat FunctionName // posts.paginate
so i can use this code into all method of Repository
public function paginate(int $paginate)
{
$cacheKey = $this->model->getTable().__FUNCTION__;
return Cache::remember($cacheKey,$this->cacheDuration , function () use ($paginate) {
return $this->model->latest()->paginate($paginate);
});
}
this is fine. but the problem is that this code repeat in all the method of this class.
if i use this code in another class, method name's will be incorrect.
What do you suggest to prevent duplication of this code?
My Answer:
One way to prevent duplication of the code for generating unique cache keys is to create a separate method in the BaseRepository class that generates the cache key based on the table name and the calling function name. You can then call this method in each of the repository methods that need a unique cache key.
Here's an example of how you can refactor the BaseRepository class to include a generateCacheKey method:
php
class BaseRepository implements BaseRepositoryInterface{
protected $model;
protected int $cacheDuration = 600; //per seconds
public function __construct(Model $model)
{
return $this->model = $model;
}
protected function generateCacheKey(string $functionName): string
{
return $this->model->getTable() . '.' . $functionName;
}
public function paginate(int $paginate)
{
$cacheKey = $this->generateCacheKey(__FUNCTION__);
return Cache::remember($cacheKey, $this->cacheDuration, function () use ($paginate) {
return $this->model->latest()->paginate($paginate);
});
}
// other methods ...
}
Now, you can simply call the generateCacheKey method in each repository method that needs a unique cache key:
php
public function paginate(int $paginate)
{
$cacheKey = $this->generateCacheKey(__FUNCTION__);
return Cache::remember($cacheKey, $this->cacheDuration, function () use ($paginate) {
return $this->model->latest()->paginate($paginate);
});
}
This way, you can avoid duplicating the code for generating cache keys in each method and ensure that each cache key is unique based on the table name and the calling function name.
Rate this post
4 of 5 based on 2816 votesComments