Skip to content

Cache does not flush in deleting function of Model #173

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
andersonpem opened this issue Oct 10, 2018 · 6 comments
Closed

Cache does not flush in deleting function of Model #173

andersonpem opened this issue Oct 10, 2018 · 6 comments

Comments

@andersonpem
Copy link

andersonpem commented Oct 10, 2018

Describe the bug
Hi. Good night. Firstly, thanks for the great package. It really is handy.
I'm working on a menu class. Before deleting the current model the class checks for any relationships and deletes them.
After all the deletion is complete the cache is supposed to be flushed automatically. However I still get the old results if I refresh a page which queries the model.

Eloquent Query

$mi = Menubuilder\MenuItem::find(9)
$mi->delete();

The relationship in MenuItem is a parent-children relationship with rows of the same table.

The function:

public static function boot() {
        parent::boot();
        static::deleting(function(MenuItem $mi) { // before delete() method call this
             if ($mi->hasChildren()) {
                 foreach ($mi->children() as $mc) {
                     $mc->delete();
                 }
             }
        });
    }

If I refresh the page where I'm seeing the actual list I still see the old items and no query is executed according to Debugbar. When the cache is flushed the queries are re-executed

screenshot from 2018-10-10 19-05-54
In the case of this very image, the "About" section was supposed to disappear. If I go to the terminal and do the same command, then it runs normally.
And what's interesting is that if I check for the deletion of the models they were indeed deleted.

Is this maybe a scope problem?

Stack Trace

#0 /home/developer/repos/research/mbdr/vendor/predis/predis/src/Connection/StreamConnection.php(128): Predis\\Connection\\AbstractConnection->onConnectionError('php_network_get...', 0)
#1 /home/developer/repos/research/mbdr/vendor/predis/predis/src/Connection/StreamConnection.php(178): Predis\\Connection\\StreamConnection->createStreamSocket(Object(Predis\\Connection\\Parameters), 'tcp://redis:637...', 4)
#2 /home/developer/repos/research/mbdr/vendor/predis/predis/src/Connection/StreamConnection.php(100): Predis\\Connection\\StreamConnection->tcpStreamInitializer(Object(Predis\\Connection\\Parameters))
#3 /home/developer/repos/research/mbdr/vendor/predis/predis/src/Connection/AbstractConnection.php(81): Predis\\Connection\\StreamConnection->createResource()
#4 /home/developer/repos/research/mbdr/vendor/predis/predis/src/Connection/StreamConnection.php(258): Predis\\Connection\\AbstractConnection->connect()
#5 /home/developer/repos/research/mbdr/vendor/predis/predis/src/Connection/AbstractConnection.php(180): Predis\\Connection\\StreamConnection->connect()
#6 /home/developer/repos/research/mbdr/vendor/predis/predis/src/Connection/StreamConnection.php(288): Predis\\Connection\\AbstractConnection->getResource()
#7 /home/developer/repos/research/mbdr/vendor/predis/predis/src/Connection/StreamConnection.php(394): Predis\\Connection\\StreamConnection->write('*2\
\
$3\
\
GET\
\
$1...')
#8 /home/developer/repos/research/mbdr/vendor/predis/predis/src/Connection/AbstractConnection.php(110): Predis\\Connection\\StreamConnection->writeRequest(Object(Predis\\Command\\StringGet))
#9 /home/developer/repos/research/mbdr/vendor/predis/predis/src/Client.php(331): Predis\\Connection\\AbstractConnection->executeCommand(Object(Predis\\Command\\StringGet))
#10 /home/developer/repos/research/mbdr/vendor/predis/predis/src/Client.php(314): Predis\\Client->executeCommand(Object(Predis\\Command\\StringGet))
#11 /home/developer/repos/research/mbdr/vendor/laravel/framework/src/Illuminate/Redis/Connections/Connection.php(96): Predis\\Client->__call('get', Array)
#12 /home/developer/repos/research/mbdr/vendor/laravel/framework/src/Illuminate/Redis/Connections/Connection.php(108): Illuminate\\Redis\\Connections\\Connection->command('get', Array)
#13 /home/developer/repos/research/mbdr/vendor/laravel/framework/src/Illuminate/Cache/RedisStore.php(54): Illuminate\\Redis\\Connections\\Connection->__call('get', Array)
#14 /home/developer/repos/research/mbdr/vendor/laravel/framework/src/Illuminate/Cache/TagSet.php(87): Illuminate\\Cache\\RedisStore->get('tag:genealabs:l...')
#15 [internal function]: Illuminate\\Cache\\TagSet->tagId('genealabs:larav...')
#16 /home/developer/repos/research/mbdr/vendor/laravel/framework/src/Illuminate/Cache/TagSet.php(76): array_map(Array, Array)
#17 /home/developer/repos/research/mbdr/vendor/laravel/framework/src/Illuminate/Cache/TagSet.php(66): Illuminate\\Cache\\TagSet->tagIds()
#18 /home/developer/repos/research/mbdr/vendor/laravel/framework/src/Illuminate/Cache/RedisTaggedCache.php(159): Illuminate\\Cache\\TagSet->getNamespace()
#19 /home/developer/repos/research/mbdr/vendor/laravel/framework/src/Illuminate/Cache/RedisTaggedCache.php(138): Illuminate\\Cache\\RedisTaggedCache->deleteKeysByReference('forever_ref')
#20 /home/developer/repos/research/mbdr/vendor/laravel/framework/src/Illuminate/Cache/RedisTaggedCache.php(84): Illuminate\\Cache\\RedisTaggedCache->deleteForeverKeys()
#21 /home/developer/repos/research/mbdr/vendor/genealabs/laravel-model-caching/src/Traits/Caching.php(42): Illuminate\\Cache\\RedisTaggedCache->flush()
#22 /home/developer/repos/research/mbdr/vendor/genealabs/laravel-model-caching/src/Console/Commands/Clear.php(46): Modules\\Menubuilder\\Entities\\MenuItem->flushCache()
#23 /home/developer/repos/research/mbdr/vendor/genealabs/laravel-model-caching/src/Console/Commands/Clear.php(19): GeneaLabs\\LaravelModelCaching\\Console\\Commands\\Clear->flushModelCache('Menubuilder\\\\Men...')
#24 [internal function]: GeneaLabs\\LaravelModelCaching\\Console\\Commands\\Clear->handle()
#25 /home/developer/repos/research/mbdr/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array)
#26 /home/developer/repos/research/mbdr/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#27 /home/developer/repos/research/mbdr/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#28 /home/developer/repos/research/mbdr/vendor/laravel/framework/src/Illuminate/Container/Container.php(564): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#29 /home/developer/repos/research/mbdr/vendor/laravel/framework/src/Illuminate/Console/Command.php(179): Illuminate\\Container\\Container->call(Array)
#30 /home/developer/repos/research/mbdr/vendor/symfony/console/Command/Command.php(255): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#31 /home/developer/repos/research/mbdr/vendor/laravel/framework/src/Illuminate/Console/Command.php(166): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#32 /home/developer/repos/research/mbdr/vendor/symfony/console/Application.php(886): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#33 /home/developer/repos/research/mbdr/vendor/symfony/console/Application.php(262): Symfony\\Component\\Console\\Application->doRunCommand(Object(GeneaLabs\\LaravelModelCaching\\Console\\Commands\\Clear), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#34 /home/developer/repos/research/mbdr/vendor/symfony/console/Application.php(145): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#35 /home/developer/repos/research/mbdr/vendor/laravel/framework/src/Illuminate/Console/Application.php(89): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#36 /home/developer/repos/research/mbdr/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(122): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#37 /home/developer/repos/research/mbdr/artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#38 {main}
"} 

