Jump to content

Recommended Posts

Hello,

 

I am looking for a way to store a variables/object on the server in physical memory so I dont have to call a function to get a value on every page. Currently I have an application that uses an API. I must get a token from the API which I pass into the various function calls. The token is only valid for 60 minutes, but currently my application gets a new token every time a page refreshes, which is very slow. It has to hit the api server get the token and return it. Would be much faster if I get the token and store it on the server.

 

I would love something like:

 

$cache->insert("token", "123456789");

$myToken = $cache["token"];

 

 

Link to comment
https://forums.phpfreaks.com/topic/71765-way-to-cache-a-variable-in-server-memory/
Share on other sites

You could try using a flat file or a database.

 

Flat file example:

 

$maxval = 3600; //valid for an hour
$file = 'some/folders/cache.txt'; //.txt added because I like it.... cache file

$mtime = (@file_exists($file)) ? filemtime($file) : 0;

if($mtime + $maxval < time()) {
     $key = GrabNewKey($file);
     /* in GrabNewKey, the key would be gotten and saved to the cache file before being returned. */
}
else {
     $key = GetKey($file);
     //GetKey would basically just return the contents of $file
}

 

Edit: You would want to make sure to lock the file while it's being modified by the way or weird things could happen.

Yeah I was thinking of using a file or db, but again the point of this is speed, so calling a query and retrieving that value inst much faster then calling the API, perhaps even slower. Same can be said for file IO. Anyway to store in memory? In ASP.NET this is possible.

Bleh... I'm sure it's possible in C++ or any other low language, but in PHP, I don't think you can store it in memory (with the script terminating).

 

Accessing a file would most likely be faster than accessing an API, and accessing a file in most situations (as long as the file didn't have to be scanned basically) would be faster than a DB query.

 

Is this API on a remote host?  If it is, then a query or file would be faster.

 

I personally would go with a file, but that's just me ;p.

Yeah the api is a remote host, so yeah very slow. That why I want to NOT call it on every page.

 

I will try a file.

 

I am just not sure how to handle expiration though, how do I tell if the token is valid, they expire after 60 minutes. I didnt have to worry about expiration before since it was generated on every page refresh.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.