Created
September 9, 2020 08:36
-
-
Save ArisAgnew/35068c48756cbeb0e70cdc881e86d848 to your computer and use it in GitHub Desktop.
MemoryCache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Get instance of cache | |
| using System.Runtime.Caching; | |
| var cache = MemoryCache.Default; | |
| //Check if cache contains an item with | |
| cache.Contains("CacheKey"); | |
| //get item from cache | |
| var item = cache.Get("CacheKey"); | |
| //get item from cache or add item if not existing | |
| object list = MemoryCache.Default.AddOrGetExisting("CacheKey", "object to be stored", DateTime.Now.AddHours(12)); | |
| //note if item not existing the item is added by this method | |
| //but the method returns null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment