Use Memcached with Unix sockets for caching

The official documentation recommends using Redis for caching like this:

'memcache.local' => '\OC\Memcache\APCu',
'memcache.distributed' => '\OC\Memcache\Redis',
'redis' => [
     'host'     => '/run/redis/redis-server.sock',
     'port'     => 0,
     'dbindex'  => 0,
     'password' => 'secret',
     'timeout'  => 1.5,
],

I have Memcached installed and running. It’s configured to use Unix sockets instead of TCP connections because it’s much faster. How can I use Memcached instead of Redis in the configuration from above ? I know that the same documentation recommends using Memcached like this:

'memcache.local' => '\OC\Memcache\APCu',
'memcache.distributed' => '\OC\Memcache\Memcached',
'memcached_servers' => [
     [ 'server0.example.com', 11211 ],
     [ 'server1.example.com', 11211 ],
     [ 'server2.example.com', 11211 ],
 ],

but as I mentioned, I’m using Unix sockets (/var/run/memcached/memcached.sock), not TCP connections. I have good reasons not to use Redis. I’m already using Memcached for WordPress object caching with great success, and I also want to use it for Nextcloud. I also don’t use APCu for local caching because I had a bad experience with APCu. Could I use Memcached as memcache.local too ? Thank you !

Not sure if the question is “can I use memcached with unix socket on nextcloud”, but I will answser this one here anyway :wink:

Yes you can use a Memcached listening on a Unix Socket with the following configuration :

'memcached_servers' => [
     [ '/path/to/your/memcached.sock', 0 ],
],

Since Nextcloud is directly using the PHP \Memcached class, the documentation to describe a server is here : https://www.php.net/manual/en/memcached.addserver.php

Also, I’m using this socket-based memcached both for memcache.local and memcache.distribnuted as below without any problem (on NC 21):

  'memcached_servers' => [
      [ '/tmp/ben.memcached.sock', 0 ],
  ],
  'memcache.local' => '\OC\Memcache\Memcached',
  'memcache.distributed' => '\OC\Memcache\Memcached',

1 Like

Thank you very much octopuce for your reply ! I almost lost hope on getting an answer on this topic. I’m so glad that you have been able to use Memcached with Unix sockets for Nextcloud. Those who don’t use Memcached don’t know what they are loosing. If it worked for you, it should also work for me.
However, I’m still on Nextcloud 20 and it didn’t work with the settings specified above. I get the following error in my web server’s error log (I use Nginx and PHP 7.3, on a Debian 10 VPS):

PHP Fatal error:  Uncaught OC\HintException: [0]: Memcache \OC\Memcache\Memcached not available for distributed cache (Is the matching PHP module installed and enabled?)    thrown in /var/www/cloud.mydomain.com/lib/private/Memcache/Factory.php on line 113" while reading response header from upstream, client: 1.2.3.4, server: cloud.mydomain.com, request: "GET /ocs/v2.php/apps/notifications/api/v2/notifications HTTP/2.0", upstream: "fastcgi://unix:/var/run/php/php7.3-fpm.sock:", host: "cloud.mydomain.com

My Memcached server is working fine and I can see it stores data from my WordPress websites which are really fast because of this cache. I installed Memcached standard from the Debian repository, and my relevant Memcached configuration in /etc/memcached.conf is:

-d
logfile /var/log/memcached.log
-m 512
-u memcache
-s /var/run/memcached/memcached.sock
-a 770
-c 2048
-P /var/run/memcached/memcached.pid

Anyway, I’ll update Nextcloud to version 21 and I hope it will work with the new version.