Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pushing value to existing cache value in laravel

Tags:

php

laravel

I want to set a value to an existing cache. I have something like this:

Cache::put('key',['foo', 'bar'], $expiresAt);

Now how can I push 'sad' value to this key without deleting last values?

Need something like this after pushing value and getting cache:

{'foo', 'bar', 'sad'}

like image 1000
Mohsen Avatar asked Jan 05 '18 08:01

Mohsen


People also ask

Where does Laravel store cached data?

Laravel provides a unified API for various caching systems. The cache configuration is located at app/config/cache. php . In this file you may specify which cache driver you would like used by default throughout your application.

How do I enable caching in Laravel?

To enable Laravel caching services, first use the Illuminate\Contracts\Cache\Factory and Illuminate\Contracts\Cache\Repository, as they provide access to the Laravel caching services. The Factory contract gives access to all the cache drivers of your application.

How does Laravel cache work?

By default, Laravel is configured to use the file cache driver, which stores the serialized, cached objects on the server's filesystem. For larger applications, it is recommended that you use a more robust driver such as Memcached or Redis. You may even configure multiple cache configurations for the same driver.

What is Memcached in Laravel?

Adding caching to Laravel. Memcache is an in-memory, distributed cache. Its primary API consists of two operations: SET(key, value) and GET(key) . Memcache is like a hashmap (or dictionary) that is spread across multiple servers, where operations are still performed in constant time.


1 Answers

Try the following:

Cache::put('key',['foo', 'bar'], $expiresAt);
$key = Cache::get('key');
$key[] = 'sad';
Cache::put('key', $key, $expiresAt);

Just get the existing cache, update it and put it back again.

like image 124
mega6382 Avatar answered Sep 27 '22 23:09

mega6382