Sam Levin
Sam Levin

Reputation: 3416

Force update of php apc cache data variable?

Say I have a config.php file which contains definitions for a bunch of preferences. My init scripts loads these values into memory.

Say instead of loading them into memory, I loaded them into apc. How would I force apc to update the variables when I changed the contents of config.php manually? Would I need to create a utility script of some sort which performed the change so it could insure cached variables were updated automatically?

If a variable exists within apc, and a new variable is added with the same name as the existing (an updated version) is the old caches value overwritten?

Upvotes: 0

Views: 1368

Answers (1)

Charles
Charles

Reputation: 51411

How would I force apc to update the variables when I changed the contents of config.php manually?

You have a few options here.

  1. When you call apc_store, you can set a "time to live" value in seconds. You can, say, set each of your configuration values to last exactly five minutes. When once is missing (apc_exists), then you can trigger the code that regenerates and recaches them.
  2. You can store the "last update" time in both a place your code can reach and inside APC. Compare the two. If the APC cache is out of date, trigger a refresh.
  3. If you're already automatically caching values, you can have the thing that performs configuration updating call apc_delete on the keys.
  4. If you aren't already automatically caching values, you'd want to make the thing that performs configuration updating simply call apc_store itself.

The first is lowest effort, the second is the most sane, and the third and fourth are probably the right thing to do assuming that configuration updates are programmatic things instead of completely manual things, as you imply here.

If configuration updates are always manual, it would be worth writing a tiny script you can manually invoke that simply clears or updates the cache.

That said, to be very frank, the cost of parsing a pure-PHP config file on every pageview is nonexistant if you're also using APC as an bytecode cache. In that circumstance, caching the values defined within is downright silly.

If a variable exists within apc, and a new variable is added with the same name as the existing (an updated version) is the old caches value overwritten?

Correct, unless you're using apc_add instead of apc_store.

Upvotes: 1

Related Questions