1

I am trying to use Laravel 5 cache in te following way:

public function putUserCacheData($key, $value){
    \Cache::put($this->login, [$key => $value], 30);
}

So I have structure like this

    ['testlogin'] => [
                  'param1' => 'value1',
                  'param2' => 'value2',
                 ]

But I can dlete Item from cache with key param1 for testlogin?

Thanks for your help!

3 Answers 3

2

Try:

Cache::forget('testlogin.param1')

EDIT

You're right - we cannot use dot notation. So only one thing that you can do is to:

$testlogin = Cache::get('testlogin');
unset(testlogin['param1']);
Cache::put('testlogin', $testlogin);
Sign up to request clarification or add additional context in comments.

1 Comment

You can't use dot notation with cache by default
1

You can use cache tags to do this.

public function putUserCacheData($key, $value){
    \Cache::tags($this->login)->put($key, $value, 30);
}

// Remove all entries
Cache::tags($this->login)->flush();

// Remove only param1
Cache::tags($this->login)->forget('param1');

But note that cache tags are not supported when using the file or database cache drivers.

2 Comments

I was using file cache driver, will try now your example with redis, thnx!
For testing you can use array cache driver if you don't have redis or memcache installed.
-1

Unfortunely u can't do it with scratch laravel 5.*. It's better to use database or if its temp data u can easily do it with Sessions:

Session::put('some', ['data' => 'value', 'key2' => 'value2']);

Session::forget('some.data');

return Session::get('some');

4 Comments

Session or database is not cache.
@TheFallen i give him variants. He cant do it with cache in scratch laravel.
@TheFallen I know about tags, see his question, its not about using tags. It's about alternative of section function in laravel 4.
It says Laravel 5 in his question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.