2015年1月12日 星期一

arch 安裝memcached和php-memcached、php-memcache

安裝系統memcached
$ sudo pacman -S memcached
開機時啟用memcached
# systemctl enable memcached.service
啟動memcached
# systemctl start memcached.service
檢查memcached有沒有跑
# ps aux | grep memcached

# netstat -tap | grep memcached (如果用netstat -ant的話就看預設的port 11211 有沒有開,但是port可以改)
檢查執行中的設定值
$ echo "stats settings" | nc localhost 11211
STAT maxbytes 67108864 #預設最小的64MB
STAT maxconns 1024
STAT tcpport 11211
STAT udpport 11211
STAT inter 127.0.0.1
STAT verbosity 0
STAT oldest 0
STAT evictions on
STAT domain_socket NULL
STAT umask 700
STAT growth_factor 1.25
STAT chunk_size 48
STAT num_threads 4 #預設4個threads
STAT num_threads_per_udp 4
STAT stat_key_prefix :
STAT detail_enabled no
STAT reqs_per_event 20
STAT cas_enabled yes
STAT tcp_backlog 1024
STAT binding_protocol auto-negotiate
STAT auth_enabled_sasl no
STAT item_size_max 1048576
STAT maxconns_fast no
STAT hashpower_init 0
STAT slab_reassign no
STAT slab_automove 0
END
更改memcache設定值:
# vim /etc/conf.d/memcached (注意:2013/5後,裝好memcache,arch不再有這個檔案,要自己建)
# user to run memcached as; also used for pid file ownership
MEMCACHED_USER="memcached"
# see 'memcached -h' for available options
MEMCACHED_ARGS="-m 1024 -l 127.0.0.1 -t 1"
-t 1 =>number of threads to use
-l 127.0.0.1 =>  listen to localhost
預設的port是11211( -p 11211 是禁止的)
-m 1024 => 1024MB RAM分給Memcached(-m 64是禁止的),64MB是最小值
編輯/usr/lib/systemd/system/memcached.service:
 [Unit]
 Description=Memcached Daemon
 After=network.target
 [Service]
-EnvironmentFile=/etc/conf.d/memcached
-ExecStart=/usr/bin/memcached -u $MEMCACHED_USER $MEMCACHED_ARGS
+User=memcached
+# Remove '-l 127.0.0.1' to listen on all addresses
+ExecStart=/usr/bin/memcached -l 127.0.0.1
+Restart=always

 [Install]
 WantedBy=multi-user.target
重啟memcached
# systemctl restart memcached
然後再度檢查執行中的設定值:
STAT maxbytes 1073741824 #更改為1024MB
STAT maxconns 1024
STAT tcpport 11211
STAT udpport 11211
STAT inter 127.0.0.1
STAT verbosity 0
STAT oldest 0
STAT evictions on
STAT domain_socket NULL
STAT umask 700
STAT growth_factor 1.25
STAT chunk_size 48
STAT num_threads 1 #更改為1個threads
STAT num_threads_per_udp 1
STAT stat_key_prefix :
STAT detail_enabled no
STAT reqs_per_event 20
STAT cas_enabled yes
STAT tcp_backlog 1024
STAT binding_protocol auto-negotiate
STAT auth_enabled_sasl no
STAT item_size_max 1048576
STAT maxconns_fast no
STAT hashpower_init 0
STAT slab_reassign no
STAT slab_automove 0
END

安裝php memcached 擴展
我曾想用 pacman -S php-memcache 和 pacman -S php-memcached,來安裝,但是就算更改memcache.ini、memcached.ini後在phpinfo()裡面都沒看到memcached載入。

最後使用pecl來安裝
# pecl install memcache
# pecl install memcached
然後把/etc/php/conf.d 下的memcache.ini 、 memcached.ini 註解拿掉,
memcache.ini:
extension=memcache.so
memcached.ini:
extension=memcached.so

如果ubuntu使用apt-get安裝php5-memcache、php5-memcached
# apt-get install php5-memcache php5-memcached
且web server跑的是nginx
先去 /etc/php5/fpm/conf.d 檢查 memcached.ini  memcache.ini
重啟fpm(如果沒有重啟php5-fpm,phpinfo()是不會新增memcache和memcached的擴展的)
# service php5-fpm restart
重啟nginx
# service nginx restart

寫一段程式測試:
$mc = new Memcached();
$mc->addServer("127.0.0.1", 11211);
 
$result = $mc->get("test_key");
 
if($result) {
  echo $result;
} else {
  echo "No data on Cache. Please refresh page pressing F5";
  $mc->set("test_key", "test data pulled from Cache!") or die ("Failed to save data at Memcached server");
}
# php memcache.php
No data on Cache. Please refresh page pressing F5
# php memcache.php
test data pulled from Cache!

批量刪除key特定開頭的緩存

https://stackoverflow.com/a/25168042  memcache and wildcards
新增一個deleteKeysByIndex.php檔案

function deleteKeysByIndex($search) {
    $m = new Memcached();
    $m->addServer('127.0.0.1', 11211); // 127.0.0.1 緩存server IP
    $keys = $m->getAllKeys();
    foreach ($keys as $index => $key) {
        if (strpos($key,$search) !== false) {
            $m->delete($key);
        } else {
            unset($keys[$index]);
        }
    }

    // returns an array of keys which were deleted
    return $keys;
}

$keys = deleteKeysByIndex("CacheKey-User-"); // 刪除 CacheKey-User-開頭的緩存。如:CacheKey-User-1、CacheKey-User-2 ...
print_r($keys);  // 輸出被刪除的key
用法:
$ php deleteKeysByIndex.php

沒有留言:

張貼留言