How to iterate through MemoryCache in asp.net core?

前端 未结 2 1200
梦谈多话
梦谈多话 2021-01-18 11:24

There\'s no available method in IMemoryCache that allows to iterate through each cached item. My project is small, I don\'t want to use other options like Redis.

<         


        
2条回答
  •  感情败类
    2021-01-18 12:18

    For large project this is a bit an awkward way. I'd recommend you to use a wrapper class MemoryCacheManager and register it as Singleton. https://gist.github.com/vlapenkov/0a66c40221f9c56d12eb0420fb7cef77

    use

    _manager.Get("key", ()=>"value") // to set a key,
    
    _manager.GetKeys() // get all keys
    
    _manager.Remove("key") //remove one key
    
    _manager.Clear() //remove all keys
    

提交回复
热议问题