Memcached for Moodle 4.5 and 5.x, with locking
Moodle dropped its Memcached cache store in 4.2. Our free cachestore_memcachedshared brings it back with locking and purges that never empty a shared server.
Short answer: Moodle removed its Memcached cache store in 4.2, so a site on 4.5 or 5.x that runs Memcached has no store for it in core. Our free plugin, cachestore_memcachedshared, is a new Memcached store for Moodle 4.5 LTS to 5.3. It locks, it spreads keys over several servers, and purging a cache never empties the Memcached server, so other sites and apps on the same servers keep their data.
This is the thirty-fourth plugin in our series: one plugin for each Moodle plugin type, each one teaching DevLMS Studio, our AI plugin builder, how that type works. Moodle’s tracker issue MDL-77161 records the removal. The reasons given were low use, no locking and weak support for several servers. People still ask on the forums whether Memcached works with current Moodle, and MoodleHQ’s copy of the old store is the 4.1 code, unchanged since 2022. When we checked the plugins directory, it listed no cache store for Moodle 4.5 or later.
What it does
- Purging never empties the server. Unless it was told the server was shared, the old store called Memcached’s flush, which wipes everything on the server, other sites’ data included. This one stamps every value with a version. A purge moves the version on, old values stop being read, and Memcached clears them out when it needs the space.
- Locking. Only one process can take a lock. Others wait for as long as you set, then give up. A lock held by a crashed process expires on its own.
- Several servers. Keys are spread with consistent hashing, so adding or losing a server moves only part of them.
- A dead server is a quick miss. Timeouts are short, so Moodle falls back to building the data instead of hanging.
- Checked before saving. The add-instance form refuses a server line it cannot parse and a server list where nothing answers.
What it does not do
It handles application caches only: session caches need to list keys, which Memcached cannot do. Each value lives on one server, with no replication. Values over 1 MB are refused unless Memcached is started with a larger item size (-I 4m). Every web server must list the same servers in the same order. We tested it against two Memcached servers in CI, not on a production cluster.
It supports Moodle 4.5 LTS to 5.3 and the code is on GitHub.
How a Moodle cache store works
From Moodle 5.3’s cache code (the same in 4.5):
- A cache store is a class that gets, sets and deletes values for one cache definition at a time. Admins add instances of it and choose which caches go there.
- A store that can lock is what stops two requests rebuilding the same cache at the same moment.
- Purge has to remove every value of one cache, but Memcached cannot list keys. So instead of deleting, the store makes old values unreadable.
- Moodle ships a shared test that every store must pass. We run it, plus our own tests for purges, locks, lost version keys, oversized values and a server that does not answer, and browser tests that serve a course page from the store.
Need a plugin like this for your own Moodle?
Describe it in DevLMS Studio and watch it built and tested in a real Moodle, or ask our team for a fixed price.
Open DevLMS Studio