MemoryCache

Namespace: NuGet.Core
We found 10 examples in language CSharp for this search. You will see 45 fragments of code.
        public T GetValue(int id)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            return memoryCache.Get(id.ToString()) as T;
        }

        public T GetValue(string name)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            return memoryCache.Get(name) as T;
        }

        public bool Add(T value, int id)
        {            
            MemoryCache memoryCache = MemoryCache.Default;
            return memoryCache.Add(id.ToString(), value, DateTime.Now.AddMinutes(10));
        }

        public bool Add(T value, string name)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            return memoryCache.Add(name, value, DateTime.Now.AddMinutes(10));
        }

        public void Update(T value, int id)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            memoryCache.Set(id.ToString(), value, DateTime.Now.AddMinutes(10));
        }

        public void Delete(int id)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            if (memoryCache.Contains(id.ToString()))
            {
                memoryCache.Remove(id.ToString());
            }
        }

        public static object GetValue(string key)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            return memoryCache.Get(key);
        }

        public static bool Add(string key, object value, DateTimeOffset absExpiration)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            return memoryCache.Add(key, value, absExpiration);
        }

        public static void Delete(string key)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            if (memoryCache.Contains(key))
            {
                memoryCache.Remove(key);
            }
        }


    /// <summary>
    /// Clear memory cache.
    /// </summary>
    public void ClearMemoryCache()
    {
        for (int i = 0; i < countMemoryItems; i++)
        {
            memoryCache[i].Dispose();
            memoryCache[i] = null;
        }
        memoryCacheSize = 0;
        countMemoryItems = 0;
    }

        public static TValue AddOrGet<TValue>(this MemoryCache cache, string key, TValue value)
        {
            object item = cache.AddOrGetExisting(key, value, new CacheItemPolicy()) ?? value;

            return (TValue)item;
        }

        public static TValue AddOrGet<TValue>(this MemoryCache cache, string key, Func<string, TValue> valueFactory)
        {
            var lazy = new Lazy<TValue>(() => valueFactory(key));

            Lazy<TValue> item = (Lazy<TValue>)cache.AddOrGetExisting(key, lazy, new CacheItemPolicy()) ?? lazy;

            return item.Value;
        }

        public static TValue AddOrGet<TValue>(this MemoryCache cache, string key, Func<string, TValue> valueFactory, CacheItemPolicy policy, string regionName = null)
        {
            var lazy = new Lazy<TValue>(() => valueFactory(key));

            Lazy<TValue> item = (Lazy<TValue>)cache.AddOrGetExisting(key, lazy, policy, regionName) ?? lazy;

            return item.Value;
        }

        public static TValue AddOrGet<TValue>(this MemoryCache cache, string key, Func<string, TValue> valueFactory, DateTimeOffset absoluteExpiration, string regionName = null)
        {
            var lazy = new Lazy<TValue>(() => valueFactory(key));

            Lazy<TValue> item = (Lazy<TValue>)cache.AddOrGetExisting(key, lazy, absoluteExpiration, regionName) ?? lazy;

            return item.Value;
        }

        public static TReturn SafeGet<TReturn>(this MemoryCache memoryCache, string key, Func<TReturn> objectToCache)
        {
            if (memoryCache.Contains(key))
                return (TReturn)memoryCache[key];

            return (TReturn)(memoryCache[key] = objectToCache());
        }

        public static TReturn SafeGet<TReturn>(this MemoryCache memoryCache, string key, TReturn objectToCache)
        {
            if (memoryCache.Contains(key))
                return (TReturn)memoryCache[key];

            return (TReturn)(memoryCache[key] = objectToCache);
        }


        [TestMethod]
        [DataRow(2)]
        [DataRow(1000)]
        [DataRow(100_000)]
        public void Parallel_Add(int capacity)
        {
            var memoryCache = BoundedMemoryCache.CreateLRU<string, TestCacheItem>(capacity);
            var keys = new ConcurrentBag<string>();

            FillCache(capacity * 4, memoryCache, keys);

            Assert.AreEqual(capacity, GetCacheCount(memoryCache, keys));
        }

        #endregion Public 方法

        #region Private 方法

        private static void FillCache(int count, IBoundedMemoryCache<string, TestCacheItem> memoryCache, ConcurrentBag<string> keys)
        {
            Parallel.For(0, count, _ =>
            {
                var key = Guid.NewGuid().ToString();
                memoryCache.Add(key, new());
                keys.Add(key);
            });
        }

        private static int GetCacheCount(IBoundedMemoryCache<string, TestCacheItem> memoryCache, IEnumerable<string> allKeys)
        {
            return allKeys.Count(m => memoryCache.TryGet(m, out var item) && item is not null);
        }


    private object GetValue(string key)
    {
      MemoryCache memoryCache = MemoryCache.Default;
      if (!memoryCache.Contains(key))
        return String.Empty;

      return memoryCache.Get(key);
    }

    private bool SetValue(string key, object value)
    {
      MemoryCache memoryCache = MemoryCache.Default;
      DateTimeOffset offset = new DateTimeOffset(new DateTime(2050, 1, 1));
      if (memoryCache.Contains(key))
      {
        memoryCache[key] = value;
        return true;
      }

      return memoryCache.Add(key, value, offset);
    }

    public void Delete(string key)
    {
      MemoryCache memoryCache = MemoryCache.Default;
      if (memoryCache.Contains(key))
      {
        memoryCache.Remove(key);
      }
    }

    public void Clear()
    {
      MemoryCache memoryCache = MemoryCache.Default;
      List<string> allKeys = new List<string>();
      for (int i = 0; i < memoryCache.Count(); i++)
        allKeys.Add(memoryCache.ElementAt(i).Key);

      foreach (string key in allKeys)
        memoryCache.Remove(key);
    }

    public Dictionary<string, object> Get_Sets()
    {
      MemoryCache memoryCache = MemoryCache.Default;
      var cacheSets = memoryCache.Where(m => m.Value != null);

      Dictionary<string, object> sets = new Dictionary<string, object>();
      foreach (var cacheSet in cacheSets)
      {
        sets.Add(cacheSet.Key, cacheSet.Value);
      }

      return sets;
    }

        public List<T> Get<T>(string key)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            return memoryCache.Get(key) as List<T>;
        }

        public Dictionary<T, TT> Get<T, TT>(string key)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            return memoryCache.Get(key) as Dictionary<T, TT>;
        }

        public bool Add<T>(List<T> value, string key, int expirationInMinutes)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            return memoryCache.Add(key, value, DateTime.Now.AddMinutes(expirationInMinutes));
        }

        public bool Add<T, TT>(Dictionary<T, TT> value, string key, int expirationInMinutes)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            return memoryCache.Add(key, value, DateTime.Now.AddMinutes(expirationInMinutes));
        }

        public void Update<T>(List<T> value, string key, int expirationInMinutes)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            memoryCache.Set(key, value, DateTime.Now.AddMinutes(expirationInMinutes));
        }

        public void Update<T, TT>(Dictionary<T, TT> value, string key, int expirationInMinutes)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            memoryCache.Set(key, value, DateTime.Now.AddMinutes(expirationInMinutes));
        }


    private object GetValue(string key)
    {
      MemoryCache memoryCache = MemoryCache.Default;
      if (!memoryCache.Contains(key))
        return String.Empty;

      return memoryCache.Get(key);
    }

    private bool SetValue(string key, object value)
    {
      MemoryCache memoryCache = MemoryCache.Default;
      DateTimeOffset offset = new DateTimeOffset(new DateTime(2050, 1, 1));
      if (memoryCache.Contains(key))
      {
        memoryCache[key] = value;
        return true;
      }

      return memoryCache.Add(key, value, offset);
    }

    public void Delete(string key)
    {
      MemoryCache memoryCache = MemoryCache.Default;
      if (memoryCache.Contains(key))
      {
        memoryCache.Remove(key);
      }
    }

    public void Clear()
    {
      MemoryCache memoryCache = MemoryCache.Default;
      List<string> allKeys = new List<string>();
      for (int i = 0; i < memoryCache.Count(); i++)
        allKeys.Add(memoryCache.ElementAt(i).Key);

      foreach (string key in allKeys)
        memoryCache.Remove(key);
    }

        public object GetValue(string key)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            return memoryCache.Get(key);
        }

        public bool Add(string key, object value, DateTimeOffset absExpiration)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            return memoryCache.Add(key, value, absExpiration);
        }

        public void Delete(string key)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            if (memoryCache.Contains(key))
            {
                memoryCache.Remove(key);
            }
        }

        public bool IfCacheExsist(string key)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            var cache = memoryCache.Get(key);
            return cache != null;
        }

        public void ClearCache(string key)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            
            if (IfCacheExsist(key))
            {
                Delete(key);
            }
        }

        //-----------------------------PRODUCTS----------------------------------------------------
        public List<product> GetProducts()
        {
            MemoryCache memoryCache = MemoryCache.Default;
            return memoryCache.Get("products") as List<product>;
        }

        public bool AddProducts(List<product> products)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            return memoryCache.Add("products", products, DateTime.Now.AddMinutes(30));
        }

        public void DeleteProducts()
        {
            MemoryCache memoryCache = MemoryCache.Default;
            if (memoryCache.Contains("products"))
            {
                memoryCache.Remove("products");
            }
        }
        //--------------FORMATS-----------------------------------------------------
        public List<format> GetFormats()
        {
            MemoryCache memoryCache = MemoryCache.Default;
            return memoryCache.Get("formats") as List<format>;
        }

        public bool AddFormats(List<format> formats)
        {
            MemoryCache memoryCache = MemoryCache.Default;
            return memoryCache.Add("formats", formats, DateTime.Now.AddMinutes(10));
        }

        public void DeleteFormats()
        {
            MemoryCache memoryCache = MemoryCache.Default;
            if (memoryCache.Contains("formats"))
            {
                memoryCache.Remove("formats");
            }
        }

NuGet.MemoryCache : IDisposable

Methods :

public Void Dispose()
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()