Environment

  • PHP: 7.2.10
  • OS: Ubuntu 18.04
  • Laravel: 5.6.39
  • Model Caching: 0.3.1
@mikebronner
Copy link
Owner

@andersonpem You should not have to call the artisan flush method. The package manages flushing on its own when you delete relationships and models. I would recommend removing those two lines and trying again.

And please report the complete stack trace (you only reported from step #8 onwards), and don't forget to put the stack trace in the code back-ticks so that it is not formatted.

Keep in mind:

  • Global scopes are not supported by this package at this time.
  • The class MenuItem and any related classes must use Cachable;.

Could you also provide the query that created the $mi object?

@andersonpem
Copy link
Author

Hello Mike. I made some updates to the issue. Please let me know if you need something else.

@mikebronner
Copy link
Owner

@andersonpem Could you provide the line section before #0 in the stack trace as well? The stack trace usually starts with a timestamp.

I don't see any issues with the query that would cause concern. Everything should be working. What cache driver are you using?

@andersonpem
Copy link
Author

I'm using Redis. I don't have access to my computer right now. I post the information you asked as soon as I can :)

@andersonpem
Copy link
Author

After thoroughly checking the code, I think I found the problem. Spoiler: it wasn't the code.
I do use two different command line interfaces for eloquent testing: Tinker and Tinx. I use Tinx a lot because it has some handy shortcuts. I can hot reload the tinker session from Tinx using re(), for example.
It seems like Tinx can't handle the cache flushing correctly when an object is deleted with that kind of function. The exact same methods work with no problem using the default CLI, Tinker.
I'll keep investigating why it does not work in that specific condition :) thanks.

@mikebronner
Copy link
Owner

@andersonpem Thanks for following up. I'll close this for now, but if you find out more, please do re-open and follow up here. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants