MemCached Jekyll

552 mots | info web |

MemCached Jekyll

[english version at bottom]

En réflechissant à une solution pour un problème je me suis penché sur memcached et comment le faire marcher avec thin et nginx.
Je me suis vite rendu compte d’un problème de taille : Nginx et son module memcached ne remplissent pas memcached avec le contenu tout neuf. Il faut utiliser un script pour ça, hélàs le seul que j’ai trouvé était en Perl. En quelques minutes j’ai trouvé une doc sur une gem Ruby pour taper dans memcached (memcached-client). Après un break je m’y suis donc remis.

Mais j’avais un autre soucis : nginx ne semblait pas se connecter sur le memcached, il allait directement voir thin. Sombre idiot que je suis, nginx est simple et efficace, il faut pas chercher compliqué mais logique. Mon problème était que je pensais qu’en lui donnant l’adresse du serveur http pour qui il faisait proxy (thin), et du serveur memcached il se débrouillerait pour retrouver ses petits. Et bien non. C’est illogique.

La solution c’est de d’abord lui dire d’aller voir memcached et d’ensuite taper sur thin si memcached lui renvoie un 404. La bonne configuration est donc la suivante :

Oui cette configuration s’attend à ce qu’il y ait tout dans le memcached, c’est normal c’est un Jekyll que j’ai mis derrière pour tester. Mais dans le cas où vous ne voudriez pas tout, il vous suffit de rajouter quelques conditions.

Ah donc mon nginx allait désormais correctement demander au memcached s’il avait du pain. Mais il fallait mettre du pain dans la poche de memcached pour qu’il puisse dire “oui”. J’ai donc fait un script pour ça (utilisant memcached-client), publié sur github : kanjied.

MemCached Jekyll

So, looking for a solution about a problem I’ve got, I’ve looked into memcached and how to make it work with thin and nginx.
I quickly saw a problem : Nginx and its memcached module are not adding initial content to memcached. We need to use a script for that. Alas the only one I found was in Perl. After few minutes I found a doc about the memcached-client rubygem.

But the main trouble was that nginx didn’t seem to connect to memcached, it was always just asking to thin. As the stupid code monkey I am I didn’t realise how much simple and logic nginx is. My problem was that I was thinking that giving nginx both memcached and proxy lines in the main config of the server would make him figure out from where it should grab the content to send back. In fact, nginx is simple : it checks only one. The solution is simple : tell him to check memcached for the content, if it’s not there memcached will send back a 404, and then on that case you can tell nginx where to go : thin. So here is the config :

Yes, that is a very simple one, checking everyfiles with memcached, if you need to check only few ones just add filters. I was using this just to test with Jekyll so …

So now nginx was requesting content correctly to memcached, but I still needed to put content there. So I read the memcache-client doc and wrote a small script to do exactly that : kanjied.