/// <summary> /// Returns Role object from cache. If role does not already exist in cache, it /// will be read and added to cache /// </summary> /// <param name="id">The id.</param> /// <returns></returns> public static Role Read(int id) { string cacheKey = Role.CacheKey(id); ObjectCache cache = Rock.Web.Cache.RockMemoryCache.Default; Role role = cache[cacheKey] as Role; if (role != null) { return(role); } else { using (var rockContext = new RockContext()) { Rock.Model.GroupService groupService = new Rock.Model.GroupService(rockContext); Rock.Model.Group groupModel = groupService.Get(id); if (groupModel != null && groupModel.IsSecurityRole == true) { role = new Role(); role.Id = groupModel.Id; role.Name = groupModel.Name; role.Users = new List <string>(); role.IsSecurityTypeGroup = groupModel.GroupType.Guid.Equals(Rock.SystemGuid.GroupType.GROUPTYPE_SECURITY_ROLE.AsGuid()); foreach (Rock.Model.GroupMember member in groupModel.Members) { role.Users.Add(member.Person.Guid.ToString()); } cache.Set(cacheKey, role, new CacheItemPolicy()); return(role); } } } return(null); }
/// <summary> /// Returns Role object from cache. If role does not already exist in cache, it /// will be read and added to cache /// </summary> /// <param name="id">The id.</param> /// <returns></returns> public static Role Read(int id) { string cacheKey = Role.CacheKey(id); ObjectCache cache = MemoryCache.Default; Role role = cache[cacheKey] as Role; if (role != null) { return(role); } else { Rock.Model.GroupService groupService = new Rock.Model.GroupService(); Rock.Model.Group groupModel = groupService.Get(id); if (groupModel != null && groupModel.IsSecurityRole == true) { role = new Role(); role.Id = groupModel.Id; role.Name = groupModel.Name; role.Users = new List <string>(); foreach (Rock.Model.GroupMember member in groupModel.Members) { role.Users.Add(member.Person.Guid.ToString()); } cache.Set(cacheKey, role, new CacheItemPolicy()); return(role); } else { return(null); } } }
/// <summary> /// Removes role from cache /// </summary> /// <param name="id">The id.</param> public static void Flush(int id) { ObjectCache cache = Rock.Web.Cache.RockMemoryCache.Default; cache.Remove(Role.CacheKey(id)); }
/// <summary> /// Returns Role object from cache. If role does not already exist in cache, it /// will be read and added to cache /// </summary> /// <param name="id">The id.</param> /// <returns></returns> public static Role Read(int id) { return(GetOrAddExisting(Role.CacheKey(id), () => LoadById(id))); }