Esempio n. 1
0
 public static GetContentValue <T> GetContentValue <T>(IContentService service,
                                                       Dictionary <int, ContentCacheAndTime> pageCache = null,
                                                       UpdateIContentCache updateCatch = null, Action cacheCheck = null)
 {
     return((int pageId, string key, bool?keepAlive) =>
     {
         try
         {
             return GetContentById(pageId, service, pageCache, updateCatch, cacheCheck)
             .GetValueNull <T>(key);
         }
         catch (Exception ex)
         {
             var e = ex;
             return default(T);
         }
     });
 }
Esempio n. 2
0
        public static IContent GetContentById(int id, IContentService service,
                                              Dictionary <int, ContentCacheAndTime> pageCache = null,
                                              UpdateIContentCache updateCatch = null,
                                              Action cacheCheck = null)
        {
            if (pageCache == null)
            {
                pageCache = PageCache;
            }
            IContent content = null;

            try
            {
                if (cacheCheck == null)
                {
                    cacheCheck = DefaultCacheCheck(pageCache);
                }
                if (updateCatch == null)
                {
                    updateCatch = UpdateICache;
                }
                if (!PageCache.ContainsKey(id))
                {
                    content = updateCatch(service.GetById(id), false, pageCache);
                }
                else
                {
                    content = updateCatch(pageCache[id].Content, pageCache[id].KeepAlive, pageCache);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(content);
        }
Esempio n. 3
0
 public static GetContent GetContent(IContentService service, Dictionary <int, ContentCacheAndTime> pageCache = null, UpdateIContentCache updateCatch = null, Action cacheCheck = null)
 {
     return((int pageId, bool?alive) =>
     {
         return GetContentById(pageId, service, pageCache, updateCatch, cacheCheck);
     });
 }
Esempio n. 4
0
 public static GetContentValue GetContentValue(IContentService service, Dictionary <int, ContentCacheAndTime> pageCache = null, UpdateIContentCache updateCatch = null, Action cacheCheck = null)
 {
     return((int pageId, string key, bool?alive) =>
     {
         try
         {
             return GetContentById(pageId, service, pageCache, updateCatch, cacheCheck);
         }
         catch (Exception ex)
         {
             return null;
         }
     });
 }
Esempio n. 5
0
        public static IEnumerable <IContent> GetContentsByIds(
            IContentService services, IEnumerable <int> ids = null, int rootId = -1, int cacheTime = 60,
            Func <IContent, bool> where = null,
            bool?alive = null, Dictionary <int, ContentCacheAndTime> pageCache = null, UpdateIContentCache updateCatch = null, Action cacheCheck = null)
        {
            if (pageCache == null)
            {
                pageCache = PageCache;
            }
            if (updateCatch == null)
            {
                updateCatch = UpdateICache;
            }
            if (cacheCheck == null)
            {
                cacheCheck = DefaultCacheCheck(pageCache);
            }

            cacheCheck();

            var cacheCopy = new Dictionary <int, ContentCacheAndTime>(pageCache);
            IEnumerable <IContent> result = null;
            Func <List <ContentCacheAndTime>, int, bool> checkAvaliable = (list, time) => list.Count > 0 && (DateTime.Now - list.First().Time).TotalMinutes >= time;

            if (ids != null)
            {
                var pages = cacheCopy
                            .Where(b => ids.Contains(b.Value.Content.Id))
                            .Select(b => b.Value).OrderBy(b => b.Time).ToList();
                if (checkAvaliable(pages, cacheTime))
                {
                    result = pages.Select(b => b.Content);
                    goto ReturnCheck;
                }
                result = services.GetByIds(ids);
                goto ReturnCheck;
            }
            if (rootId > 0)
            {
                var contents = cacheCopy.Where(b => rootId == b.Value.Content.ParentId);
                if (where != null)
                {
                    contents = contents.Where(b => where (b.Value.Content));
                }
                var pages = contents.Select(b => b.Value).OrderBy(b => b.Time).ToList();
                if (checkAvaliable(pages, cacheTime))
                {
                    result = pages.Select(b => b.Content);
                    goto ReturnCheck;
                }
                result = services.GetChildren(rootId);
                if (where != null)
                {
                    result.Where(where);
                }
                goto ReturnCheck;
            }
ReturnCheck:
            var check = result.OrderBy(b => b.SortOrder).ToList();

            check.ForEach(b => updateCatch(b, alive, pageCache));
            return(check);
        }