Skip to content

Commit 576a8cb

Browse files
Merge pull request #898 from michelebastione/issue-895-maintenance
Changing the location where the shared strings cache is stored
2 parents 19f4221 + cd77031 commit 576a8cb

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

src/MiniExcel/OpenXml/ExcelOpenXmlSheetReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ private void SetSharedStrings()
449449
var idx = 0;
450450
if (_config.EnableSharedStringCache && sharedStringsEntry.Length >= _config.SharedStringCacheSize)
451451
{
452-
_sharedStrings = new SharedStringsDiskCache();
452+
_sharedStrings = new SharedStringsDiskCache(_config.SharedStringCachePath);
453453
foreach (var sharedString in XmlReaderHelper.GetSharedStrings(stream, _ns))
454454
_sharedStrings[idx++] = sharedString;
455455
}

src/MiniExcel/OpenXml/OpenXmlConfiguration.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.IO;
12
using MiniExcelLibs.Attributes;
23

34
namespace MiniExcelLibs.OpenXml
@@ -18,6 +19,13 @@ public class OpenXmlConfiguration : Configuration
1819
public bool IgnoreEmptyRows { get; set; } = false;
1920
public bool EnableSharedStringCache { get; set; } = true;
2021
public long SharedStringCacheSize { get; set; } = 5 * 1024 * 1024;
22+
23+
/// <summary>
24+
/// The directory where the shared strings cache files are stored.
25+
/// It defaults to the system's temporary folder.
26+
/// </summary>
27+
public string SharedStringCachePath { get; set; } = Path.GetTempPath();
28+
2129
public OpenXmlStyleOptions StyleOptions { get; set; } = new OpenXmlStyleOptions();
2230
public DynamicExcelSheet[] DynamicSheets { get; set; }
2331
public bool EnableWriteFilePath{ get; set; } = true;

src/MiniExcel/OpenXml/SharedStringsDiskCache.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@ public bool ContainsKey(int key)
2323
return key <= _maxIndx;
2424
}
2525

26-
public SharedStringsDiskCache()
26+
public SharedStringsDiskCache(string sharedStringsCacheDir)
2727
{
28-
var path = $"{Guid.NewGuid().ToString()}_miniexcelcache";
29-
_positionFs = new FileStream($"{path}_position", FileMode.OpenOrCreate);
30-
_lengthFs = new FileStream($"{path}_length", FileMode.OpenOrCreate);
31-
_valueFs = new FileStream($"{path}_data", FileMode.OpenOrCreate);
28+
if (string.IsNullOrWhiteSpace(sharedStringsCacheDir) || !Directory.Exists(sharedStringsCacheDir))
29+
throw new DirectoryNotFoundException($"\"{sharedStringsCacheDir}\" is not a valid directory for the shared strings cache.");
30+
31+
var prefix = $"{Path.GetRandomFileName()}_miniexcel";
32+
_positionFs = new FileStream(Path.Combine(sharedStringsCacheDir, $"{prefix}_position"), FileMode.OpenOrCreate);
33+
_lengthFs = new FileStream(Path.Combine(sharedStringsCacheDir, $"{prefix}_length"), FileMode.OpenOrCreate);
34+
_valueFs = new FileStream(Path.Combine(sharedStringsCacheDir, $"{prefix}_data"), FileMode.OpenOrCreate);
3235
}
3336

3437
// index must start with 0-N
@@ -140,13 +143,13 @@ public bool Remove(KeyValuePair<int, string> item)
140143

141144
public IEnumerator<KeyValuePair<int, string>> GetEnumerator()
142145
{
143-
for (int i = 0; i < _maxIndx; i++)
146+
for (int i = 0; i <= _maxIndx; i++)
144147
yield return new KeyValuePair<int, string>(i, this[i]);
145148
}
146149

147150
IEnumerator IEnumerable.GetEnumerator()
148151
{
149-
for (int i = 0; i < _maxIndx; i++)
152+
for (int i = 0; i <= _maxIndx; i++)
150153
yield return this[i];
151154
}
152155

tests/MiniExcelTests/MiniExcelIssueTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ public void TestIssue360()
413413
public void TestIssue117()
414414
{
415415
{
416-
var cache = new SharedStringsDiskCache();
416+
var cache = new SharedStringsDiskCache(Path.GetTempPath());
417417
for (int i = 0; i < 100; i++)
418418
{
419419
cache[i] = i.ToString();
@@ -425,7 +425,7 @@ public void TestIssue117()
425425
Assert.Equal(100, cache.Count);
426426
}
427427
{
428-
var cache = new SharedStringsDiskCache();
428+
var cache = new SharedStringsDiskCache(Path.GetTempPath());
429429
Assert.Empty(cache);
430430
}
431431
}

0 commit comments

Comments
 (0)