JustinK101 Posted October 4, 2007 Share Posted October 4, 2007 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"]; Quote Link to comment https://forums.phpfreaks.com/topic/71765-way-to-cache-a-variable-in-server-memory/ Share on other sites More sharing options...
trq Posted October 4, 2007 Share Posted October 4, 2007 What about sessions? Quote Link to comment https://forums.phpfreaks.com/topic/71765-way-to-cache-a-variable-in-server-memory/#findComment-361402 Share on other sites More sharing options...
JustinK101 Posted October 4, 2007 Author Share Posted October 4, 2007 My understanding is that sessions are user based right? I.E. they expire when the browser closes. I need something that spans across all users and keeps state. Quote Link to comment https://forums.phpfreaks.com/topic/71765-way-to-cache-a-variable-in-server-memory/#findComment-361406 Share on other sites More sharing options...
corbin Posted October 4, 2007 Share Posted October 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71765-way-to-cache-a-variable-in-server-memory/#findComment-361413 Share on other sites More sharing options...
JustinK101 Posted October 4, 2007 Author Share Posted October 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71765-way-to-cache-a-variable-in-server-memory/#findComment-361414 Share on other sites More sharing options...
corbin Posted October 4, 2007 Share Posted October 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71765-way-to-cache-a-variable-in-server-memory/#findComment-361420 Share on other sites More sharing options...
JustinK101 Posted October 4, 2007 Author Share Posted October 4, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/71765-way-to-cache-a-variable-in-server-memory/#findComment-361424 Share on other sites More sharing options...
trq Posted October 4, 2007 Share Posted October 4, 2007 If they expire at the same time, just run a cron to update the file. Quote Link to comment https://forums.phpfreaks.com/topic/71765-way-to-cache-a-variable-in-server-memory/#findComment-361426 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.