Write to cache after submit
I created a --simple filament resource. How do I write the values from the create/edit form to cache after it gets updated in the database?
11 Replies
How do you mean to cache?
I'd like to write the value to cache to reduce load, if not in cache, it will read from the DB. This is what I'd like to do after it submits the form:
Cache::forever('start_date', $record->start_date);
Not that I know off, it's kinda the whole point of Livewire and Filament. You'd need more of a database cache solution.
I could set this value in the .env file which will get cached, but I want to give admins the ability to set the value using a form, which in turn writes to the file cache so I can just reference the cache value instead of doing a DB call every time.
Hang onn, so you don't want to actually cache it you want to update a file?
Don't use the .env then and instead write to the database in config/ and in there they are cached after called. Then you can clear the config cache on updating.
Here is an example of using a service provider to fetch the settings from the db that will then be cached
I just don't want to make a DB call for a value used often, so I wanted to cache it in the file cache and then use Cache::get('start_date') to get the value when needed for a query. If that value is not in cache, then get it from the DB. Something like that
the resource model is actually Settings π This looks like it'll work. So will I be able to get the value using Cache::get('start_date')?
You use the above, then just call it with config so:
config('settings.the_field_value')
config is cached at boot
Is this for managing settings?
Save yourself some time use the spatie setting plugin https://filamentphp.com/docs/2.x/spatie-laravel-settings-plugin/installation
Filament
Installation - Spatie Settings Plugin - Filament
A plugin to add support for spatie/laravel-settings to Filament.
Set it up according to filament and spatie doc and configure the caching https://github.com/spatie/laravel-settings#caching-settings
GitHub
GitHub - spatie/laravel-settings: Store strongly typed application ...
Store strongly typed application settings. Contribute to spatie/laravel-settings development by creating an account on GitHub.
whoa! that looks like exactly what I need. I'll check it out. Thanks for your help.