# Performance and Caching

## Overview

The Performance & Caching feature is designed to improve the responsiveness of the control by caching the results of number-to-word conversions. Developers can enable caching using the `EnableCaching` property and control the cache size with the `MaxCacheSize` property. This approach minimizes redundant computations for repeated conversions, thereby enhancing overall application performance.

***

### Key Points

<table><thead><tr><th width="241">Key Point</th><th>Description</th></tr></thead><tbody><tr><td>Caching toggle</td><td>The <code>EnableCaching</code> property allows developers to switch caching on or off based on their application's performance needs.</td></tr><tr><td>Configurable cache size</td><td>The <code>MaxCacheSize</code> property defines the maximum number of cached conversions, helping to manage memory usage efficiently.</td></tr><tr><td>Performance optimization</td><td>Caching reduces redundant computations for repeated number conversions, leading to a faster and more responsive UI experience.</td></tr><tr><td>Automatic cache clearing</td><td>Disabling caching automatically clears the current cache, ensuring outdated or irrelevant data is removed.</td></tr></tbody></table>

***

### Best Practices

<table><thead><tr><th width="356">Best Practice</th><th>Description</th></tr></thead><tbody><tr><td>Enable caching for frequently used values</td><td>Activate <code>EnableCaching</code> when conversions are expected to be repeated, such as in data-heavy applications or real-time UI updates.</td></tr><tr><td>Set an appropriate cache size</td><td>Configure <code>MaxCacheSize</code> based on expected application usage to balance performance benefits with memory consumption.</td></tr><tr><td>Monitor cache performance</td><td>Regularly review the cache behavior, especially in high-load scenarios, to adjust settings for optimal performance.</td></tr><tr><td>Clear cache when necessary</td><td>If the underlying conversion logic or formatting settings change, consider disabling caching temporarily to force recalculation.</td></tr></tbody></table>

***

### Common Pitfalls

<table><thead><tr><th width="361">Pitfall</th><th>Description</th></tr></thead><tbody><tr><td>Over-reliance on caching</td><td>Depending too heavily on caching without monitoring can lead to stale data if underlying conversion logic changes.</td></tr><tr><td>Setting cache size too low or too high</td><td>An improperly sized cache may either lead to frequent cache misses or excessive memory consumption.</td></tr><tr><td>Disabling cache without proper clearance</td><td>Failing to clear the cache when disabling caching can result in memory leaks or unexpected behavior.</td></tr><tr><td>Not testing cache performance under load</td><td>Without stress testing, the caching mechanism may not perform as expected under heavy usage scenarios.</td></tr></tbody></table>

***

### Usage Scenarios

<table><thead><tr><th width="361">Scenario</th><th>Description</th></tr></thead><tbody><tr><td>Real-time conversion in data entry screens</td><td>Caching improves performance when users input numbers rapidly, as repeated conversions are retrieved from the cache.</td></tr><tr><td>Batch processing in background tasks</td><td>During bulk conversions, caching prevents redundant computations for recurring numbers, saving processing time.</td></tr><tr><td>Applications with static or rarely changing numbers</td><td>When numbers change infrequently, caching minimizes the need for recalculation, improving overall system responsiveness.</td></tr></tbody></table>

***

### Real Life Usage Scenarios

<table><thead><tr><th width="219">Scenario</th><th>Description</th></tr></thead><tbody><tr><td>Financial reporting</td><td>Repeated conversion of common figures (e.g., recurring monetary amounts) is optimized using caching to accelerate report generation.</td></tr><tr><td>Inventory management</td><td>In systems that display large lists of items with repeated numeric values, caching speeds up the display and lookup processes.</td></tr><tr><td>Educational tools</td><td>Applications that convert a range of numbers for instructional purposes benefit from caching by reducing conversion delays.</td></tr></tbody></table>

***

### Troubleshooting Tips

| Issue                                          | Possible Cause                                                                                               | Recommended Action                                                                                               |
| ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------- |
| Cache not updating after configuration changes | The cache may still hold outdated conversion results even after changing properties like formatting options. | Disable caching temporarily or clear the cache to force recalculation with updated settings.                     |
| Unexpected memory consumption                  | The cache size might be set too high, causing unnecessary memory usage.                                      | Adjust the `MaxCacheSize` property to an optimal value based on the application's performance needs.             |
| Repeated cache misses                          | Caching might be disabled or not properly enabled in high-frequency conversion scenarios.                    | Verify that `EnableCaching` is true and that cache size is sufficient to store frequently requested conversions. |

***

### Code Examples

#### Example 1: Enabling Caching and Setting Cache Size

```csharp
using SiticoneNetFrameworkUI;

// Create an instance of the control
var humanizer = new SiticoneHumanizerLong();

// Enable caching to optimize performance
humanizer.EnableCaching = true;

// Set the maximum cache size to 500 entries
humanizer.MaxCacheSize = 500;

// Set a numeric value to convert
humanizer.Value = 12345;

// Retrieve and display the converted words
Console.WriteLine("Converted Words: " + humanizer.Words);
```

#### Example 2: Disabling Caching to Force Recalculation

```csharp
using SiticoneNetFrameworkUI;

// Create an instance of the control
var humanizer = new SiticoneHumanizerLong();

// Enable caching initially
humanizer.EnableCaching = true;
humanizer.Value = 9876;
Console.WriteLine("Converted Words with caching: " + humanizer.Words);

// Disable caching (this clears the cache) to force fresh conversion
humanizer.EnableCaching = false;
humanizer.Value = 9876;
Console.WriteLine("Converted Words without caching: " + humanizer.Words);
```

#### Example 3: Performance Testing with High Frequency Conversions

```csharp
using System;
using System.Diagnostics;
using SiticoneNetFrameworkUI;

public class PerformanceTest
{
    public static void Main()
    {
        var humanizer = new SiticoneHumanizerLong
        {
            EnableCaching = true,
            MaxCacheSize = 1000
        };

        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();

        // Simulate high-frequency conversions
        for (int i = 0; i < 10000; i++)
        {
            humanizer.Value = i;
            // Optionally retrieve humanizer.Words to simulate UI binding or further processing
            var words = humanizer.Words;
        }

        stopwatch.Stop();
        Console.WriteLine($"High frequency conversion completed in {stopwatch.ElapsedMilliseconds} ms");
    }
}
```

***

### Review

<table><thead><tr><th width="244">Aspect</th><th>Review Comments</th></tr></thead><tbody><tr><td>Performance improvement</td><td>Caching significantly reduces processing time for repeated conversions, making the control more responsive.</td></tr><tr><td>Flexibility</td><td>With configurable caching options, developers can balance performance gains against memory usage based on application needs.</td></tr><tr><td>Ease of integration</td><td>The caching mechanism is seamlessly integrated with the core conversion logic and requires minimal configuration.</td></tr></tbody></table>

***

### Summary

<table><thead><tr><th width="304">Summary Statement</th><th>Description</th></tr></thead><tbody><tr><td>Performance &#x26; Caching optimizes conversion operations by storing repeated results</td><td>This feature enhances the responsiveness of the control by caching conversion results, allowing for faster repeated conversions and reduced computational overhead.</td></tr></tbody></table>

***

### Additional Useful Sections

#### Integration Checklist

<table><thead><tr><th width="344">Checklist Item</th><th>Status/Notes</th></tr></thead><tbody><tr><td>Enable caching</td><td>Set <code>EnableCaching</code> to true if repeated conversions are expected.</td></tr><tr><td>Configure cache size</td><td>Set <code>MaxCacheSize</code> to an optimal value based on expected application load and available memory.</td></tr><tr><td>Monitor cache performance</td><td>Regularly review the performance impact and memory usage of caching under real-world conditions.</td></tr><tr><td>Clear cache after configuration changes</td><td>Disable caching temporarily or clear the cache when altering formatting or conversion settings to avoid stale data.</td></tr></tbody></table>

#### FAQ

| Question                                                      | Answer                                                                                                                                         |
| ------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| How do I enable caching for the conversion results?           | Set the `EnableCaching` property to true to activate caching of conversion results.                                                            |
| What is the purpose of the MaxCacheSize property?             | The `MaxCacheSize` property determines the maximum number of entries that can be stored in the cache to manage memory usage.                   |
| Can I change the cache settings on the fly?                   | Yes, adjusting `EnableCaching` and `MaxCacheSize` at runtime will affect caching behavior immediately, including cache clearing when disabled. |
| How can I troubleshoot performance issues related to caching? | Check if caching is enabled, verify that the cache size is configured correctly, and ensure that cached data is not outdated.                  |

***

This comprehensive documentation for the Performance & Caching feature provides detailed guidance for developers to integrate and optimize the control's performance by leveraging caching capabilities, ensuring a smooth and efficient user experience in their .NET WinForms applications.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs-siticoneframework.gitbook.io/home/net-framework-or-net-core-ui/data-formatting-and-display/siticone-humanizerlong/performance-and-caching.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
