123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- using System;
- using System.Collections.Generic;
- using System.Web;
- using System.Web.Caching;
- namespace CP.Cache
- {
- public class BaseCache
- {
- protected static volatile System.Web.Caching.Cache cache = HttpRuntime.Cache;
-
-
-
-
-
-
- public static bool IsExist(string k)
- {
- if (string.IsNullOrEmpty(k))
- return false;
- return cache[k] != null;
- }
-
-
-
-
-
-
- public static T Get<T>(string k)
- {
- if (!string.IsNullOrEmpty(k) && cache[k] != null)
- {
- return (T)cache.Get(k);
- }
- return default(T);
- }
-
-
-
-
-
-
-
- public static void Add<T>(string k,T v,int m = 0)
- {
- if(!string.IsNullOrEmpty(k) && v != null)
- {
- if (m > 0)
- {
- cache.Insert(k, v, null, DateTime.Now.AddMinutes(m), System.Web.Caching.Cache.NoSlidingExpiration);
- }
- else
- {
- cache.Insert(k, v);
- }
- }
- }
-
-
-
-
- public static void Remove(string k)
- {
- if(IsExist(k))
- {
- cache.Remove(k);
- }
- }
-
-
-
-
- public static void RemoveList(string k)
- {
- if (!string.IsNullOrEmpty(k))
- {
- var e = cache.GetEnumerator();
- while (e.MoveNext())
- {
- if (e.Key.ToString().IndexOf(k, StringComparison.OrdinalIgnoreCase) != -1)
- {
- Remove(e.Key.ToString());
- }
- }
- }
- }
-
-
-
- public static void RemoveAll()
- {
- var e = cache.GetEnumerator();
- while (e.MoveNext())
- {
- Remove(e.Key.ToString());
- }
- }
-
-
-
-
- public static List<string> GetKeys()
- {
- List<string> list = new List<string>();
- var e = cache.GetEnumerator();
- while (e.MoveNext())
- {
- list.Add(e.Key.ToString());
- }
- return list;
- }
-
-
-
-
- public static int Count()
- {
- return cache.Count;
- }
- }
- }
|