John Davidson

php - How I can update key of Laravel cache?

0 comments
Message:


i store some id in cache like this :


$ids = Cache::remember('customer', 120, function () {
return [1,2,3,4,5,6,7,8,9];
});

how can remove 1 and 2 from cache?



My Answer:
You can update the cache key by first retrieving the current cache value, modifying it, and then storing it back in the cache with the same key. In this case, you can remove 1 and 2 from the cached array like this:

php
$ids = Cache::remember('customer', 120, function () {
return [1,2,3,4,5,6,7,8,9];
});

// Remove 1 and 2 from the cached array
$ids = array_diff($ids, [1, 2]);

// Store the updated array back in the cache
Cache::put('customer', $ids, 120);


This code snippet retrieves the current cached array, removes elements 1 and 2 using the `array_diff` function, and then stores the updated array back in the cache with the same key 'customer'.

Rate this post

3 of 5 based on 6448 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us