C# MemoryCache 帮助类

 注意需引用System.Runtime.Caching.dll

 public static class CacheManagerHelper
 {
        private static object _lockObj = new object();
        private readonly static MemoryCache _cache;
        static CacheManager()
        {
            _cache = MemoryCache.Default;
        }
        public static void Remove(string key)
        {
            _cache.Remove(key);
        }
        public static T Get<T>(string key)
        {
            return (T)_cache.Get(key);
        }
        public static void Set(CacheItem cacheItem, CacheItemPolicy policy)
        {
            _cache.AddOrGetExisting(cacheItem, policy);
        }
        public static void Set<T>(string key, T item, int minutes = 5)
        {
            if (item == null)
                return;
            _cache.AddOrGetExisting(key, item, DateTime.Now.Add(TimeSpan.FromMinutes(minutes)));
        }
        public static T Get<T>(Func<KeyValuePair<string, object>, bool> predicate)
        {
            return (T)_cache.FirstOrDefault(predicate).Value;
        }
        public static bool Contains(string key)
        {
            return _cache.Contains(key);
        }
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# MemoryCache is a caching mechanism provided by the .NET Framework that allows developers to store data in memory for faster access. It is a key-value store where data is stored as key-value pairs and can be accessed using the key. MemoryCache is designed to be used in applications that require frequent access to data, such as web applications that need to retrieve data from a database repeatedly. By storing frequently accessed data in MemoryCache, you can reduce the number of database calls and improve the performance of your application. MemoryCache provides several features, such as time-based expiration, sliding expiration, and the ability to remove items from the cache manually. It also provides thread-safe operations to prevent race conditions when multiple threads access the cache simultaneously. To use MemoryCache in your C# application, you can create an instance of the MemoryCache class and add items to the cache using the Add method. You can also retrieve items from the cache using the Get method and remove items from the cache using the Remove method. Here's an example of how to use MemoryCache in C#: ``` // Create a new instance of the MemoryCache class MemoryCache cache = MemoryCache.Default; // Add an item to the cache with a key of "myKey" and a value of "myValue" cache.Add("myKey", "myValue", DateTimeOffset.Now.AddMinutes(5)); // Retrieve the value of the item with a key of "myKey" string value = cache.Get("myKey") as string; // Remove the item from the cache with a key of "myKey" cache.Remove("myKey"); ``` Overall, MemoryCache is a powerful tool for improving the performance of your C# applications by reducing the number of database calls and providing faster access to frequently accessed data.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值