Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage caching options: EnableHeadersHashing vs CleanableHashingRegexes #2233

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

Taiizor
Copy link

@Taiizor Taiizor commented Dec 10, 2024

New Feature: EnableHeadersHashing & CleanableHashingRegexes

This pull request introduces several enhancements to the caching mechanism by adding new configuration options and updating related tests. The main changes include the addition of headers hashing, cleanable hashing regexes, and their integration into the existing caching logic.

Hashing Behavior Configuration

  • If EnableContentHashing is set to False and EnableHeadersHashing is set to False:

    • The output is written to Redis and remains consistent until its expiration time, even if the parameters change.
    • Hint: CleanableHashingRegexes is it doesnt make a difference.
  • If EnableContentHashing is set to True and EnableHeadersHashing is set to False:

    • The output is written to Redis, but no data is ever retrieved from Redis.
    • Hint: CleanableHashingRegexes is not using. If used it will work.
  • If EnableContentHashing is set to True and EnableHeadersHashing is set to True:

    • The output is written to Redis and remains consistent until its expiration time.
    • Hint: CleanableHashingRegexes is using. If its not used it will not work.

Example Usings

"FileCacheOptions": {
	"TtlSeconds": 600,
	"Region": "versatile",
	"EnableContentHashing": true,
	"EnableHeadersHashing": true,
	"Header": "AL-Caching-Control",
	"CleanableHashingRegexes": [
		"cf-ray=[a-f0-9]{16}-[A-Z]{1,3}", //CloudFlare CF-RAY
		"--[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}--|(--[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})" //GUID of Parameters
	]
}
Always Caching [EnableContentHashing False - EnableHeadersHashing False]
2024-12-05_12-09-34.False-False.mp4
Nothing Caching [EnableContentHashing True - EnableHeadersHashing False]
2024-12-05_12-11-11.True-False.mp4
Working Caching [EnableContentHashing True - EnableHeadersHashing True]
2024-12-05_12-13-13.True-True.mp4

@Taiizor
Copy link
Author

Taiizor commented Dec 10, 2024

Let me briefly explain the changes:

When EnableHeadersHashing is enabled, it reads the request headers and adds them to the cache key. However, the parameters in the request headers include unique GUIDs every time. A different example of this issue is when a proxy like CloudFlare is used. These setups can also add their own header parameters with each request. To resolve such situations, CleanableHashingRegexes can be used as an additional solution.

Perhaps, on the Ocelot side, a default regex pattern could be added to CleanableHashingRegexes to remove parameter GUIDs. Similarly, the same solution could include a default regex pattern for CloudFlare.

Regex pattern for solving the parameter GUID issue: --[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}--|(--[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})
Regex pattern for solving the CloudFlare issue: cf-ray=[a-f0-9]{16}-[A-Z]{1,3}

public CacheOptions(int? ttlSeconds, string region, string header, bool? enableContentHashing, bool? enableHeadersHashing, List<string>? cleanableHashingRegexes)
{
	TtlSeconds = ttlSeconds ?? 0;
	Region = region;
	Header = header;
	EnableContentHashing = enableContentHashing ?? false;
	EnableHeadersHashing = enableHeadersHashing ?? false;
	CleanableHashingRegexes = cleanableHashingRegexes ?? new()
	{
		"cf-ray=[a-f0-9]{16}-[A-Z]{1,3}" //CloudFlare CF-RAY
		"--[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}--|(--[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12})" //GUID of Parameters
	};
}

@raman-m
Copy link
Member

raman-m commented Dec 11, 2024

What's your real full name?
Please stop using any AI-powered code generation and text generation tools ❗
I would prefer to talk to a real human who speaks English.

P.S.

Thank you for creation the PR without fake changes. The PR has no EOL issues now.

@raman-m raman-m added proposal Proposal for a new functionality in Ocelot Caching Ocelot feature: Caching Configuration Ocelot feature: Configuration labels Dec 11, 2024
Copy link
Member

@raman-m raman-m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my brief opinion, you are suggesting a new feature for caching. On the other hand, our team views the CacheManager as an outdated Ocelot extension package, and we plan to discontinue support by archiving the project soon:

However, it appears your pull request pertains only to the core Caching feature, which is positive.

$\color{red}{No\ Acceptance\ and\ Unit\ tests!}$

Please include tests. See both Ocelot.AcceptanceTests and Ocelot.UnitTests projects.
Please note that updating old tests is must-have, you did it, but our Dev Process requires to write new tests for the new feature!

$\color{orange}{Documentation\ Update\ Required!}$

The sources for our documentation can be found here: docs
In particular, you must update the feature document: caching.rst

P.S. It is essential to be familiar with our Development Process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Caching Ocelot feature: Caching Configuration Ocelot feature: Configuration proposal Proposal for a new functionality in Ocelot
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants