GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - dimsf/MemCacheAutoReload: Extension methods for ASP.NET Core MemoryCache, to support automatic, thread-safe lazy initialization of cache entries.

BranchesTags
Open more actions menu

Latest commit

 

History

17 Commits
17 Commits

Folders and files

NameName
Last commit message
Last commit date
MemCacheAutoReload
MemCacheAutoReload
 
 
Tests
Tests
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
MemCacheAutoReload.sln
MemCacheAutoReload.sln
 
 
README.md
README.md
 
 

Repository files navigation

MemCacheAutoReload

Extension methods for ASP.NET Core MemoryCache, to support automatic, thread-safe lazy initialization of cache entries.

Installation

Install-Package MemCacheAutoReload -Version 1.2.0

Usage

Add or get entry(async version)

Task<T> GetOrAddAutoReloadAsync<T>(string key, Func<Task<T>> valueProvider, TimeSpan refreshInterval)

Example

IMemoryCache memCache = ...;
var result = await memCache.GetOrAddAutoReloadAsync("key", async() => 
{
    await Task.Delay(2000); //Simulate some delay
    return Environment.TickCount; //Return result
}, TimeSpan.FromSeconds(2)); //Refresh(call value provider every two seconds)

Add or get entry(sync version)

T GetOrAddAutoReloadAsync<T>(string key, Action<T> valueProvider, TimeSpan refreshInterval)

Example

IMemoryCache memCache = ...;
var result = memCache.GetOrAddAutoReloadAsync("key", () => 
{
        Thread.Sleep(2000); //Simulate some delay
        return Environment.TickCount; //Return result
}, TimeSpan.FromSeconds(2)); //Refresh(call value provider every two seconds)

The GetOrAddAutoReaload/GetOrAddAutoReloadAsync are thread safe. The value generated is cached, and only one thread at a time executes the value provider. The cached value is returned in case a thread executes the value provider to refresh the current value, so there is no blocking except the first time, where no cached value if available.

Set value

memCache.SetAutoReload("testKey", () = > "a test value", TimeSpan.FromSeconds(2));

Remove value

memCache.RemoveAutoReload("testKey");

About

Extension methods for ASP.NET Core MemoryCache, to support automatic, thread-safe lazy initialization of cache entries.

Topics

asp-net-corememcache

Resources

Readme
MIT license
Activity

Stars

0 stars

Watchers

1 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.