Skip to content

This library is used to create a cached function. The return value of the function is cached with IMemoryCache.

License

Notifications You must be signed in to change notification settings

jxdking/CachedFunc2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CachedFunc2

This library is used to create a cached function. The return value of the function is cached with IMemoryCache.

Register The Service

serviceCollection.AddMemoryCache();
serviceCollection.AddCachedFunc();

Then, you can get CachedFuncSvc object through dependency injection.

How to Use

Assume there is a slow-running function.

static int SlowFunc(int n) {
    Thread.Sleep(1000);
    return n;
}

Create CachedFunc with [CachedFuncSvc] object.

CachedFunc<int, int> cachedFunc = cachedFuncSvc.Create(SlowFunc);

Then, you have a cached function. Call this function in the same way as the orignal function.

int result = cachedFunc(12345);

Options

CachedFuncSvc.Create() accepts MemoryCacheEntryOptions argument. In the backend, it creates a MemoryCache entry with all the arguments at the time the CachedFunc<> is called. On the following calls to the CachedFunc<>, the cached value will be returned as long as the cache is not expired based on the MemoryCacheEntryOptions.

For the entry with MemoryCacheEntryOptions.Property.NeverRemove, the Cache will be eager loaded as soon as the current cache entry is expired.

About

This library is used to create a cached function. The return value of the function is cached with IMemoryCache.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages