Linux设置chrome缓存至内存,及开关机同步

默认chrome缓存位置在~/.cache/google-chrome中,磁盘io很多。为减少磁盘io,保护磁盘同时加快chrome速度,可设置缓存使用内存盘,唯一缺点是重启后缓存丢失。

1. 开机自动挂载内存盘: (使用/dev/shm, 无需自己创建内存盘)

1) sudo mkdir /ramdisk
2) sudo chmod 777 /ramdisk
3) sudo vim /etc/fstab, 添加如下内容:

# ramdisk
none /ramdisk tmpfs nodev,nosuid,noatime,mode=1777,size=512M   0   0

可使用 df -h 命令查看 /ramdisk 虚拟分区大小。

2. 使用 'ln -s' 链接缓存文件夹到内存盘中:

1) touch /home/dylanchu/scripts/chrome/onboot.sh
2) chmod +x /home/dylanchu/scripts/chrome/onboot.sh
3) vim /home/dylanchu/scripts/chrome/onboot.sh, 内容如下:

#!/bin/sh

#for the google chrome cache
/bin/rm ~/.cache/google-chrome -R
/bin/mkdir -p /dev/shm/google-chrome
/bin/ln -sf /dev/shm/google-chrome ~/.cache/google-chrome

#for the chromium cache
#/bin/rm ~/.cache/chromium
#/bin/mkdir -p /dev/shm/chromium
#/bin/ln -sf /dev/shm/chromium ~/.cache/chromium

# import dumped cache file to ram:
echo [`date +"%Y-%m-%d %H:%M"`] On boot - Importing caches to ram >> /home/dylanchu/chromecache_sync.log
/home/dylanchu/scripts/chrome/chromecache import >> /home/dylanchu/chromecache_sync.log
echo [`date +"%Y-%m-%d %H:%M"`] On boot - Caches imported to ram >> /home/dylanchu/chromecache_sync.log

4) 上面 /home/dylanchu/scripts/chrome/chromecache 是一个用来导入已同步缓存包的脚本 (需要先安装lzop压缩插件) ,内容如下:

#!/usr/bin/sh

# invoke this after reboot and before shutdown
# make sure that you already have 'lzop' installed on your system

case "$1" in
 import)
   cd /dev/shm
   tar --lzop -pxf /home/dylanchu/.cache/chromecache-backup.tar.lzop
   ;;
 dump)
   cd /dev/shm
   # delete files larger than 3MB
   find ./google-chrome/ -size +3M -exec rm {} \;
   tar --lzop -pcf /home/dylanchu/.cache/chromecache-backup.tar.lzop google-chrome/
   ;;
 *)
   echo -e "Usage: $(cd `dirname $0`; pwd)/chromecache {import|dump}"
   exit 1
   ;;
esac

exit 0

5) 添加上述 '3' 中的onboot.sh脚本到开机自启动:
这里用xfce gui的 “会话和启动” (session-settings),点击添加,并设置名称和脚本路径。

6) 添加关机前导出缓存到硬盘:

  • a) vim /home/dylanchu/scripts/chrome/onshutdown.sh, 内容如下:
#!/bin/sh

# dump cache files from ram to disk:
echo [`date +"%Y-%m-%d %H:%M"`] On shutdown - Dumping caches to disk >> /home/dylanchu/chromecache_sync.log
/home/dylanchu/scripts/chrome/chromecache dump >> /home/dylanchu/chromecache_sync.log
echo [`date +"%Y-%m-%d %H:%M"`] On shutdown - Caches dumped to disk >> /home/dylanchu/chromecache_sync.log
ping -c 3 127.1 > /dev/null
  • b) 加可执行权限chmod +x /home/dylanchu/scripts/chrome/onshutdown.sh

  • c) sudo vim /lib/systemd/system/chromedumpcache.service,内容如下:<测试无效>
[Unit]
Description=Dump chrome caches from ram to disk at shutdown.
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target

[Service]
Type=simple
RemainAfterExit=true
# when system start
ExecStart=/bin/true
# when system shutdown
ExecStop=/home/dylanchu/scripts/chrome/onshutdown.sh

[Install]
WantedBy=multi-user.target halt.target reboot.target shutdown.target

修改:

systemctl get-default 命令可以查看系统启动默认进入哪个界面

multi-user.target是字符界面,改为graphical.target发现正常工作:(测试发现仅关机和重启时工作)

扫描二维码关注公众号,回复: 3462233 查看本文章
[Unit]
Description=Dump chrome caches to disk
DefaultDependencies=no
Before=umount.target shutdown.target reboot.target halt.target

[Service]
Type=simple
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/home/dylanchu/scripts/chrome/onshutdown.sh

[Install]
WantedBy=graphical.target
  • d) sudo systemctl enable chromedumpcache.service
  • e) sudo systemctl daemon-reload
  • e) sudo systemctl status chromedumpcache.service

3. 重启后生效。


参考:
https://wiki.archlinux.org/index.php/Tmpfs
https://wiki.archlinux.org/index.php/Chromium/Tips_and_tricks#Cache_in_tmpfs

内存盘和硬盘同步:
http://docs.observium.org/persistent_ramdisk/
https://askubuntu.com/questions/416299/execute-command-before-shutdown-reboot(用systemd关机前执行指令)
http://blog.csdn.net/kai165416/article/details/79449638 (删除大于固定大小的文件)

https://askubuntu.com/questions/794290/create-ramdisk-on-16-04-for-chrome
https://www.omgubuntu.co.uk/2010/11/move-google-chrome-cache-to-ramdisk

http://www.bubuko.com/infodetail-1900178.html
http://blog.51cto.com/10237569/1871723

https://superuser.com/questions/1016827/how-do-i-run-a-script-before-everything-else-on-shutdown-with-systemd

http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html

https://www.freedesktop.org/software/systemd/man/bootup.html#System%20Manager%20Bootup

猜你喜欢

转载自www.cnblogs.com/dylanchu/p/9750494.html