#12. Memcache

Uninstallation/Installation,

sudo apt-get remove memcached

sudo apt-get remove --auto-remove memcached

sudo apt update && sudo apt upgrade

sudo apt install memcached 


# For the use of mem-cache

gem 'dalli'

root@test_app:~# memcached -h | head -1

memcached 1.5.22


rails dev:cache

root@test_app:~# ls tmp/

cache  data  development_secret.txt  miniprofiler  pids  res  restart.txt  sockets

root@test_app:~# rails dev:cache

Development mode is now being cached.

root@test_app:~# ls tmp/

cache  caching-dev.txt data  development_secret.txt  miniprofiler  pids  res  restart.txt  sockets

root@test_app:~# rails dev:cache

Development mode is no longer being cached.

root@test_app:~# ls tmp/

cache  data  development_secret.txt  miniprofiler  pids  res  restart.txt  sockets


touch tmp/restart.txt

root@test_app:~# ls tmp/

cache  caching-dev.txt data  development_secret.txt  miniprofiler  pids  res  restart.txt  sockets

root@test_app:~# touch tmp/restart.txt 

root@test_app:~# ls tmp/

cache  caching-dev.txt data  development_secret.txt  miniprofiler  pids  res  restart.txt  sockets


Inside container,

/etc/init.d/memcached status
or
service memcached status

/etc/init.d/memcached restart
or
service memcached restart

apt-get update && apt-get install telnet

root@test_app:~#  telnet 127.0.0.1 11211

Trying 127.0.0.1...

telnet: Unable to connect to remote host: Connection refused

Ctrl + ] - to close telnet connection


log file,

cat  /var/log/memcached.log

config file,

sudo vim  /etc/memcached.conf

Examples,

Rails.cache.write("foo", "bar")

puts Rails.cache.read("foo")

def fetch_current_time
  rails. Cache. Fetch('current_time', expires_in: 10. Seconds) do
    time. Now
  end
end

while true
  puts fetch_current_time
  sleep 9
end

def fetch_api_settings
  rails. Cache. Fetch('api_settings', expires_in: 1. Minutes) do
    ApiSetting. All
  end
end

while true
  puts fetch_api_settings
end



Default IP and port,

'127.0.0.1:11211'


Useful commands

sudo systemctl stop memcached

sudo systemctl start memcached

sudo systemctl restart memcached

sudo systemctl status memcached


Socket settings

sudo mkdir /var/run/memcached

sudo chown memcache:memcache /var/run/memcached

in the conf file of memcached add

-p /var/run/memcached/memcached.pid


Steps to use unix socket with memcached

if you use memcached the user created is called memcache on debian and ubuntu.

Make the memcache user a member of the www-data group which apache and nginx run as by default on ubuntu systems.

Sudo usermod -g www-data memcache

sudo vim /etc/memcached.conf

comment out the port and listen lines to disable tcp.

# -p 11211

# -l 127.0.0.1



add the lines for specifying the memcached socket path and permissions.

# set unix socket which we put in the folder /tmp/memcached and made memcache user the owner

-s /tmp/memcached.sock

# set permissions for the memcached socket so memcache user and www-data group can execute

-a 775

Save the memcached.conf file

sudo service memcached restart

now check if the memcached unix socket is there,

ls -lh /tmp

you should see the memcached.sock file there.

now you can configure your service to use memcached unix sockets instead of tcp.



You can specify the memcached server with a unix socket

notice we have set the port to 0.

config.cache_store = : mem_cache_store, '/tmp/memcached.sock:0'

List files
root@test_app: ~# ls -lh /tmp

total 4.0k

srwxrwxr-x 1 memcache memcache 0 jun 22 15:50 memcached. Sock

drwxr-xr-x 5 root root 4.0k jun 22 15:13 passenger.3ybro0a



root@test_app: ~# ls /var/run

crond.pid crond.reboot lock log memcached.pid mount nginx.pid sendsigs.omit.d shm sshd syslog-ng.pid systemd user utmp


root@test_app: ~# ps aux | grep memcache

memcache 2926 0.0 0.0 409776 4068 ? Sl 16:24 0:00 /usr/bin/memcached -m 64 -u memcache -s /tmp/memcached. Sock -a 775 -p /var/run/memcached/memcached. Pid

root 2963 0.0 0.0 9348 712 pts/0 s+ 16:25 0:00 grep memcache

Use the following inside environment.rb to see the cache logs

ActiveSupport::Notifications.subscribe(/cache/) do |*args|
   puts"========================#{args.inspect}"
end

References:
1. https://www.rubydoc.info/github/mperham/dalli/Dalli%2FClient:initialize
2. https://guides.wp-bullet.com/configure-memcached-to-use-unix-socket-speed-boost/


Comments

Popular posts from this blog

#6. Fetch time in rails

#8. nginx+puma deployment using capistrano

#21. Rails scope