Manual:Memcached/cs

memcached je úložiště objektů v paměti, které může MediaWiki použít pro hodnoty ukládání do mezipaměti, aby se snížila potřeba provádět náročné výpočty a snížilo zatížení databázových serverů.



Kdy použít
Pro malý web hostovaný jediným serverem nemusí instalace Memcached stát za potíže. V takových případech zvažte konfiguraci MediaWiki tak, aby místo hlavního úložiště objektů používala APCu PHP. Pro velké webové stránky, jako je Wikipedie a obecně pro wikiny hostované více webovými servery, je Memcached běžnou volbou pro mezipaměť objektů MediaWiki.

Další informace o možnostech ukládání do mezipaměti v MediaWiki naleznete na stránce Příručka:Ladění výkonu § Ukládání objektů do mezipaměti



Jak nainstalovat Memcached
Většina správců balíčků pro Linux a macOS má k dispozici balíčky připravené k použití pro Memcached (včetně Debianu, Fedory a Ubuntu).

Pokud pro vaši distribuci není k dispozici žádný balíček, možná jej budete muset zkompilovat ze zdroje stažením z memcached.org. Ke kompilaci ze zdroje budete také potřebovat libevent. Memcached a libevent jsou projekty s otevřeným zdrojovým kódem vydané pod licencemi ve stylu BSD.

Pro více informací o Memcached obecně viz také "Memcached" na Wikipedii.

Zabezpečení
Memcached nemá žádné zabezpečení ani ověřování. Ujistěte se prosím, že váš server je správně zabezpečen firewallem a že porty používané pro servery s memcachingem nejsou veřejně přístupné. Jinak může kdokoli na internetu vkládat data do vaší mezipaměti a číst je.

Útočník obeznámený s interními informacemi MediaWiki by toho mohl využít k tomu, aby si udělil přístup pro vývojáře a smazal všechna data z databáze wiki a také získal hash hesel všech uživatelů a e-mailové adresy.



PHP klient pro Memcached
V době psaní tohoto článku (MediaWiki 1.27) používá MediaWiki čistě PHP klienta memcached (na základě práce Ryana T. Deana). Podporuje také rozšíření PECL php-memcached. To use Memcached with MediaWiki, PHP must be compiled with  (this is the default).

To read more about how to select Memcached as the backend for different parts of MediaWiki, see Manual:Caching.

Setup
If you want to start small, just run one memcached on your web server: memcached -d -l 127.0.0.1 -p 11211 -m 64

(to run in daemon mode, accessible only via loopback interface, on port 11211, using up to 64MB of memory)

In your LocalSettings.php file, set:

The wiki should then use memcached to cache various data. To use multiple servers (physically separate boxes or multiple caches on one machine on a large-memory x86 / Power box), just add more items to the array. To increase the weight of a server (say, because it has twice the memory of the others and you want to spread usage evenly), make its entry a subarray:

SELinux
For systems with SELinux, there are several policies for Memcached. To allow Apache (httpd) to access Memcached, you must set the following policy:

setsebool -P httpd_can_network_memcache 1

Loss of session data when saving
If you store session data in memcached, and users see this message intermittently when they try to save edits:

then one or more of your memcached servers might have a misconfigured  file. On each of your memcached servers, make sure the server's own hostname is mapped to localhost:

127.0.0.1 servername.here localhost localhost.localdomain ...

Otherwise, the server might not be able to connect to its own memcached process.

Using memcached in your code
If you're writing an extension that does expensive database queries, it might be useful to cache the data in memcached. There are a few main ways to get a handle to memcached:


 * ...use this if you want a memory-based shared cache with explicit purge ability in order to store values derived from persistent sources
 * ...use this if you want a memory-based ephemeral store that is not shared among datacenters
 * ...use this if you want a memory-based ephemeral cache that is not shared among web servers
 * ...use this if you want any available cache, which may or may not be per-datacenter, even an emulated one that uses a SQL database. Note that these may return handles that talk to Redis, APC, MySQL or other stores instead. The use of the word "memcached" is historically due to the API being defined around the simple commands that memcached supports and the fact that, to date, memcached is normally the best general-purpose cache store.

Extensions that have specific needs (like persistence) should define new configuration settings like or. Code using the caches can pass them to  and , respectively.

The following code snippet demonstrates how to cache the results of a database query into memcached for 15 minutes and query memcached first for the results instead of the database.

The abstract BagOStuff and WANObjectCache classes define and document all of the available functions:



Old Development Notes
Broadly speaking, we'd like to be able to dump lots of data in the cache, use it whenever we can, and automatically expire it when changes are made.

Expiration model

 * explicit expiration times: memcached lets us set an expiration time on an object when we store it. After the time is up, another request for the object will find that it has expired and return nothing to us.
 * pro: last-ditch fallback to let data that could be updated badly eventually fall out of the cache
 * con: we have to know ahead of time when it will cease to be invalid. hard to do when we're dealing with user edits!


 * delete cached objects when we know we're doing something that will cause them to be invalid but are not in a position to update them while we're at it
 * pro: fairly simple; the item will be reloaded from the database and recached when it's next needed
 * con: if this will affect a large number of related items (for instance, creating or deleting a page invalidates the links/brokenlinks tables and rendered HTML cache of pages that link to that page) we may have to hunt them all down and do a lot of updating


 * include timestamps on cached objects and do our own expiries based on dependencies
 * pro: can expire many objects at once by updating a single node they depend on
 * con: more things to load; multiple dependencies could be trickier to work with

Questions & Answers
Q: Can I search on part of a key or a regular expression on a Memcached server?

A: No, you can only search for an exact key if you need more information on what you could possibly do you can check out the Memcached protocol

Q: Can I have multiple wikis point to the same Memcached server?

A: Yes, as long as each have different wiki-ids ($wgDBname). Certain cache keys are intentionally shared in such a scenario, such as rate limiting stuff.