Comments (32)
@mikebronner it seems to work as expected! I will have a look on the logs today - but it's looking good at the moment!
from laravel-model-caching.
Thanks, @jetwes , I will look into this. Have you been able to see any kind of pattern that might be causing this? Are you using the cache cool down option?
from laravel-model-caching.
No, i'm not using the cool down option. I cant'see a pattern at the moment.
Another example:
if ($user->last_shop_id != null) { $shop = Shop::where('id', '=', $user->last_shop_id)->first(); } else { $shop = Shop::where('id', '=', $user->main_shop_id)->first(); } $user->setShop(['id' => $shop->id, 'name' => $shop->shortname]);
I get an Trying to get property of non-object
error on the last line - it seems that $shop isn't fetched correctly. If i disable the cache everything is fine. It's on my login controller - within the setShop method i set a default shop for my session. I don't cache the user model. If i disable the cache on the shop model - everything is ok. I was on version 0.2.23 before updating this morning. The issues started right after the update to the latest version.
additional info: I'm using redis 2.0.8 (latest) and a seperate db for model caching
from laravel-model-caching.
It seems a bit like if the queries are hit in short time the error occurs (just a guess) - but i'm not using cool down. I have about 170 users that are working within this app (all at the same time, it's a business portal for managing mobile phone shops) averaging about 90.000 Request per 24h..
from laravel-model-caching.
Another example:
$shops = Shop::where('active',1)->where('shortname','!=','Zentrale')->where('micro_region',Shop::where('id',Auth::user()->main_shop_id)->first()->micro_region)->orderBy('shortname','ASC')
Error: Exception · Property [micro_region] does not exist on this collection instance.
The errors don't accur on every requests - but a couple of times in a minute...
from laravel-model-caching.
@jetwes thanks again for the details. It almost sounds like different users are overriding the query cache in different circumstances, which if true, means that some keys are not granular enough, and even though you specified `first()1st it's actually returning the collection.
from laravel-model-caching.
@jetwes I had another thought. If you could see if you can get this error to happen again in your dev environment, would you be able to look at the collection that it errors on, and see what query created that collection? If so, that would be the smoking gun we need to identify the source of the problem. In the mean while I am writing a few more tests to cover some scenarios I can think of.
from laravel-model-caching.
ok, i'll have a look at it, later
from laravel-model-caching.
Ok - seems that the mess is on my side. I have a couple of old queries ending ->get()[0]; instead of using ->first();
All this queries fail. When i change them, flush the cache - than they are working.
Sorry for confusing. I will monitor this further...
But there's a strange thing. php artisan modelCache:flush --model=Portal\Models\Sale
I tried php artisan modelCache:flush --model=Portal\\Models\\Shop
to flush the cache model and i get the error 'Portal\Models\Sale' is not an instance of CachedModel.
The Model uses the trait Cachable...
namespace Portal\Models;
/**
- Class Shop.
*/
class Shop extends PortalBaseModel
namespace Portal\Models;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Model;abstract class PortalBaseModel extends Model
{
use Cachable;
}
from laravel-model-caching.
Hi @jetwes, awesome, thanks for researching this further.
Try the following on the commandline: php artisan modelCache:flush --model=\\Portal\\Models\\Shop
(not the leading slash). Let me know if that doesn't work.
from laravel-model-caching.
nope - same error - on all my models. I can't flush the model from the command line..
did you change the way you return collections in the last couple of versions? Just asking because my get()[0] queries were ok in the old version...
from laravel-model-caching.
Thanks for checking. I will look into why models can't be flushed from the command-line and see if there is an edge-case.
The way data is stored in the cache changed, but it shouldn't affect being able to access an index on a collection (since get should always return a collection, which is stored in cache). Could you provide me with a full query that you are trying to run, ending in [0]
. I will use that to write a test to see what happens.
In the mean time, I would highly recommend using a dedicated cache store for model-caching, if you haven't already done so. You can then flush the cache for models without affecting your normal cache, by simply omitting the model options on the command-line: php artisan modelCache:flush
. I do still need to document that better on the readme -- both how to flush the complete cache, as well as set up a dedicated cache.
from laravel-model-caching.
i'm using redis in a seperate database.
An example Query:
$saleItem = SaleItem::where('id','=',Request::input('id'))->get()[0];
I already refactored most of the old queries to first() - i will monitor this in the next days..
from laravel-model-caching.
Thanks! I will try to make a test case this evening and report back
from laravel-model-caching.
Hi @jetwes, I tried to replicate the problem, but was unable to get a failure out of it. See my unit test here (44fccfc). If you can think of anything or provide steps to reproduce, or even a unit test, that would be awesome!
from laravel-model-caching.
Also, you could simplify your query, by using find()
, perhaps like so:
$saleItem = SaleItem::find(Request::input('id'));
from laravel-model-caching.
Also, I covered the process I went through here, let me know if I missed something: https://t.co/CHKiHkfYGz
I would probably suggest opening a new issue if you find a problem, as the original issue on this one has been resolved, and we're working on secondary things now. :)
from laravel-model-caching.
I am able to replicate the flushing issue ... working on that separate from this issue
from laravel-model-caching.
Fix for model flush command will be in the next release in a few minutes. :)
from laravel-model-caching.
Thank you!
from laravel-model-caching.
The problem still persists. Changing to first() fixed the error message itself - but the value is still not fetched from the cache - sometimes.
In this example i display a list of users. The query is
$users = User::with('role')->with('shop')->with('user_salaries')->get();
As you can see in the pitcture the field under "Bonn-Duisdorf" is empty . Thats wrong - when i disable the cache i see the correct value "Münster-Hiltrup".
The view for this looks like {{ $user->main_shop($user->main_shop_id)->shortname }}
Method main_shop in my user model:
public function main_shop($id) { return Shop::where('id', '=', $id)->first(); }
from laravel-model-caching.
Is Shop also using Cachable? In your view, can dump out the user I'd in the loop, before you display the main_shop? Could you post a demo repo with just the view, route, and controller needed to get this specific page to work? That way I could try to replicate the problem. I suspect the issue lies somewhere other than in the query itself.
from laravel-model-caching.
i dumped out the user id - it's the correct user_id (the other list entries are correct - so no surprise)
Yes, Shop is cachable.
I try to post a demo repo .
from laravel-model-caching.
by the way - the user is not cachable.
from laravel-model-caching.
i posted it to a repo - you got an invite link. Its straighforwarded - i uploaded vendor, too. so just clone it and you are ready. a sql is inside the migrations folder for quick generetion of the db tables.
I deleted all other stuff, like auth etc.
from laravel-model-caching.
Got the invite, thanks! Will take a look at it tomorrow :)
from laravel-model-caching.
@jetwes I started installing the repo, but it appears the migration folder is missing. Could you add that for me? :) Thanks!
from laravel-model-caching.
oh sorry - i'm looking for it this evening - i was sick the last couple of days - sorry for the delay
from laravel-model-caching.
Hope you feel better - gute Besserung!
from laravel-model-caching.
thank you mike - danke schön ;)
Just uploaded the folder
from laravel-model-caching.
@jetwes I fixed an issue with first()
not invalidating, could you try again with the latest version?
from laravel-model-caching.
Closing for now. If it crops up again, please open a new issue. :)
from laravel-model-caching.
Related Issues (20)
- Unsupported enums HOT 3
- Performance test on 3000 records HOT 1
- Install error "requires illuminate/cache ^9.0 -> found..." HOT 2
- Cache Not Clearing With morphTo/morphedByMany
- GeneaLabs\LaravelModelCaching\CacheKey::processEnum() HOT 3
- Only flush cache when delete() returns a count
- Clear cache in a record of the model. HOT 2
- Saving not working with eloquent strict mode HOT 2
- What is cacheCooldownSecounds definition? HOT 1
- Add support for Laravel 10 HOT 2
- Object of class Illuminate\\Database\\Query\\Expression could not be converted to string HOT 6
- Since 0.12.4 I get error "GeneaLabs\LaravelModelCaching\CacheKey::processEnum(): Argument #1 ($value) must be of type BackedEnum|UnitEnum|string, null given, called in ...\vendor\genealabs\laravel-model-caching\src\CacheKey.php on line 409" HOT 23
- Cannot access "parent" when current class scope has no parent HOT 1
- Laravel v10.3.0 breaks LaravelModelCaching HOT 2
- checkCooldownAndFlushAfterPersisting(): Argument #2 ($relationship) must be of type string, array given, called in /var/www/html/vendor/genealabs/laravel-model-caching/src/Traits/ModelCaching.php on line 83 HOT 8
- Is table join supported? HOT 7
- The attribute [column3] either does not exist or was not retrieved for model HOT 1
- Stale cached value returned where(...)->first() HOT 4
- Fix for GeneaLabs\LaravelModelCaching\CacheKey::processEnum(): Argument #1 ($value) must be of type BackedEnum|UnitEnum|string, null given
- processEnum(): Argument #1 ($value) must be of type BackedEnum|UnitEnum|Illuminate\Database\Query\Expression|string, null given HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-model-caching.