Skip to content

Object caching

ShopClass has an object cache: a short-lived store for the results of repeated database work — category trees, preferences, location lookups — shared across requests. On a busy site it is the difference between a handful of queries per page and a few dozen.

By default the driver is an in-memory array that lives for one request only and does not persist. That is safe everywhere and helps nothing. Configuring a real backend is a two-line change.

Install the matching PHP extension and confirm PHP can see it:

Terminal window
php -m | grep -E 'memcached|apcu'

The setting does nothing if the extension is missing.

Right for anything with more than one web server, and fine with one.

config.php
define('OSC_CACHE', 'memcached');

That connects to 127.0.0.1:11211. For a different host, or several servers:

define('OSC_CACHE', 'memcached');
$_cache_config = array(
array('default_host' => '10.0.0.5', 'default_port' => 11211, 'default_weight' => 1),
array('default_host' => '10.0.0.6', 'default_port' => 11211, 'default_weight' => 1),
);

Simpler, faster, and confined to one PHP process pool. Right for a single VPS, wrong the moment you add a second web server.

define('OSC_CACHE', 'apcu');

Cached entries live 60 seconds by default. Raise it on a site whose categories and preferences rarely change:

define('OSC_CACHE_TTL', 300);

Longer TTLs mean an admin change can take that long to appear on the front end.

Handy for containers, where editing config.php per environment is awkward:

Variable Purpose
OSC_CACHE Driver name — memcached, apcu, memcache
OSC_CACHE_HOST Server host, for memcached/memcache
OSC_CACHE_PORT Server port, default 11211

An explicit define() in config.php — or a $_cache_config array — always wins over the environment.

After a bulk import, a direct database edit, or anything that changed data behind the application’s back:

Terminal window
php oc-cli.php cache:flush

define('OSC_CACHE', 'memcache') still works and drives the old, unmaintained memcache extension. It is deprecated — use memcached.

Changes in the admin panel take a while to show. That is OSC_CACHE_TTL doing its job. Lower it, or flush after admin work.

The site got slower after enabling it. The cache server is probably unreachable, so every lookup pays a connection timeout before falling through. Confirm the host and port, and that the daemon is running.

Two web servers disagree about what the site looks like. You are on APCu, which is per-server. Move to memcached.