castleproject / Core

Castle Core, including Castle DynamicProxy, Logging Services and DictionaryAdapter
http://www.castleproject.org/
Other
2.2k stars 467 forks source link

Calling Async method in Intercept #640

Closed arslanaybars closed 1 year ago

arslanaybars commented 1 year ago

I developed a package that helps us with aspect-oriented caching which name is AybCache I used castle core for the intercepting.

This package basically check data from the cache if it exists return from the cache, if it is not exist run the method and write the result to the cache using IDistributedCache

But I need to check data with sync way because I could not call the async method with await in void intercept method. which is below var data = _distributedCache.Get(cacheKey); click to see in repository

But under high load, my Redis synctimeout could not be enough and need to increase synctimeout which I don't want.

Is there any way to call _distributedCache.GetAsync(cacheKey) method instead of _distributedCache.Get(cacheKey); in the intercept method

Pull requests are welcome.

jonorossi commented 1 year ago

@arslanaybars Have you read our docs on async? https://github.com/castleproject/Core/blob/master/docs/dynamicproxy-async-interception.md

You can definitely call GetAsync in Intercept, you just can't await it. You'll have to set up your own continuation, which can be done using the await C# keyword using one of the library listed at the bottom of the docs.