I am working on my first ever Laravel 4 REST project. My basic question is how can I create a global variable that will hold the value to be used in my controller methods? Here are the requirements for it... There needs to be 2 variables ($token - string and $expirationdate - date). I need for all calls to the controller methods for all clients to reference a singleton value (just meaning one value for everyone not that it has to be stored as a singleton). The token is going to be a randomly generated string. However for each call to the method I will need to check the variable for the expiration date. If it has expired then I need to be able to update the $token variable with a newly generated random string and then modify the expiration date to be something like todays date + 30 days.
So what I am thinking is there a way for the variable to be stored in memory on the server that can be accessed in a thread safe manner. Or will this require that I store the value in some sort of settings file which I can read and write to also in a thread safe manner.
I tried adding the following line in the app.php... 'securitytoken' => 'defaultvalue',
Then I could use the following line of code to access the value in my controller $token = Config::get('app.securitytoken');
However if the token expires I tried adding this line to "update" the value of the setting. Config::set('app.securitytoken', Str::random(32));
Then no matter what every call to the controller always gets the 'defaultvalue' returned.
Also I am expecting around 1 million hits per day so that is why I prefer to just save value in memory if needed.
Any help would be greatly appreciated. Thanks Wayne
This sounds like a good use-case for using the cache. I'd create a SecurityTokenService class with a single getToken method. This method would do the following:
Check the cache for an existing token (which will automatically be false if the expiration time has passed)
If no token is found, generate a new token and store it in the cache
Return the token
The only downside is that the token will be reset whenever you use the cache:clear command.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community