Rails: Scheduled task to warm up the cache?

前端 未结 4 810
暗喜
暗喜 2021-02-06 08:08

I am using the following to cache a slow loading page using memcached:

caches_action :complex_report, :expires_in => 1.day

The controller ac

4条回答
  •  长发绾君心
    2021-02-06 08:39

    Here is an expansion on the previous cron based solution which uses curl's ability to store cookies so that you can auth in one step and then use the cookie again as an authenticated user in the next step. So if you put these lines in a script called "prepare_cache.sh"

    rm /tmp/cookiejar
    curl --request POST -d "login=" -d "password=" -c /tmp/cookiejar http://yourwebpages.url/login
    curl --request GET -b -c /tmp/cookiejar http://yourwebpages.url/page_to_cache
    rm /tmp/cookiejar
    

    replacing the login and password parameters with ones which match the variables used in your login form and obviously the urls to call. I'm removing the cookiejar before to make sure there isn't a file there already and removing it at the end to make sure there isn't a cookie floating about with access levels it shouldn't have.

    Then you can call this script with the cron job:

    */15 * * * * /home/myname/prepare_cache.sh > /dev/null 2>&1
    

    And hopefully that should work. Seemed to work for me when I tried it.

提交回复
热议问题