Skip to content

Commit da51052

Browse files
committed
Rename configuration properties for for HybridCachePlugin
All properties have been renamed and are now in Megabytes rather than bytes
1 parent 90fd5d5 commit da51052

File tree

8 files changed

+42
-38
lines changed

8 files changed

+42
-38
lines changed

CHANGES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* Delete references to `DiskCache` and `TinyCache` from **both nuget.config and Web.config**
3333
* `Install-Package ImageResizer.Plugins.HybridCache`
3434
* Put `<add name="HybridCache" />` in the `<resizer><plugins>` section of `Web.config`
35-
* Put `<hybridCache cacheLocation="C:\imageresizercache\" cacheMaxSizeBytes="1,000,000,000" />` in the `<resizer>` section of `Web.config`. If you want to use a temp folder, omit cacheLocation.
35+
* Put `<hybridCache cacheLocation="C:\imageresizercache\" cacheSizeMb="1,000" />` in the `<resizer>` section of `Web.config`. If you want to use a temp folder, omit cacheLocation.
3636
* HybridCache requires a cache folder outside of the web root. DiskCache did not support that.
3737
* HybridCache, unlike DiskCache, can precisely limit the cache size & disk utilization.
3838
* HybridCache uses a write-ahead log to prevent orphaned cache entries.

ImageResizer.sln

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
4040
CONTRIBUTING.md = CONTRIBUTING.md
4141
.github\workflows\dotnet.yml = .github\workflows\dotnet.yml
4242
LICENSE.md = LICENSE.md
43-
.github\workflows\outdated-dotnet-sweep.yml = .github\workflows\outdated-dotnet-sweep.yml
4443
nuget\NugetPackages.targets = nuget\NugetPackages.targets
4544
.editorconfig = .editorconfig
4645
tests\api-surface\ImageResizer.Plugins.AzureReader2.txt = tests\api-surface\ImageResizer.Plugins.AzureReader2.txt

plugins/ImageResizer.Plugins.HybridCache/HybridCacheOptions.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ namespace ImageResizer.Plugins.HybridCache
55
public class HybridCacheOptions
66
{
77
/// <summary>
8-
/// Where to store the cached files and the database
8+
/// Where to store the cached files and the database (directory path)
99
/// </summary>
10-
public string DiskCacheDirectory { get; set; }
10+
public string CacheLocation { get; set; }
1111

1212
/// <summary>
1313
/// How many RAM bytes to use when writing asynchronously to disk before we switch to writing synchronously.
1414
/// Defaults to 100MiB.
1515
/// </summary>
16-
public long QueueSizeLimitInBytes { get; set; } = 100 * 1024 * 1024;
16+
public long WriteQueueMemoryMb { get; set; } = 100;
1717

1818
/// <summary>
1919
/// Defaults to 1 GiB. Don't set below 9MB or no files will be cached, since 9MB is reserved just for empty directory
2020
/// entries.
2121
/// </summary>
22-
public long CacheSizeLimitInBytes { get; set; } = 1 * 1024 * 1024 * 1024;
22+
public long CacheSizeMb { get; set; } = 1024;
2323

2424
/// <summary>
2525
/// The minimum number of bytes to free when running a cleanup task. Defaults to 1MiB;
2626
/// </summary>
27-
public long MinCleanupBytes { get; set; } = 1 * 1024 * 1024;
27+
public long EvictionSweepSizeMb { get; set; } = 1;
2828

2929
/// <summary>
3030
/// The minimum age of files to delete. Defaults to 10 seconds.
@@ -40,7 +40,7 @@ public class HybridCacheOptions
4040

4141
public HybridCacheOptions(string cacheDir)
4242
{
43-
DiskCacheDirectory = cacheDir;
43+
CacheLocation = cacheDir;
4444
}
4545
}
4646
}

plugins/ImageResizer.Plugins.HybridCache/HybridCachePlugin.cs

+24-21
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,14 @@ private static string GetLegacyDiskCachePhysicalPath(Config config)
5353

