Answer the question
In order to leave comments, you need to log in
Where do I need to submit another Persistence to the repository by condition?
The repository fetches data using Persistence. There are two Persistence . For example memory and databese. Maybe even memcahed.
Where do you need to insert the condition that if memory Persistence returned 0 then go to databese Persistence?
And even more important, where to cache from db to memory Persistence. Or rather, where it is more correct to divide it from the point of view of OOP.
Approximate code:
class MyController{
function postsAction(){
$memoryRepository = new PostsRepository(new PostsMemoryPesistence());
if( empty( $posts = $memoryRepository->getAll() ) ) {
$posts = (new PostsRepository(new PostsDBPesistence()))->getAll();
$memoryRepository->save( $posts )
}
}
}
class PostsRepository{
function __constructor(Presistence presistence){
this->presistence = presistence;
}
function getAll(){
return this->presistence->getAll();
}
function save( Posts[] $posts ){
foreach($posts as $post){
this->presistence->save( $post );
}
}
}
interface Presistence{
function getAll();
functions save();
}
class PostsMemoryPesistence implements Presistence{
$posts;
function getAll(){}
functions save(){}
}
class PostsDBPesistence implements Presistence{
$db;
function getAll(){}
functions save(){}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question