#$GLOBALS / Registry in Laravel?

16 messages · Page 1 of 1 (latest)

visual breach
#

Is there a $GLOBALS-like equivalent in Laravel? I can use Config::set () and Config::get () and that achieves what I want, but that doesn't seem "correct" since the values I'm storing aren't configuration related.

I don't want the values to persist in the session, nor do I want to use a cache like Redis. I just want to be able to store a value somewhere and then later down the line, be able to retrieve it, like a registry.

Does Laravel do anything like this?

Thanks!

cinder lily
#

Well no, that also sounds like a really bad approach. Might be worth explaining what you're trying to do / achieve, so people can give feedback on how to achieve that, since this sounds much like an XY problem

visual breach
#

Let's say I want to know if a redis connection is available. I can do the following:
app ()->bind ('redisconn', function ()
{
try
{
return Redis::connection ();
}
catch (Exception $e)
{
return false;
}
});

$hasConnection = app ('redisconn');

This would work fine for a redis server that's working as it should, but if it's a slow server or if the server's down, there's a wait time (assume the original timeout value is used).
Additionally, the next time I query for 'redisconn', that same wait time is introduced.

Ideally I would be able to store $hasConnection somewhere so later down the line I can immediately see it's false, so I should continue without redis.

My issue isn't redis specific though. It can apply to anything where I'd like to test a condition at the beginning for use later.

cinder lily
#

Not sure I understand what you're trying to achieve with this, Laravel would need to boot, which means on every single request this would need to happen. And like, what does this solve?

visual breach
#

Let's say I have a route for retrieving movies.
It retrieves the movie's main information. Then retrieves everything related to cast and crew. Then retrieves all user reviews of the movie. Finally it retrieves all places to watch the movie (streaming, DVD, cinema, etc). All of this information gets cached since it doesn't change often.
That can all be achieved in a single query, but that's messy. This stuff is ideally separated.

So you might have:

  • getMovieInformation ()
  • getCastAndCrew ()
  • getReviews ()
  • getWatchLocations ()
    Each of which is something like:
  • Check if cached
  • If cached, return cached information
  • If not cached, retrieve from database and cache. Then return the result.

Going back to the redis example, unfortunately the redis server is operating much slower than it should right now. Everytime it checks to see if the value is cached, there's a long slowdown.

If, however, after the first request we can say "The redis server is slow, or down, so avoid redis altogether", then that solves the problem until the server can be fixed.

#

Basically I just want a way to be able to store some type of information in memory, and be able to retrieve it later. That's the goal.

calm marsh
#

I sure do love using my redis as a secondary db

visual breach
#

I like inefficiency 😛 But the question still stands. Is there a Laravel way of storing a value in memory at some point and being able to retrieve it later? As I said, Config::set () and Config::get () does exactly that, so I'd just like to know if there's something better.

cinder lily
#

Your example of using the service container also does exactly that. But what you're doing sounds just rather weird, why not write your own cache manager (or extend Redis) to fallback to for example file cache

visual breach
#

The example is just to illustrate setting something and retrieving it later. It doesn't have to be related to redis, caching, database, etc. Just being able to set a value (doesn't matter what it is), and being able to retrieve it later in the script.

cinder lily
#

Yeah, but that's just not descriptive it's quite impossible to help you. Like; you could create a global variable, you could create a file where it's stored, you could use the service container, you could write it to your DB, you could call an API. That's why in the beginning I mentioned the XY problem, you already have a solution while not describing the problem you're facing.

visual breach
#

I'm just looking for a generic, non-project specific, way of storing variables. That's all. It sounds like there's nothing really suited to it. Perhaps the following that binds it into the service container is the closest way of doing things:

app ()->instance ('some_value', 5);
app ('some_value');

#

Anyway, thanks for the help guys.

ember fern
#

So you're looking for a global variable, that could change from any place. Normally you try to avoid those when programming

visual breach
#

Correct, and I understand the pitfalls of global variables but they can be useful if used appropriately, such as the above example where I can test at the beginning of a script for a slow cache server and then go directly to the database in that situation. If you keep attempting to access the cache server later in the script, you're going to have slowdowns.

ember fern
#

Using config sounds reasonable, bypass-cache sounds like a config (even if it's never set at the beginning of a request)