5454
private void LoadSettings(Config c)
5555
{
56-
_cacheOptions.DiskCacheDirectory = c.get("hybridCache.cacheLocation", ResolveCacheLocation(_cacheOptions.DiskCacheDirectory));
57-
_cacheOptions.CacheSizeLimitInBytes = c.get("hybridCache.cacheMaxSizeBytes", _cacheOptions.CacheSizeLimitInBytes);
58-
_cacheOptions.DatabaseShards = c.get("hybridCache.shardCount", _cacheOptions.DatabaseShards);
59-
_cacheOptions.QueueSizeLimitInBytes = c.get("hybridCache.writeQueueLimitBytes", _cacheOptions.QueueSizeLimitInBytes);
60-
_cacheOptions.MinCleanupBytes = c.get("hybridCache.minCleanupBytes", _cacheOptions.MinCleanupBytes);
56+
var cacheSizeMb = c.get("hybridCache.cacheSizeMb", _cacheOptions.CacheSizeMb);
57+
var writeQueueMemoryMb = c.get("hybridCache.writeQueueMemoryMb", _cacheOptions.WriteQueueMemoryMb);
58+
var evictionSweepSizeMb = c.get("hybridCache.evictionSweepSizeMb", _cacheOptions.EvictionSweepSizeMb);
59+
var shardCount = c.get("hybridCache.shardCount", _cacheOptions.DatabaseShards);
6160

61+
_cacheOptions.CacheLocation = c.get("hybridCache.cacheLocation", ResolveCacheLocation(_cacheOptions.CacheLocation));
6262
// Resolve cache directory
63-
_cacheOptions.DiskCacheDirectory = ResolveCacheLocation(_cacheOptions.DiskCacheDirectory);
64-
63+
_cacheOptions.CacheLocation = ResolveCacheLocation(_cacheOptions.CacheLocation);
6564

6665
if (FirstInstancePerPath.AddOrUpdate(DiskCacheDirectory, this, (k, v) => v) != this)
6766
{
@@ -74,7 +73,7 @@ private void LoadSettings(Config c)
7473
}
7574

7675
private string GetDefaultCacheLocation() {
77-
var subfolder = $"imageresizer_cache_{Math.Abs(PathUtils.AppPhysicalPath.GetHashCode())}";
76+
var subfolder = $"imageresizer_cache_{Math.Abs(PathUtils.AppPhysicalPath.GetHashCode()).ToString()}";
7877
return Path.Combine(Path.GetTempPath(), subfolder);
7978
}
8079

@@ -174,11 +173,11 @@ public async Task ProcessAsync(HttpContext context, IAsyncResponsePlan plan)
174173

175174
private static Imazen.HybridCache.HybridCache CreateHybridCacheFromOptions(HybridCacheOptions options, ILogger logger)
176175
{
177-
var cacheOptions = new Imazen.HybridCache.HybridCacheOptions(options.DiskCacheDirectory)
176+
var cacheOptions = new Imazen.HybridCache.HybridCacheOptions(options.CacheLocation)
178177
{
179178
AsyncCacheOptions = new AsyncCacheOptions()
180179
{
181-
MaxQueuedBytes = Math.Max(0, options.QueueSizeLimitInBytes),
180+
MaxQueuedBytes = Math.Max(0, options.WriteQueueMemoryMb),
182181
WriteSynchronouslyWhenQueueFull = true,
183182
MoveFileOverwriteFunc = (from, to) =>
184183
{
@@ -188,12 +187,12 @@ private static Imazen.HybridCache.HybridCache CreateHybridCacheFromOptions(Hybri
188187
},
189188
CleanupManagerOptions = new CleanupManagerOptions()
190189
{
191-
MaxCacheBytes = Math.Max(0, options.CacheSizeLimitInBytes),
192-
MinCleanupBytes = Math.Max(0, options.MinCleanupBytes),
190+
MaxCacheBytes = Math.Max(0, options.CacheSizeMb),
191+
MinCleanupBytes = Math.Max(1, options.EvictionSweepSizeMb),
193192
MinAgeToDelete = options.MinAgeToDelete.Ticks > 0 ? options.MinAgeToDelete : TimeSpan.Zero
194193
}
195194
};
196-
var database = new MetaStore(new MetaStoreOptions(options.DiskCacheDirectory)
195+
var database = new MetaStore(new MetaStoreOptions(options.CacheLocation)
197196
{
198197
Shards = Math.Max(1, options.DatabaseShards),
199198
MaxLogFilesPerShard = 3
@@ -222,7 +221,7 @@ public Task<IStreamCacheResult> GetOrCreateBytes(byte[] key, AsyncBytesResult da
222221
return _cache.GetOrCreateBytes(key, dataProviderCallback, cancellationToken, retrieveContentType);
223222
}
224223

225-
private string DiskCacheDirectory => _cacheOptions?.DiskCacheDirectory;
224+
private string DiskCacheDirectory => _cacheOptions?.CacheLocation;
226225

227226

228227
private bool HasNtfsPermission()
@@ -282,13 +281,17 @@ public IEnumerable<IIssue> GetIssues() {
282281
"Please give user " + GetExecutingUser() + " read and write access to directory \"" + DiskCacheDirectory + "\" to correct the problem. You can access NTFS security settings by right-clicking the aforementioned folder and choosing Properties, then Security.", IssueSeverity.ConfigurationError));
283282

284283
//Warn user about setting hashModifiedDate=false in a web garden.
285-
if (_cacheOptions.MinCleanupBytes < 1000 * 1000)
286-
issues.Add(new Issue("HybridCache", "minCleanupBytes should not be set below 1 megabyte (1,000,000). Found in the <hybridCache /> element in Web.config.",
284+
if (_cacheOptions.EvictionSweepSizeMb < 1)
285+
issues.Add(new Issue("HybridCache", "evictionSweepSizeMb should not be set below 1 MB. Found in the <hybridCache /> element in Web.config.",
287286
"Setting a value too low will waste energy and reduce performance", IssueSeverity.ConfigurationError));
288287

289-
if (_cacheOptions.CacheSizeLimitInBytes < 1000 * 1000 * 100)
290-
issues.Add(new Issue("HybridCache", "cacheMaxSizeBytes should not be set below 100 MiB, 1GiB is the suggested minimum . Found in the <hybridCache /> element in Web.config.",
288+
if (_cacheOptions.CacheSizeMb < 100)
289+
issues.Add(new Issue("HybridCache", "cacheSizeMb should not be set below 100 MiB, 1GB is the suggested minimum . Found in the <hybridCache /> element in Web.config.",
291290
"Setting a value too low will increase latency, increase cache misses, waste energy and reduce server performance.", IssueSeverity.ConfigurationError));
291+
292+
if (_cacheOptions.WriteQueueMemoryMb < 50)
293+
issues.Add(new Issue("HybridCache", "writeQueueMemoryMb should not be set below 50 MiB, 100Mib is the suggested minimum . Found in the <hybridCache /> element in Web.config.",
294+
"Setting a value too low will increase latency by forcing images to be written to disk before HTTP responses are sent.", IssueSeverity.ConfigurationError));
292295

293296
if (conflictsExist)
294297
issues.Add(new Issue("HybridCache", "More than one instance of HybridCache has been created for the same directory, these instances will fight.", IssueSeverity.ConfigurationError));
@@ -304,9 +307,9 @@ public IEnumerable<IIssue> GetIssues() {
304307
public IEnumerable<KeyValuePair<string, string>> GetInfoPairs()
305308
{
306309
var list = new List<KeyValuePair<string, string>>();
307-
list.Add(new KeyValuePair<string, string>("hybridCache_" + "cacheMaxSizeBytes", _cacheOptions.CacheSizeLimitInBytes.ToString()));
308-
list.Add(new KeyValuePair<string, string>("hybridCache_" + "writeQueueLimitBytes", _cacheOptions.QueueSizeLimitInBytes.ToString()));
309-
list.Add(new KeyValuePair<string, string>("hybridCache_" + "minCleanupBytes", _cacheOptions.MinCleanupBytes.ToString()));
310+
list.Add(new KeyValuePair<string, string>("hybridCache_" + "cacheSizeMb", _cacheOptions.CacheSizeMb.ToString()));
311+
list.Add(new KeyValuePair<string, string>("hybridCache_" + "writeQueueMemoryMb", _cacheOptions.WriteQueueMemoryMb.ToString()));
312+
list.Add(new KeyValuePair<string, string>("hybridCache_" + "evictionSweepSizeMb", _cacheOptions.EvictionSweepSizeMb.ToString()));
310313
list.Add(new KeyValuePair<string, string>("hybridCache_" + "shardCount", _cacheOptions.DatabaseShards.ToString()));
311314
list.Add(new KeyValuePair<string, string>("hybridCache_network_drive", CacheDriveOnNetwork() ? "1" : "0"));
312315
list.Add(new KeyValuePair<string, string>("hybridCache_filesystem", GetCacheDrive()?.DriveFormat ?? ""));

plugins/ImageResizer.Plugins.HybridCache/README_HYBRIDCACHE.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,25 @@ This plugin only works with the URL API, not the managed API.
1515
1. ` PM> Install-Package ImageResizer.Plugins.HybridCache `
1616
2. In the `<resizer><plugins>` section of `Web.config`, insert `<add name="HybridCache" />`.
1717
3. (optional) In the `<resizer>` section of Web.config, insert <br />
18-
`<hybridCache cacheLocation="C:\imageresizercache\" cacheMaxSizeBytes="1,000,000,000" />`.
18+
`<hybridCache cacheLocation="C:\imageresizercache\" cacheSizeMb="1,000" />`.
1919

2020

2121

2222
## Notes
2323

2424
* `<hybridCache cacheLocation="C:\imageresizercache\"/>` defaults to a app-unique subfolder of the IIS user account's temp folder. Cannot be located in the project or a web-accessible folder.
25-
* `<hybridCache cacheMaxSizeBytes=""1,000,000,000" />` is in bytes and cannot be set below 9MB (9,000,000) or no files will be cached. 1GiB is the suggested minimum.
25+
* `<hybridCache cacheSizeMb="1,000" />` is in bytes and cannot be set below 9MB (9,000,000) or no files will be cached. 1GiB is the suggested minimum.
2626
* `<hybridCache databaseShards="8" />` adjust the number of shards (and write ahead log groups) in the database. Delete the cache folder after changing this number. Don't change this number unless directed by support.
27-
* `<hybridCache queueSizeLimitInBytes="100,000,000" />` limits how much RAM can be used by the asynchronous write queue before making requests wait for caching writing to complete. (HybridCache writes cache entries in the background to improve latency). 100MB is the default and suggested minimum.
28-
* `<hybridCache.minCleanupBytes="1,000,000" />` determines the minimum amount of bytes to evict from the cache once a cleanup is triggered. 1MB is the default and suggested minimum.
27+
* `<hybridCache writeQueueMemoryMb="100" />` limits how much RAM can be used by the asynchronous write queue before making requests wait for caching writing to complete. (HybridCache writes cache entries in the background to improve latency). 100MB is the default and suggested minimum.
28+
* `<hybridCache.evictionSweepSizeMb="1" />` determines the minimum amount of bytes to evict from the cache once a cleanup is triggered. 1MB is the default and suggested minimum.
2929

3030
## Migrating from DiskCache or TinyCache
3131

3232
* **Delete your `/imagecache/` folder (otherwise it will become publicly accessible!!!)** (Actually, if installed, HybridCache will kill the application with an error message to prevent that - for all we know you resize images of passwords and have directory listing enabled)
3333
* Delete references to `DiskCache` and `TinyCache` from **both nuget.config and Web.config**
3434
* `Install-Package ImageResizer.Plugins.HybridCache`
3535
* Put `<add name="HybridCache" />` in the `<resizer><plugins>` section of `Web.config`
36-
* Put `<hybridCache cacheLocation="C:\imageresizercache\" cacheMaxSizeBytes="1,000,000,000" />` in the `<resizer>` section of `Web.config`. If you want to use a temp folder, omit cacheLocation.
36+
* Put `<hybridCache cacheLocation="C:\imageresizercache\" cacheSizeMb="1,000" />` in the `<resizer>` section of `Web.config`. If you want to use a temp folder, omit cacheLocation.
3737
* HybridCache requires a cache folder outside of the web root. DiskCache did not support that.
3838
* HybridCache, unlike DiskCache, can precisely limit the cache size & disk utilization.
3939
* HybridCache uses a write-ahead log to prevent orphaned cache entries.

tests/ImageResizer.AllPlugins.Tests/TestAll.cs

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ private static Dictionary<string, string[]> GetData()
114114
public static List<object> GetSourceObjects()
115115
{
116116
var sources = new List<object>();
117+
//TODO: This breaks!
117118
sources.Add(@"C:\Users\lilith\work\resizer\examples\images\red-leaf.jpg");
118119
//sources.Add("~/images/red-leaf.jpg");
119120
// sources.Add("/gradient.png");

tests/api-surface/ImageResizer.Plugins.HybridCache.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ namespace ImageResizer.Plugins.HybridCache
77
public class HybridCacheOptions
88
{
99
public HybridCacheOptions(string cacheDir) { }
10-
public long CacheSizeLimitInBytes { get; set; }
10+
public string CacheLocation { get; set; }
11+
public long CacheSizeMb { get; set; }
1112
public int DatabaseShards { get; set; }
12-
public string DiskCacheDirectory { get; set; }
13+
public long EvictionSweepSizeMb { get; set; }
1314
public System.TimeSpan MinAgeToDelete { get; set; }
14-
public long MinCleanupBytes { get; set; }
15-
public long QueueSizeLimitInBytes { get; set; }
15+
public long WriteQueueMemoryMb { get; set; }
1616
}
1717
public class HybridCachePlugin : ImageResizer.Plugins.IAsyncTyrantCache, ImageResizer.Plugins.IPlugin, ImageResizer.Plugins.IPluginInfo, ImageResizer.Plugins.IPluginRequiresShutdown
1818
{

tests/api-surface/ImageResizer.txt

+1
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ namespace ImageResizer.Configuration
622622
public void WriteDiagnosticsTo(string path) { }
623623
public bool get(string selector, bool defaultValue) { }
624624
public int get(string selector, int defaultValue) { }
625+
public long get(string selector, long defaultValue) { }
625626
public string get(string selector, string defaultValue) { }
626627
public T get<T>(string selector, T defaultValue)
627628
where T : struct, System.IConvertible { }

0 commit comments

Comments
 (0)