Comments (4)
Setting $flushCacheOnUpdate
will invalidate the entire caching. Instead, you might want to do a manual flush.
- You might do manual invalidation:
$user1 = User::cacheFor(60)->cacheTags(['id:1'])->find(1);
$user2 = User::cacheFor(60)->cacheTags(['id:2'])->find(2);
$user1->update(['name' => 'John']);
User::flushQueryCache(['id:1']);
- If you still want to use
$flushCacheOnUpdate = true;
, update the tags that will get flushed upon updates:
class User {
/**
* Invalidate the cache automatically
* upon update in the database.
*
* @var bool
*/
protected static $flushCacheOnUpdate = true;
/**
* Set the base cache tags that will be present
* on all queries.
*
* @return array
*/
protected function getCacheBaseTags(): array
{
return [
"id:{$this->id}",
];
}
}
$user1 = User::cacheFor(60)->cacheTags(['id:1'])->find(1);
$user2 = User::cacheFor(60)->cacheTags(['id:2'])->find(2);
$user1->update(['name' => 'John']);
// Cache flushed successfully only for user 1
from laravel-eloquent-query-cache.
So I've been trying the second method - but I'm not using the ->cacheTags() in the query.
IE: If I have
$o = Import::find($this->argument('arg'));
$o->active = ! $o->active;
$o->save();
$this->info('Changed active to :'.($o->active ? 'ON' : 'OFF'));
dump(['now'=>Import::find($this->argument('arg'))->active ? 'ON' : 'OFF']);
I only ever get:
Changed active to :ON
array:1 [
"now" => "OFF"
]
So the only way to achieve it is if I have cacheTags()
in the original query? (IE: It cannot automatically do it just using getCacheBaseTags()
) ?
from laravel-eloquent-query-cache.
This package caches the queries, not the models.
from laravel-eloquent-query-cache.
I'm not sure I follow - but...
One of the attractive attributes of this library is that caching is added with minimal change to existing code. (Hence why I dont use cacheTags()
yet in my queries.)
So I tried this:
getCacheBaseTags()
on the Model is as you have it.
Then,
$cachetag = sprintf('%s:%d','id',$this->argument('arg'));
$o = Import::cacheTag($cachetag)->find($this->argument('arg'));
$o->active = ! $o->active;
$o->save();
$this->info('Changed active to :'.($o->active ? 'ON' : 'OFF'));
dump(['now'=>Import::find($this->argument('arg'))->active ? 'ON' : 'OFF']);
(Notice in the final query I have not added the cacheTag()
. So while the DB is updated correctly, any subsequent queries not using the cacheTag() id yields a static stale record :( )
I think the answer is yes - but I have to use cacheTag()
consistently if I want to expire only changed records from the cache (of the same model), and still have my queries return the correct (cached) record?
(I do a large import of 10,000's records of multiple models - and the DB is remote, so I was planning on using a local redis cache to reduce some of the latency to the DB, where I do some queries to validate some records before I create/update a record. So during the import, I dont want to expire unchanged records.)
from laravel-eloquent-query-cache.
Related Issues (20)
- [improvement] Updating `BelongsToMany` relationship doesn't flush the cache HOT 3
- Don't use cache depends on config HOT 7
- Invalidating specific user cache HOT 1
- have question in relation HOT 4
- redis cache, does not respect the time specified in $cacheFor
- ManyToMany invalidate cache is not bi-directional when attaching/detaching HOT 2
- flushQueryCache for specific tag not working HOT 3
- global prefix HOT 2
- Not compatible with ```Model::preventAccessingMissingAttributes()``` HOT 7
- How to invalidate a specific model ID ? HOT 3
- Laravel 10 support HOT 13
- when we use PLUCK method the cache doesnt work.. HOT 2
- Turn off caching globally on demand HOT 2
- DontCache doesn't apply to a relation HOT 1
- cacheFor(0) would cache forever? HOT 1
- Received Mockery_0_Illuminate_Cache_CacheManager::driver(), but no expectations were specified HOT 1
- Does using Laravel's paginate() method make it impossible to use the cache? HOT 1
- On update, cache flush bug HOT 2
- Support for Laravel 11.x HOT 3
- is it possible to append cache tags after get
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-eloquent-query-cache.