Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5: forget cache not working

I try to delete specific cached data using:

Cache::forget('key');

also tried

Cache::pull('key');

but cache still existed in DB

note: I'm using Database cache, and Laravel 5.1.7

like image 281
mwafi Avatar asked Aug 20 '15 17:08

mwafi


People also ask

How do I manually delete Laravel cache?

The following is the syntax to clear cache in Laravel is given below: php artisan cache: clear. php artisan config: clear. php artisan cache: clear.

How do I clear the cache in Laravel?

To clear all Laravel's cache, just run the following command: $ php artisan optimize:clear Compiled views cleared! Application cache cleared!

How do I clear my config cache?

Click Ctrl-A to highlight all the files in the folder and then Ctrl-click cache. ini so it is deselected. Delete all the files. This will leave you with only the cache.


2 Answers

I found the answer, the problem happen because I copy cache key from DB with prefix, and Laravel normally add another prefix, and it will never matched

like image 119
mwafi Avatar answered Oct 03 '22 21:10

mwafi


For the records: if you are using cache tags, the forget method will not work. This is what I managed to do to solve the problem:

    $cache_key = "MY_CACHE_KEY";
    $cache_tags = ["MY_CACHE_TAG"]; //SOMETIMES I USE MULTIPLE TAGS
    if (\Cache::tags($cache_tags)->has($cache_key)) {
        \Cache::tags($cache_tags)->forget($cache_key);
        $results = "CACHE item " . $cache_key . " forgotten";
    } else {
        $results = "Key " . $cache_key . " IS NOT CACHED";
    }
like image 45
Marco Cazzaro Avatar answered Oct 03 '22 22:10

Marco Cazzaro