3

I have a bash script which is run every 10m by cron. The script performs an expensive calculation for some value (say variable x=value). I need to "cache" this value for 2-3 hours. What are possible solutions to this problem?

I tried memcached but it doesn't seem to play well with bash.

3 Answers 3

11

Write a second script that does the actual calculation and saves the result to a file:

# calculate $curval
printf '%s' "$curval" > /var/foo/value.txt

Schedule it with cron to run every 2-3 hours.

In the "every 10 minutes" script, simply read the current value from the file:

curval=$(< /var/foo/value.txt)

A nice refinement is to call the calculation script from the "every 10 minutes" script if the value.txt file doesn't exist yet. You could even make it add the crontab entry if it's missing.

2
  • 4
    Three issues: You should quote $curval to avoid issues with IFS, and you should also use printf, as you can't tell what's in the file, so echo is unsafe (it could pass any argument to echo). Also, there's no need to invoke the external command cat to read a file into a variable when bash can do it itself, in the form $(</var/foo/value.txt).
    – Chris Down
    Sep 21, 2011 at 14:17
  • Yes, all that. :) Sep 21, 2011 at 14:32
1

I'd agree with writing the result to a file, set the variable in the file

curval=the_value

Then in the shell (or another script)

source current_variable_value_result_file
1

You can store your variables inside a simple plain text file.

I used two bash functions for that:

# the cache file in /tmp
CACHE_FILE="/tmp/your-file.txt"
touch "$CACHE_FILE"

# Set a cache/key value in your text file us `=` as separator
function set_cache() {
  echo -e "$1=$2" >> "$CACHE_FILE"
}

# grep the key in the cache file and extract the value using awk
function get_cache() {
  grep -F "$1" "$CACHE_FILE" | awk -F"=" '{print $2}'
}

It's far from perfect and it doesn't work if your key contains the separator value you choose (here =), but it's a good start and it helped me a lot.

I guess a second cron job could remove the cache file periodically.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .