Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davidmirv's avatar

Caching an item for current request. Not using session

I swear I read this in the docs once but I can no longer find it in the 5.5. documentation but I need to cache an item or a few items for the current request only but I cannot use Session. Is there a better way to do this I'm currently using Cache::remember with a 1 minute cache time... Thanks!

0 likes
8 replies
skliche's avatar
skliche
Best Answer
Level 42

There might be a better way, but you could write it to the config:

config(['key' => $whatEverYouWantToCache]);

And when you need it you retrieve it from the config. On the next request it should be history ...

2 likes
davidmirv's avatar

Will that work for any variable type or only scalars ?

skliche's avatar

That works with objects like an Eloquent instance, arrays, ... whatever you throw at it, no problems.

Cronix's avatar

the config is just a big array under the hood with all config settings from all config files, so you can put anything in there that you normally could put in an array, which is just about anything.

ARG's avatar

Old thread, but if anyone else is still looking, the $GLOBALS array works fine for this type of caching. Ex:

function get_query_value() {
        $key           = "key_temp_cache";     
        $GLOBALS[$key] = $GLOBALS[$key] ?? \Auth::user()->posts()->get();                         
        return $GLOBALS[$key];
}

Also posted here.

jes490's avatar

You can use array driver for caching. It will live as long as the current request (current process to be precise, but in case of php it's almost the same)

Cache::driver('array')->rememberForever('key', function() { return 'value'; });
6 likes

Please or to participate in this conversation.