Skip to content

Commit 73f3f38

Browse files
committed
VSI_CACHE_SIZE: Use CPLParseMemorySize
1 parent ef07c72 commit 73f3f38

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

autotest/gcore/vsifile.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,16 @@ def test_vsifile_4():
237237
# Test vsicache
238238

239239

240-
@pytest.mark.parametrize("cache_size", ("0", "65536", None))
241-
def test_vsifile_5(cache_size):
240+
@pytest.mark.parametrize("cache_size", ("0", "64kb", None))
241+
def test_vsifile_5(tmp_path, cache_size):
242242

243-
fp = gdal.VSIFOpenL("tmp/vsifile_5.bin", "wb")
243+
fp = gdal.VSIFOpenL(tmp_path / "vsifile_5.bin", "wb")
244244
ref_data = "".join(["%08X" % i for i in range(5 * 32768)])
245245
gdal.VSIFWriteL(ref_data, 1, len(ref_data), fp)
246246
gdal.VSIFCloseL(fp)
247247

248248
with gdal.config_options({"VSI_CACHE": "YES", "VSI_CACHE_SIZE": cache_size}):
249-
fp = gdal.VSIFOpenL("tmp/vsifile_5.bin", "rb")
249+
fp = gdal.VSIFOpenL(tmp_path / "vsifile_5.bin", "rb")
250250

251251
gdal.VSIFSeekL(fp, 50000, 0)
252252
if gdal.VSIFTellL(fp) != 50000:
@@ -277,8 +277,6 @@ def test_vsifile_5(cache_size):
277277

278278
gdal.VSIFCloseL(fp)
279279

280-
gdal.Unlink("tmp/vsifile_5.bin")
281-
282280

283281
###############################################################################
284282
# Test vsicache an read errors (https://github.com/qgis/QGIS/issues/45293)

doc/source/user/configoptions.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,9 @@ Performance and caching
321321
Set the size of the VSI cache. Be wary of large values for
322322
``VSI_CACHE_SIZE`` when opening VRT datasources containing many source
323323
rasters, as this is a per-file cache.
324+
Since GDAL 3.11, the value of ``VSI_CACHE_SIZE`` may be specified using
325+
memory units (e.g., "25 MB").
326+
324327

325328
Driver management
326329
^^^^^^^^^^^^^^^^^

port/cpl_vsil_cache.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,30 @@ class VSICachedFile final : public VSIVirtualHandle
112112

113113
static size_t GetCacheMax(size_t nCacheSize)
114114
{
115-
return nCacheSize ? nCacheSize
116-
: static_cast<size_t>(std::min(
117-
static_cast<GUIntBig>(
118-
std::numeric_limits<size_t>::max() / 2),
119-
CPLScanUIntBig(CPLGetConfigOption("VSI_CACHE_SIZE",
120-
"25000000"),
121-
40)));
115+
if (nCacheSize)
116+
{
117+
return nCacheSize;
118+
}
119+
120+
const char *pszCacheSize = CPLGetConfigOption("VSI_CACHE_SIZE", "25000000");
121+
GIntBig nMemorySize;
122+
bool bUnitSpecified;
123+
if (CPLParseMemorySize(pszCacheSize, &nMemorySize, &bUnitSpecified) !=
124+
CE_None)
125+
{
126+
CPLError(
127+
CE_Failure, CPLE_IllegalArg,
128+
"Failed to parse value of VSI_CACHE_SIZE. Using default of 25MB");
129+
nMemorySize = 25000000;
130+
}
131+
else if (static_cast<size_t>(nMemorySize) >
132+
std::numeric_limits<size_t>::max() / 2)
133+
{
134+
nMemorySize =
135+
static_cast<GIntBig>(std::numeric_limits<size_t>::max() / 2);
136+
}
137+
138+
return static_cast<size_t>(nMemorySize);
122139
}
123140

124141
/************************************************************************/

0 commit comments

Comments
 (0)