GithubHelp home page GithubHelp logo

Comments (4)

rennokki avatar rennokki commented on May 13, 2024

Setting $flushCacheOnUpdate will invalidate the entire caching. Instead, you might want to do a manual flush.

  1. 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']);
  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.

leenooks avatar leenooks commented on May 13, 2024

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.

rennokki avatar rennokki commented on May 13, 2024

This package caches the queries, not the models.

from laravel-eloquent-query-cache.

leenooks avatar leenooks commented on May 13, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.