swayok / alternative-laravel-cache

Replacements for Laravel's redis and file cache stores that properly implement tagging idea
MIT License
169 stars 26 forks source link

Question: using Cache::forget('tag-test1') instead of Cache::tag('tag_1')->forget('tag-test1') #26

Closed pravo-pro closed 3 years ago

pravo-pro commented 3 years ago

in description you say use Cache::forget('tag-test1') instead this mean what Cache::tag('tag_1')->put('cache_key', $data, $time); was overwritten by Cache::tag('tag_2')->put('cache_key', $data, $time);

or will only be purge together? by Cache::forget('cache_key')

and Cache::tag('tag_1')->forget('cache_key') - leave alive tag_2 cache?

swayok commented 3 years ago

Hi. Cache key is the only thing you need to manage certain cache entry while tags does not actually matter. Tags here are not like folders (Laravel's implementation actually) - all cache entries are accessible without tags. So writhing to cache key with different tag will overwrite cache entry with this key ignoring the tags it was previously created with.

The main idea behind current version of tagging is possibility to delete all related cache entries marked with certain tag with single line of code. Entries may even be marked by several tags while deleted by sinle one of them.

I've updated readme with some clarification about tagging implemented in Laravel and tagging implemented in this package with real usage example.

pravo-pro commented 3 years ago

OK, I understood, tags are only used to reset the cache, when accessing it, tags do not play any role. Therefore, you can not specify them when you apply.

I did some tests - everything is ok

        Сache::tags(['tag_1', 'common'])->put('cache_key1', '123', 3000000);
        Cache::tags(['tag_2', 'common'])->put('cache_key2', '234', 3000000);
        Cache::tags(['tag_2', 'tag_3', 'common'])->put('cache_key3', '345', 3000000);
        Cache::tags(['tag_3', 'common'])->put('cache_key4', '456', 3000000);

for flush by tags example

        Cache::tags(['tag_2', 'tag_3'])->flush();
        dump(Cache::get('cache_key1')); //123
        dump(Cache::get('cache_key2')); //null
        dump(Cache::get('cache_key3')); //null
        dump(Cache::get('cache_key4')); //null

or we can use forget bu cache_key

        Cache::forget('cache_key3');
        dump(Cache::get('cache_key1')); //123
        dump(Cache::get('cache_key2')); //234
        dump(Cache::get('cache_key3')); //null
        dump(Cache::get('cache_key4')); //456