andersao / andersao/l5-repository
Repository's cache not being updated after delete??
- Ngôn ngữ chính
- PHP
- Star
- 4.2k
- Fork
- 880
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
Hello,
I am new to Laravel and i am trying to introduce myself into this framework developing a new App where i am using this repository package to take care of my persistence layer.
I got to a point where i found a problem that i am not being able to resolve - i don't know if it is a bug from this package or something that i am not doing well.
The problem seems to be related with the Cache (i am using these Repositories implementing the `CacheableInterface`).
Let's make this simple test case:
1. Create a new instance of a Model (for example, "User"), filled with random data;
2. Save this new Model in the database, using de Repository;
3. Delete the recently created Model from de database, using the Repository;
4. Try to get from the database the removed item using the `repository->find($id)` method.
The problem is: when the cache is enable, the `repository->find($id)` method returns the object (that was already removed...) instead of returning the `ModelNotFoundException` - because the item doesn't exists in the database anymore. It seems that the removed item was not removed from the cache and is being returned even after it was removed from database.
Here is my code:
```
...
// call a method that creates a new Model with random data
$obj = $this->getSampleModel();
// saves this recently created model into database using the Repository
$obj = $this->repository->create($obj->toArray());
$id = $obj->getID();
// try to get the item from database using its $id
try {
$obj = $this->repository->find($id);
echo "The object was found";
} catch (ModelNotFoundException $e) {
echo "ERROR: The object was not found";
}
// here i know that the row was already created in the database because i get the "The object was found" message.
// removes the recently created item from database using the Repository
$this->repository->delete($id);
try {
// let's try to get the item from database: because it was removed before, this action should throw a ModelNotFoundException
$removed_obj = $this->repository->find($id);
echo "The item was found!";
} catch (ModelNotFoundException $e) {
echo "Success! The item was not found!";
}
```
Running this code, i always get the `The item was found!` message. But, if i check the database, the item is not there anymore - it was removed!
If i disable cache, using `$this->repository->skipCache(true);` before the previous code, i receive the `Success! The item was not found!` message, as expected.
It seems that the cache is caching the result of the first `$this->repository->find($id)` and is returning it again later.
Is there something that i am not doing well?
Does anyone experienced something similar?
Thank you very much!
Hướng dẫn đóng góp
Đánh giá
Issue này chưa được đánh giá.