Why Sitecore Cache Sizes Are Listed in KB, MB, GB In Configuration
Sitecore certainly has its quirks - every product does - but one oddity that has always stood out to me is how Sitecore defines cache sizes in configuration.
<setting name="Caching.DefaultClientDataCacheSize" value="25MB"/> <setting name="Caching.DefaultFilteredItemsCacheSize" value="10MB"/> <setting name="Caching.DefaultHtmlCacheSize" value="25MB"/> <setting name="Caching.DefaultItemPathsCacheSize" value="50MB"/> <setting name="Caching.DefaultPathCacheSize" value="500KB"/>
Why did Sitecore go with a human-readable format for the cache sizes? Sitecore configuration is generally meant for developers and administrators to maintain, so using stricter, technical values shouldn't be an issue. Because most .NET file operations operate at the byte-level, it would make sense to define these values in bytes.
However, as a consumer/user of Sitecore, it's super-easy to parse, understand, and modify the configuration as-is. "10MB" is a lot easier to read and understand than "10485760" (bytes).
No matter the reasons, Sitecore obviously interprets these values from strings and converts them into bytes (internally stored as long data types). Sitecore provides a string utility method that does this heavy lifting, so any of the following are valid sizes:
- 12000 (bytes)
- 500KB (kilobytes)
- 25MB (megabytes)
- 2GB (gigabytes)
Wait, gigabytes?! As of 2018, no default Sitecore configuration actually uses caches measured in gigabytes - but the option to go that high is there. I discovered the supported "GB" size by cracking open Sitecore.Kernel.dll in dotPeek and studying the Sitecore.StringUtil.ParseSizeString(string) method. Feel free to review the source yourself, but the algorithm (in short) follows:
- Sanitize periods (".") and spaces (" ") from the string value.
If the value ends with "B", determine if it's "KB", "MB", or "GB" and assign a scale multiplier as needed:
- KB = 1024
- MB = 1048576
- GB = 1073741824
- Parse the numberic value from the string
- Return value in bytes = Parsed numberic value * scale multiplier
Because ParseSizeString() is a public method and lives in Sitecore.Kernel.dll, it's available for your own code as well - take advantage of it and keep your own configuration files clean and easy to read for future developers and consumers.
Do you have questions or comments about this post? Find me on Twitter: @SitecoreSpark