예제 #1
0
        /// <summary>
        /// Gets a cached object by using a model/entity
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static T Get(TT entity)
        {
            if (entity == null)
            {
                return(default(T));
            }

            var value = new T();

            value.SetFromEntity(entity);

            IdFromGuidCache.UpdateCacheItem(entity.Guid.ToString(), new IdFromGuidCache(entity.Id), TimeSpan.MaxValue);
            UpdateCacheItem(entity.Id.ToString(), value, TimeSpan.MaxValue);

            return(value);
        }
예제 #2
0
        /// <summary>
        /// Gets the cached object by id using the included RockContext if needed.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <returns></returns>
        public static T Get(int id, RockContext rockContext)
        {
            if (id == 0)
            {
                return(default(T));
            }

            return(GetOrAddExisting(id, () =>
            {
                var result = QueryDb(id, rockContext);
                if (result != null)
                {
                    IdFromGuidCache.UpdateCacheItem(result.Guid.ToString(), new IdFromGuidCache(id), TimeSpan.MaxValue);
                }

                return result;
            }));
        }
예제 #3
0
        /// <summary>
        /// Gets the cached object by guid using the included RockContext if needed.
        /// </summary>
        /// <param name="guid">The unique identifier.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <returns></returns>
        public static T Get(Guid guid, RockContext rockContext)
        {
            // see if the Id is stored in CacheIdFromGuid
            int?idFromGuid = IdFromGuidCache.GetId(guid);
            T   cachedEntity;

            if (idFromGuid.HasValue)
            {
                cachedEntity = Get(idFromGuid.Value, rockContext);
                return(cachedEntity);
            }

            // If not, query the database for it, and then add to cache (if found)
            cachedEntity = QueryDb(guid, rockContext);
            if (cachedEntity != null)
            {
                IdFromGuidCache.UpdateCacheItem(guid.ToString(), new IdFromGuidCache(cachedEntity.Id), TimeSpan.MaxValue);
                UpdateCacheItem(cachedEntity.Id.ToString(), cachedEntity, TimeSpan.MaxValue);
            }

            return(cachedEntity);
        }