CoreserverでRedis Object Cacheを使う

Coresercerで、ほぼ下記リンクの通りにして、Redis Object Cacheを使うように設定しました。

コアサーバーでRedisを動かしてWPプラグイン「Redis Object Cache」を適用させる | Gadgetter ガジェッター
コアサーバーにRedisをインストールし、Wordpressプラグイン「Redis Object Cache」を適用させて高速化を行ったので、これらの導入手順を紹介します。     契約しているコアサ …

現在の最新版は8.6.1で、GitHubからダウンロードできます。

GitHub - redis/redis: For developers, who are building real-time data-driven applications, Redis is the preferred, fastest, and most feature-rich cache, data structure server, and document and vector query engine.
For developers, who are building real-time data-driven applications, Redis is the preferred, fastest, and most feature-r...

後、書いてなかったことで調べてやったことは、radis.confの先頭にmaxmemoryの記述を追加したり…

radis.conf

unixsocket /home/hasera/.local/bin/redis.sock
unixsocketperm 700
timeout 600
maxmemory 1gb
maxmemory-policy allkeys-lru

何も書かなくても私のブログでは100MBも使わないようで、全然大丈夫だと思うのですが、(Core-Xなので)6GBも使えるので、1GBくらいに設定しておいても良いでしょう。「allkeys-lru」は、万一1GB使い切った場合、古い順に削除してメモリを確保する…はずです。

cronの設定

1時間に1回動かすスクリプト

redis-serverプロセスがなかったらredis-serverを起動します。これは↑のリンク先の通り。

#!/bin/sh

cd /home/hasera/
echo ‘— cron1h.sh start —‘ >> cron.log
date >> cron.log
uptime >> cron.log

COUNT=`ps aux | grep ‘redis-server’ | grep -v grep | wc -l`

if [ $COUNT = 0 ]; then
echo “redis-serverE is dead.” >> cron.log
/home/hasera/.local/bin/redis-server /home/hasera/.local/bin/redis.conf –daemonize yes
else
echo “redis-server is alive.” >> cron.log
fi

echo ‘othlog.hasera.net/wp-cron.php start’ >> cron.log
curl -s “https://othlog.hasera.net/wp-cron.php” -o /dev/null -w ‘%{http_code} %{time_total}[sec]\n’ >> cron.log

tail cron.log -n 500 > cron.tmp
mv cron.tmp cron.log
echo ‘— cron1h.sh end —‘ >> cron.log

※–daemonize yes を付けるとバックグラウンドで起動できます。nohupも不要です。
※WordPressのcron(wp-cron)は止めていて、サーバーのcronで1時間に1回にアクセスして動かすようにしています。

1日1回動かすスクリプト

1日1回、キャッシュをフラッシュ(クリア)するようにしました。不要な処理かも知れませんが、永続ってなんか怖いので。

これは、サーバーのcornの設定で、午前3時30分に動かすようにしました。

redis-serverプロセスがなかったらredis-serverを起動します(一応)

#!/bin/sh

cd /home/hasera/
echo ‘— cron1d.sh start —‘ >> cron.log
date >> cron.log
uptime >> cron.log

COUNT=`ps aux | grep ‘redis-server’ | grep -v grep | wc -l`

if [ $COUNT = 0 ]; then
echo “redis-server is dead.” >> cron.log
/home/hasera/.local/bin/redis-server /home/hasera/.local/bin/redis.conf –daemonize yes
else
echo “redis-server flushall.” >> cron.log
/home/hasera/.local/bin/redis-cli -s /home/hasera/.local/bin/redis.sock flushall
fi

tail cron.log -n 500 > cron.tmp
mv cron.tmp cron.log
echo ‘— cron1h.sh end —‘ >> cron.log

Coreserverのcronジョブの設定

cron

・1時間に1回(毎時0分)と、サーバー再起動時に、cron1h.shを動かします。
・1日1回(3時30分)に、cron1d.shを動かします。

タイトルとURLをコピーしました