tags:

views:

64

answers:

1

I'm a Java and C# developer, and, I admit, I'm not that good in PHP.

I need to store an object in an application scope that lives as long as the app itself is running. I can't save it in the Session, because it expires, also I can't serialize it to disk.

Is there something like a C# Application object in PHP?

+3  A: 

PHP has an application scope of sorts. it's called APC (Alternative PHP Cache).

Data should be cached in APC if it meets the following criteria:

  1. It is not user session-specific (if so, put in $_SESSION[])
  2. It is not really long-term (if so, use the file system)
  3. It is only needed on one PHP server (if not, consider using memcached)
  4. You want it available to every page of your site, instantly, even other (non-associated) PHP programs.
  5. You do not mind that all the data stored in it is lost on Apache reload/restart.
  6. You want data access far faster than file-based, memcached, or (esp.) database-based.

APC is installed on a great many hosts already, but follow the aforementioned guide to get installed on your box. Then you do something like this:

if (apc_exists('app:app_level_data') !== false)
{
    $data = apc_get('app:app_level_data');
}
else
{
    $data = getFromDB('foo');
    apc_store('app:app_level_data', $data);
}
hopeseekr