System.Web.Caching.Cache.cs
ylbtech-System.Web.Caching.Cache.cs |
1.返回顶部 |
1、
#region 程序集 System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a // C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5System.Web.dll #endregion using System.Collections; using System.Reflection; namespace System.Web.Caching { // // 摘要: // 实现的 Web 应用程序缓存。 此类不能被继承。 [DefaultMember("Item")] public sealed class Cache : IEnumerable { // // 摘要: // 在中使用 absoluteExpiration 中的参数 System.Web.Caching.Cache.Insert(System.String,System.Object) // 方法调用,以指示项应该永远不会过期。 此字段为只读。 public static readonly DateTime NoAbsoluteExpiration; // // 摘要: // 用作 slidingExpiration 中的参数 System.Web.Caching.Cache.Insert(System.String,System.Object) // 或 System.Web.Caching.Cache.Add(System.String,System.Object,System.Web.Caching.CacheDependency,System.DateTime,System.TimeSpan,System.Web.Caching.CacheItemPriority,System.Web.Caching.CacheItemRemovedCallback) // 方法调用,以禁用可调到期。 此字段为只读。 public static readonly TimeSpan NoSlidingExpiration; // // 摘要: // 初始化 System.Web.Caching.Cache 类的新实例。 public Cache(); // // 摘要: // 获取或设置指定键处的缓存项。 // // 参数: // key: // 一个 System.String 对象,表示缓存项的键。 // // 返回结果: // 指定的缓存项。 public object this[string key] { get; set; } // // 摘要: // 获取存储在缓存中的项的数目。 // // 返回结果: // 存储在缓存中的项的数目。 public int Count { get; } // // 摘要: // 获取用于缓存的可用字节数。 // // 返回结果: // 可用于缓存的字节数。 public long EffectivePrivateBytesLimit { get; } // // 摘要: // 获取 ASP.NET 开始从缓存中删除项之前,应用程序可以使用的物理内存的百分比。 // // 返回结果: // 向应用程序可用物理内存的百分比。 public long EffectivePercentagePhysicalMemoryLimit { get; } // // 摘要: // 指定将项添加到 System.Web.Caching.Cache 具有依赖关系、 过期时间和优先级策略和您可以使用从删除插入的项时通知您的应用程序委托对象 // Cache。 // // 参数: // key: // 用于引用项的缓存密钥。 // // value: // 要添加到缓存中的项。 // // dependencies: // 文件或缓存关键的依存关系项。 当任何依赖关系更改时,该对象将变为无效,并从缓存中删除。 如果没有依赖关系,此参数包含 null。 // // absoluteExpiration: // 此时已添加的对象过期并从缓存中删除的时间。 如果您使用可调到期, absoluteExpiration 参数必须是 System.Web.Caching.Cache.NoAbsoluteExpiration。 // // slidingExpiration: // 对象的到期时间和上次访问所添加的对象的时间之间的间隔。 如果此值为 20 分钟的等效项,该对象到期,并从缓存中删除 20 分钟后它上次访问。 如果您使用的绝对过期 // slidingExpiration 参数必须是 System.Web.Caching.Cache.NoSlidingExpiration。 // // priority: // 通过表示的对象的相对成本 System.Web.Caching.CacheItemPriority 枚举。 缓存时逐出对象; 使用此值具有较低的成本的对象会从缓存后再成本较高的对象。 // // onRemoveCallback: // 调用的委托,如果提供,是在从缓存中移除对象时。 可以使用此以通知应用程序时从缓存中删除它们的对象。 // // 返回结果: // 一个对象,表示该项目之前存储在缓存中; 如果已添加的项否则为 null。 // // 异常: // T:System.ArgumentNullException: // key 或 value 参数设置为 null。 // // T:System.ArgumentOutOfRangeException: // slidingExpiration 参数设置为小于 TimeSpan.Zero 或一年以上。 // // T:System.ArgumentException: // absoluteExpiration 和 slidingExpiration 参数都设置为想要将添加到项 Cache。 public object Add(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback); // // 摘要: // 检索指定的项 System.Web.Caching.Cache 对象。 // // 参数: // key: // 要检索的缓存项的标识符。 // // 返回结果: // 检索到的缓存项,或 null 如果未找到此项。 public object Get(string key); // // 摘要: // 检索用于循环访问密钥设置的字典枚举数以及它们在缓存中包含的值。 // // 返回结果: // 要循环访问的枚举数 System.Web.Caching.Cache 对象。 public IDictionaryEnumerator GetEnumerator(); // // 摘要: // 将一项插入 System.Web.Caching.Cache 带有一个缓存键来引用其位置,并使用提供的默认值的对象 System.Web.Caching.CacheItemPriority // 枚举。 // // 参数: // key: // 用于引用项的缓存密钥。 // // value: // 要插入到缓存的对象。 // // 异常: // T:System.ArgumentNullException: // key 或 value 参数为 null。 public void Insert(string key, object value); // // 摘要: // 将对象插入 System.Web.Caching.Cache 文件或项的依赖项。 // // 参数: // key: // 用于标识项的缓存密钥。 // // value: // 要插入缓存中的对象。 // // dependencies: // 文件或缓存关键的依存关系所插入对象。 当任何依赖关系更改时,该对象将变为无效,并从缓存中删除。 如果没有依赖关系,此参数包含 null。 // // 异常: // T:System.ArgumentNullException: // key 或 value 参数为 null。 public void Insert(string key, object value, CacheDependency dependencies); // // 摘要: // 将对象插入 System.Web.Caching.Cache 与依赖关系和过期策略。 // // 参数: // key: // 用于引用该对象的缓存密钥。 // // value: // 要插入缓存中的对象。 // // dependencies: // 文件或缓存关键的依存关系所插入对象。 当任何依赖关系更改时,该对象将变为无效,并从缓存中删除。 如果没有依赖关系,此参数包含 null。 // // absoluteExpiration: // 从该处插入的对象过期并从缓存中删除的时间。 若要避免的本地时间,如从标准时间到夏时制的更改可能存在的问题,请使用 System.DateTime.UtcNow // 而不是 System.DateTime.Now 为此参数值。 如果您使用的绝对过期 slidingExpiration 参数必须是 System.Web.Caching.Cache.NoSlidingExpiration。 // // slidingExpiration: // 对象的到期时间和上次访问所插入的对象的时间之间的间隔。 如果此值为 20 分钟的等效项,该对象会过期,可从缓存中删除上次访问后的 20 分钟。 如果您使用可调到期, // absoluteExpiration 参数必须是 System.Web.Caching.Cache.NoAbsoluteExpiration。 // // 异常: // T:System.ArgumentNullException: // key 或 value 参数为 null。 // // T:System.ArgumentOutOfRangeException: // 您设置 slidingExpiration 参数小于 TimeSpan.Zero 或一年以上的等效项。 // // T:System.ArgumentException: // absoluteExpiration 和 slidingExpiration 参数都设置为想要将添加到项 Cache。 public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration); // // 摘要: // 将对象插入 System.Web.Caching.Cache 具有依赖关系、 过期时间和优先级策略和您可以使用从删除插入的项时通知您的应用程序委托对象 Cache。 // // 参数: // key: // 用于引用该对象的缓存密钥。 // // value: // 要插入缓存中的对象。 // // dependencies: // 文件或缓存关键的依存关系项。 当任何依赖关系更改时,该对象将变为无效,并从缓存中删除。 如果没有依赖关系,此参数包含 null。 // // absoluteExpiration: // 从该处插入的对象过期并从缓存中删除的时间。 若要避免的本地时间,如从标准时间到夏时制的更改可能存在的问题,请使用 System.DateTime.UtcNow // 而不是 System.DateTime.Now 为此参数值。 如果您使用的绝对过期 slidingExpiration 参数必须是 System.Web.Caching.Cache.NoSlidingExpiration。 // // slidingExpiration: // 对象的到期时间和上次访问所插入的对象的时间之间的间隔。 如果此值为 20 分钟的等效项,该对象会过期,可从缓存中删除上次访问后的 20 分钟。 如果您使用可调到期, // absoluteExpiration 参数必须是 System.Web.Caching.Cache.NoAbsoluteExpiration。 // // priority: // 与存储在缓存中,如通过所表示的其他项相关对象的成本 System.Web.Caching.CacheItemPriority 枚举。 逐出对象; 时,缓存使用此值具有较低的成本的对象会从缓存后再成本较高的对象。 // // onRemoveCallback: // 调用的委托,如果提供,将从缓存中移除对象时。 可以使用此以通知应用程序时从缓存中删除它们的对象。 // // 异常: // T:System.ArgumentNullException: // key 或 value 参数为 null。 // // T:System.ArgumentOutOfRangeException: // 您设置 slidingExpiration 参数小于 TimeSpan.Zero 或一年以上的等效项。 // // T:System.ArgumentException: // absoluteExpiration 和 slidingExpiration 参数都设置为想要将添加到项 Cache。 public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback); // // 摘要: // 将对象插入 System.Web.Caching.Cache 对象与依赖关系、 过期策略和一个委托,可用于通知应用程序之前从缓存中移除的项。 // // 参数: // key: // 用来引用对象的缓存密钥。 // // value: // 要向缓存中插入的对象。 // // dependencies: // 文件或缓存关键的依存关系项。 当任何依赖关系更改时,该对象将变为无效,并从缓存中删除。 如果没有依赖关系,此参数包含 null。 // // absoluteExpiration: // 从该处插入的对象过期并从缓存中删除的时间。 若要避免的本地时间,如从标准时间到夏时制的更改可能存在的问题,请使用 System.DateTime.UtcNow // 而不是 System.DateTime.Now 为此参数值。 如果您使用的绝对过期 slidingExpiration 参数必须设置为 System.Web.Caching.Cache.NoSlidingExpiration。 // // slidingExpiration: // 该对象的到期的时间和上次访问缓存的对象的时间之间时间间隔。 如果此值为 20 分钟的等效项,该对象会过期,可从缓存中删除上次访问后的 20 分钟。 如果您使用可调到期, // absoluteExpiration 参数必须设置为 System.Web.Caching.Cache.NoAbsoluteExpiration。 // // onUpdateCallback: // 从缓存中删除该对象之前将调用一个委托。 您可以用于更新缓存的项目,并确保它不删除从缓存。 // // 异常: // T:System.ArgumentNullException: // key, ,value, ,或 onUpdateCallback 参数是 null。 // // T:System.ArgumentOutOfRangeException: // 您设置 slidingExpiration 参数小于 TimeSpan.Zero 或一年以上的等效项。 // // T:System.ArgumentException: // absoluteExpiration 和 slidingExpiration 参数都设置为想要将添加到项 Cache。- 或 -dependencies // 参数是 null, ,和 absoluteExpiration 参数设置为 System.Web.Caching.Cache.NoAbsoluteExpiration, // ,和 slidingExpiration 参数设置为 System.Web.Caching.Cache.NoSlidingExpiration。 public void Insert(string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemUpdateCallback onUpdateCallback); // // 摘要: // 从该应用程序中移除指定的项 System.Web.Caching.Cache 对象。 // // 参数: // key: // 一个 System.String 要移除的缓存项的标识符。 // // 返回结果: // 从其中移除的项 Cache。 如果未找到键参数中的值,将返回 null。 public object Remove(string key); } }
2、
2.返回顶部 |
1、ICache.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Purple.Code { public interface ICache { T GetCache<T>(string cacheKey) where T : class; void WriteCache<T>(T value, string cacheKey) where T : class; void WriteCache<T>(T value, string cacheKey, DateTime expireTime) where T : class; void RemoveCache(string cacheKey); void RemoveCache(); } }
2、Cache.cs
using System; using System.Collections; using System.Web; namespace Purple.Code { public class Cache : ICache { private static System.Web.Caching.Cache cache = HttpRuntime.Cache; public T GetCache<T>(string cacheKey) where T : class { if (cache[cacheKey] != null) { return (T)cache[cacheKey]; } return default(T); } public void WriteCache<T>(T value, string cacheKey) where T : class { cache.Insert(cacheKey, value, null, DateTime.Now.AddMinutes(10), System.Web.Caching.Cache.NoSlidingExpiration); } public void WriteCache<T>(T value, string cacheKey, DateTime expireTime) where T : class { cache.Insert(cacheKey, value, null, expireTime, System.Web.Caching.Cache.NoSlidingExpiration); } public void RemoveCache(string cacheKey) { cache.Remove(cacheKey); } public void RemoveCache() { IDictionaryEnumerator CacheEnum = cache.GetEnumerator(); while (CacheEnum.MoveNext()) { cache.Remove(CacheEnum.Key.ToString()); } } } }
3、CacheFactory.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Purple.Code { public class CacheFactory { public static ICache Cache() { return new Cache(); } } }
4、
3. 调用返回顶部 |
1、
public bool ActionValidate(string userId, string moduleId, string action) { //ylb 用户多权限 var authorizeurldata = new List<AuthorizeActionModel>(); var cachedata = CacheFactory.Cache().GetCache<List<AuthorizeActionModel>>("authorizeurldata_" + userId); //取缓存 if (cachedata == null) { var moduledata = moduleApp.GetList(); var buttondata = moduleButtonApp.GetList(); var userRoleEntitys = userRoleApp.GetListByUserId(userId); //获取用户角色 foreach (UserRoleEntity userRoleEntity in userRoleEntitys) { var authorizedata = service.IQueryable(t => t.F_ObjectId == userRoleEntity.F_RoleId).ToList(); foreach (var item in authorizedata) { if (item.F_ItemType == 1) { ModuleEntity moduleEntity = moduledata.Find(t => t.F_Id == item.F_ItemId); authorizeurldata.Add(new AuthorizeActionModel { F_Id = moduleEntity.F_Id, F_UrlAddress = moduleEntity.F_UrlAddress }); } else if (item.F_ItemType == 2) { ModuleButtonEntity moduleButtonEntity = buttondata.Find(t => t.F_Id == item.F_ItemId); authorizeurldata.Add(new AuthorizeActionModel { F_Id = moduleButtonEntity.F_ModuleId, F_UrlAddress = moduleButtonEntity.F_UrlAddress }); } } } CacheFactory.Cache().WriteCache(authorizeurldata, "authorizeurldata_" + userId, DateTime.Now.AddMinutes(5)); //存缓存 } else { authorizeurldata = cachedata; } authorizeurldata = authorizeurldata.FindAll(t => t.F_Id.Equals(moduleId)); foreach (var item in authorizeurldata) { if (!string.IsNullOrEmpty(item.F_UrlAddress)) { string[] url = item.F_UrlAddress.Split('?'); if (item.F_Id == moduleId && url[0] == action) { return true; } } } return false; }
2、
4.返回顶部 |
5.返回顶部 |
6.返回顶部 |
作者:ylbtech 出处:http://ylbtech.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |