-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
#2246 Review Core cache and redesign Regex
caching
#2251
Conversation
Replace returning type Response<UrlMatch> with UrlMatch, but it should be simple bool actually
With 1min TTL by default
@donnytian Welcome to review! |
} | ||
} | ||
|
||
public bool TryGetValue(string key, string region, out T value) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just asking, why should we have region as parameter since we don't use it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regions are used in the Administration API to delete cached objects within specific region:
Ocelot/docs/features/administration.rst
Lines 139 to 145 in d1ecfaf
DELETE {adminPath}/outputcache/{region} | |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
This clears a region of the cache. If you are using a backplane, it will clear all instances of the cache! | |
Giving your the ability to run a cluster of Ocelots and cache over all of them in memory and clear them all at the same time, so just use a distributed cache. | |
The region is whatever you set against the **Region** field in the `FileCacheOptions <https://github.com/search?q=repo%3AThreeMammals%2FOcelot%20FileCacheOptions&type=code>`_ section of the Ocelot configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regions have been defined in the Caching Configuration since the early releases:
Ocelot/docs/features/caching.rst
Lines 41 to 47 in d1ecfaf
"Region": "europe-central", | |
"Header": "OC-Caching-Control", | |
"EnableContentHashing": false // my route has GET verb only, assigning 'true' for requests with body: POST, PUT etc. | |
} | |
In this example **TtlSeconds** is set to 15 which means the cache will expire after 15 seconds. | |
The **Region** represents a region of caching. |
Target: Emptying cache region by Administration API or even by custom C# code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, the region name should be the route name, but we propose to assign a custom name. Consequently, the DefaultMemoryCache
service could be redesigned and enhanced.
@raman-m ok, fine |
Fixes #2246
Regex
caching logic #2246Proposed Changes
Added new useful method to the
IOcelotCache<T>
interface:bool TryGetValue(string, string, out T)
.As a result, the
Ocelot.Cache.CacheManager
package must be released❗Refactored the
DefaultMemoryCache<T>
class to use concurrent collections for regions, as the previous ones were not thread-safe.Proposed implementing a real cache in the
UpstreamTemplatePatternCreator
class to initialize thePattern
property of the class with a cachedRegex
object when theUpstreamPathTemplate
is being created.Note that the design with a static concurrent dictionary was acceptable due to the constant number of routes (and
Regex
objects) in the collection. However, for dynamic routing scenarios, it is better to switch to a real caching design for future use.❗Resolved memory leak in the
DownstreamUrlCreatorMiddleware
class by refactoring theRemoveQueryStringParametersThatHaveBeenUsedInTemplate
method, where allRegex
objects were saved into_regex
, an internal static collection❗ This faulty design was unfortunately released in version 23.4.0 as PR Best practices for regular expressions versusRegex
performance review #1348❗ We must learn from this incident because sometimes optimizations for CPU may adversely affect memory consumption 🙈Ultimately, the algorithm was refactored to remove
Regex
in favor ofStringBuilder
.Refactored the
IUrlPathToUrlTemplateMatcher
interface, theUrlMatch
class, and the associated logic.Added load test to reproduce the problem with a memory leak. It helped to develop the fix. It should not run in the CI/CD environment because it consumes high CPU resources; thus, the test is skipped.