Skip to content
This repository was archived by the owner on Jul 11, 2025. It is now read-only.

Commit 028f194

Browse files
Merge pull request #10 from open-AIMS/cache-warmup
Add cache warmup
2 parents 0e26ddb + 175e285 commit 028f194

File tree

4 files changed

+73
-19
lines changed

4 files changed

+73
-19
lines changed

Manifest.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
julia_version = "1.10.5"
44
manifest_format = "2.0"
5-
project_hash = "d0ffae0ac62d29f3001ef8e43624c0daaa93de19"
5+
project_hash = "23aa2c97b270f292dd6c68842031110f00d039d9"
66

77
[[deps.AbstractFFTs]]
88
deps = ["LinearAlgebra"]

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Oxygen = "df9a0d86-3283-4920-82dc-4555fc0d1d8b"
3535
Proj = "c94c279d-25a6-4763-9509-64d165bea63e"
3636
ProtoBuf = "3349acd9-ac6a-5e09-bcdb-63829b23a429"
3737
Rasters = "a3a2b9e3-a471-40c9-b274-f788e487c689"
38+
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
3839
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
3940
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
4041
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ These are currently the files/data created in Step/Script 1a in https://github.c
1818
PREPPED_DATA_DIR = "C:/some_path_to_data/MPA/"
1919

2020
[server_config]
21-
CACHE_DIR = "<some location to cache geotiffs>"
21+
TIFF_CACHE_DIR = "<some location to cache geotiffs>"
22+
REGIONAL_CACHE_DIR = "<some location to cache regional datasets>"
2223
DEBUG_MODE = "false" # Optional, disables file caching and displays debug logs
2324
COG_THREADS = "2" # Optional, Number of threads to use when creating COGs (defaults to 1)
2425
TILE_SIZE = "256" # Optional, tile block size to use (defaults to 256)

src/ReefGuideAPI.jl

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ using
55
Glob,
66
TOML
77

8+
using Serialization
9+
810
using DataFrames
911
using OrderedCollections
1012
using Memoization
@@ -37,18 +39,38 @@ function get_regions()
3739
return regions
3840
end
3941

42+
"""
43+
setup_regional_data(config::Dict)
44+
45+
Load regional data to act as an in-memory cache.
46+
47+
# Arguments
48+
- `config` : Configuration settings, typically loaded from a TOML file.
49+
- `reef_data_path` : Path to pre-prepared reef data
50+
51+
# Returns
52+
OrderedDict of `RegionalCriteria` for each region.
53+
"""
4054
function setup_regional_data(config::Dict)
41-
return setup_regional_data(config["prepped_data"]["PREPPED_DATA_DIR"])
42-
end
43-
function setup_regional_data(reef_data_path::String)
4455
if @isdefined(REGIONAL_DATA)
4556
@debug "Using previously generated regional data store."
4657
sleep(1) # Pause so message is noticeably visible
4758
return REGIONAL_DATA
4859
end
4960

61+
# Check disk-based store
62+
reg_cache_dir = config["server_config"]["REGIONAL_CACHE_DIR"]
63+
reg_cache_fn = joinpath(reg_cache_dir, "regional_cache.dat")
64+
if isfile(reg_cache_fn)
65+
@debug "Loading regional data cache from disk"
66+
@eval const REGIONAL_DATA = deserialize($(reg_cache_fn))
67+
return REGIONAL_DATA
68+
end
69+
5070
@debug "Setting up regional data store..."
5171

72+
reef_data_path = config["prepped_data"]["PREPPED_DATA_DIR"]
73+
5274
regional_assessment_data = OrderedDict{String, RegionalCriteria}()
5375
for reg in get_regions()
5476
data_paths = String[]
@@ -74,7 +96,8 @@ function setup_regional_data(reef_data_path::String)
7496
elseif occursin("flat", string(dp))
7597
flat_table = GeoParquet.read(parq_file)
7698
else
77-
error("Unknown lookup found: $(parq_file)")
99+
msg = "Unknown lookup found: $(parq_file). Must be 'slope' or 'flat'"
100+
throw(ArgumentError(msg))
78101
end
79102
end
80103
end
@@ -96,19 +119,28 @@ function setup_regional_data(reef_data_path::String)
96119
)
97120
end
98121

122+
# Store cache on disk to avoid excessive cold startup times
123+
@debug "Saving regional data cache to disk"
124+
serialize(reg_cache_fn, regional_assessment_data)
125+
99126
# Remember, `@eval` runs in global scope.
100127
@eval const REGIONAL_DATA = $(regional_assessment_data)
101128

102129
return REGIONAL_DATA
103130
end
104131

105-
function _cache_location(config)
132+
"""
133+
_cache_location(config::Dict)::String
134+
135+
Retrieve cache location for geotiffs.
136+
"""
137+
function _cache_location(config::Dict)::String
106138
cache_loc = try
107139
in_debug = "DEBUG_MODE" in config["server_config"]
108140
if in_debug && lowercase(config["server_config"]["DEBUG_MODE"]) == "true"
109141
mktempdir()
110142
else
111-
config["server_config"]["CACHE_DIR"]
143+
config["server_config"]["TIFF_CACHE_DIR"]
112144
end
113145
catch
114146
mktempdir()
@@ -117,7 +149,12 @@ function _cache_location(config)
117149
return cache_loc
118150
end
119151

120-
function n_gdal_threads(config)::String
152+
"""
153+
n_gdal_threads(config::Dict)::String
154+
155+
Retrieve the configured number of threads to use when writing COGs with GDAL.
156+
"""
157+
function n_gdal_threads(config::Dict)::String
121158
n_cog_threads = try
122159
config["server_config"]["COG_THREADS"]
123160
catch
@@ -127,7 +164,12 @@ function n_gdal_threads(config)::String
127164
return n_cog_threads
128165
end
129166

130-
function tile_size(config)::Tuple
167+
"""
168+
tile_size(config::Dict)::Tuple
169+
170+
Retrieve the configured size of map tiles in pixels (width and height / lon and lat).
171+
"""
172+
function tile_size(config::Dict)::Tuple
131173
tile_dims = try
132174
res = parse(Int, config["server_config"]["TILE_SIZE"])
133175
(res, res)
@@ -138,23 +180,33 @@ function tile_size(config)::Tuple
138180
return tile_dims
139181
end
140182

183+
"""
184+
warmup_cache(config_path::String)
185+
186+
Invokes warm up of regional data cache to reduce later spin up times.
187+
"""
188+
function warmup_cache(config_path::String)
189+
config = TOML.parsefile(config_path)
190+
setup_regional_data(config)
191+
end
192+
141193
function start_server(config_path)
142-
println("Launching server...please wait")
194+
@info "Launching server... please wait"
143195

144-
println("Parsing configuration from $(config_path)...")
196+
@info "Parsing configuration from $(config_path)..."
145197
config = TOML.parsefile(config_path)
146-
println("Successfully parsed configuration.")
198+
@info "Successfully parsed configuration."
147199

148-
println("Setting up region routes...")
200+
@info "Setting up region routes..."
149201
setup_region_routes(config)
150-
println("Completed region routes setup.")
202+
@info "Completed region routes setup."
151203

152-
println("Setting up tile routes...")
204+
@info "Setting up tile routes..."
153205
setup_tile_routes()
154-
println("Completed tile routes setup.")
206+
@info "Completed tile routes setup."
155207

156-
println("Initialisation complete, starting server on port 8000.")
157-
println("Starting with $(Threads.nthreads()) threads...")
208+
@info "Initialisation complete, starting server on port 8000."
209+
@info "Starting with $(Threads.nthreads()) threads..."
158210
if Threads.nthreads() > 1
159211
serveparallel(host="0.0.0.0", port=8000)
160212
else

0 commit comments

Comments
 (0)