mikebronner / laravel-model-caching

Eloquent model-caching made easy.
MIT License
2.26k stars 215 forks source link

Cache for model not cleared after simple update #395

Closed retaildesk closed 3 years ago

retaildesk commented 3 years ago

Describe the bug When i update a model the cache is not automatically cleared for that model. I suppose this needs to be done automatically?

Environment

I have simple crud for model Suppliers. When i load the table i see all the suppliers. When i update one supplier it is changed in the database but when i refresh the page the old value is still showing.

$supplier = Supplier::find($request->id); $supplier->name = $request->name; $supplier->save();

Thank you !

mikebronner commented 3 years ago

HI @retaildesk, this could be due to a multitude of factors. You may have another package that is prevent model caching from working (any package that implements or overrides the method "newEloquentBuilder"), or you may not have added the Cachable trait to the model. Also, any updates made directly to the database will not update the cache. You need to make the updates through the model itself. So the best way to test this would be to have two browser windows open, view the data on one browser, go make the update on the other browser, then reload the first browser. It should show changed.

retaildesk commented 3 years ago

Hi Mike,

Thanks for your answer.

I searched my whole project (incl vendor dir for packages) for the line newEloquentBuilder and nothing found. (only your package).

I can confirm that i included the Cachable trait to the model. And of course i checked for the incompatible packages on the main page of this project (laravel-mysql-spatial, pivot etc. )

retaildesk commented 3 years ago

These are all my packages installed.

"require": { "php": "^7.4", "ext-ftp": "^7.4", "ext-gettext": "", "arrilot/laravel-widgets": "^3.13", "barryvdh/laravel-dompdf": "^0.9", "barryvdh/laravel-translation-manager": "^0.5.9", "bastiaanh/overheid-kvk": "^1.0", "doctrine/dbal": "^3.0", "fideloper/proxy": "^4.4", "fruitcake/laravel-cors": "^2.0", "genealabs/laravel-model-caching": "^0.11.2", "gerardojbaez/money": "0.", "graham-campbell/github": "^10.0", "guzzlehttp/guzzle": "^7.2", "huddledigital/zendesk-laravel": "^3.5", "illuminated/db-profiler": "^8.1", "kriswallsmith/buzz": "^1.1", "laravel/cashier": "^12.6", "laravel/framework": "^8.25", "laravel/passport": "^10.0", "laravel/sanctum": "^2.9", "laravel/tinker": "^2.5", "laravel/ui": "^3.2", "league/flysystem-aws-s3-v3": "^1.0", "league/flysystem-cached-adapter": "^1.1", "maatwebsite/excel": "^3.1", "mailgun/mailgun-php": "^3.0", "milon/barcode": "^8.0", "nicolab/php-ftp-client": "^1.5", "nyholm/psr7": "^1.3", "owen-it/laravel-auditing": "^12.0", "pendonl/laravel-pro6pp": "^1.0", "php-http/guzzle7-adapter": "^0.1.1", "pusher/pusher-php-server": "^4.1", "rap2hpoutre/fast-excel": "^2.5", "sabas/edifact": "^0.5.1", "sentry/sentry-laravel": "^2.4", "spatie/db-dumper": "^2.17", "spatie/laravel-db-snapshots": "^1.7", "staudenmeir/eloquent-json-relations": "^1.1", "yajra/laravel-datatables-buttons": "^4.0", "yajra/laravel-datatables-html": "^4.0", "yajra/laravel-datatables-oracle": "^9.15" }, "require-dev": { "barryvdh/laravel-debugbar": "^3.5", "facade/ignition": "^2.5", "kitloong/laravel-migrations-generator": "^4.4", "mockery/mockery": "^1.4.2", "nunomaduro/collision": "^5.0", "phpunit/phpunit": "^9.3.3", "fakerphp/faker": "^1.9.1" },

mikebronner commented 3 years ago

@retaildesk Can you provide the code that does the update, where you expect the cache to be cleared?

retaildesk commented 3 years ago

i save the supplier with the following code. After saving i expect the cache to be cleared.

private function storeSupplier($request, $stopRedirect){
        $this->validate($request, [
            'name' => 'required|max:255',
        ]);
        if (isset($request->id)) {
            $supplier = Supplier::find($request->id);

            if (! $stopRedirect) {
                setNotification('success', __('De leverancier is gewijzigd.'));
            }
        } else {
            $supplier = new Supplier;

            if (! $stopRedirect) {
                setNotification('success', __('De leverancier is toegevoegd.'));
            }
        }
        $supplier->name = $request->name;
        $supplier->save();

        return $supplier;
    }
retaildesk commented 3 years ago

I found the issue. It was because i was using a different database connection for select queries (from read-only mysql server). And i saw the cache_prefix was also including the connection_name.

What i did to fix it is setting my own cache_prefix.

Thanks for help!