-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp_cache.sh
More file actions
61 lines (53 loc) · 1.34 KB
/
Copy pathwp_cache.sh
File metadata and controls
61 lines (53 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Declare the cache array in the global scope
declare -A CACHE
function make_cache() {
local base=$1
verbose 10 "function: $FUNCNAME"
make_cache_file_name "$base"
load_cache "$base"
}
function make_cache_file_name() {
CACHEFILE=".$1.wp_cache"
}
function get_cache_item() {
key=$1
if [[ ${CACHE[$key]+_} ]]
then
echo "${CACHE[$key]}"
else
echo ""
fi
}
function update_cache() {
KEY="$1"
VALUE="$2"
CACHE["$KEY"]=$VALUE
echo "" > "${CACHEFILE}"
# write one key-value pair per line separated by tabs.
for i in "${!CACHE[@]}"
do
echo -e "$i\t${CACHE[$i]}" >> "${CACHEFILE}"
done
}
function load_cache() {
verbose 10 "function: $FUNCNAME"
if [ -e "${CACHEFILE}" ]
then
verbose 5 "Load cache from $CACHEFILE"
local file=$(cat "$CACHEFILE")
# For reasons unknown the usual ways of saving and retrieving
# associative arrays (declare -p etc.) don't seem to work for
# me.
while IFS=$'\t' read -r key value
do
# Check that the key is not empty, this copes with cache
# files created by faulty programs.
if [[ "${key}" != "" ]]
then
CACHE[${key}]=${value}
fi
done < "$CACHEFILE"
else
CACHE="( )"
fi
}