Beispiel #1
0
        public Database(string connectionString)
        {
            cli = new MongoClient(connectionString);
            db  = cli.GetServer().GetDatabase("autoru");
            CachedContent.EnsureIndex(IndexKeys <CachedContent> .Descending(x => x.Timestamp), IndexOptions.SetTimeToLive(TimeSpan.FromDays(7)));
            Post.EnsureIndex(IndexKeys <Post> .Descending(x => x.Timestamp), IndexOptions.SetTimeToLive(TimeSpan.FromDays(7)));
            Post.EnsureIndex(IndexKeys <Post>
                             .Ascending(x => x.ForumId)
                             .Descending(x => x.TopicId)
                             .Ascending(x => x.Index)
                             );
            if (!Forum.AsQueryable().Any())
            {
                Forum.Save(new Forum {
                    ForumId = "moto", DoCrawl = true
                });
                Forum.Save(new Forum {
                    ForumId = "scooter", DoCrawl = true
                });
            }

            User.EnsureIndex(IndexKeys <User> .Ascending(x => x.UserName));
            ReadId.EnsureIndex(IndexKeys <ReadId> .Ascending(x => x.UserId).Ascending(x => x.PostId));
            ReadId.EnsureIndex(IndexKeys <ReadId> .Ascending(x => x.UserId).Descending(x => x.Timestamp));
        }
        public static async Task AddItemsAsync <T>(string key, CachedContent <T> data, bool useStorageCache)
        {
            try
            {
                string json = await Json.StringifyAsync(data);

                if (useStorageCache)
                {
                    await UserStorage.WriteTextAsync(key, json);
                }

                if (_memoryCache.ContainsKey(key))
                {
                    _memoryCache[key] = json;
                }
                else
                {
                    _memoryCache.Add(key, json);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }
        public static async Task <CachedContent <T> > GetItemsAsync <T>(string key)
        {
            string json = null;

            if (_memoryCache.ContainsKey(key))
            {
                json = _memoryCache[key];
            }
            else
            {
                json = await UserStorage.ReadTextFromFileAsync(key);

                _memoryCache[key] = json;
            }
            if (!String.IsNullOrEmpty(json))
            {
                try
                {
                    CachedContent <T> records = await Json.ToObjectAsync <CachedContent <T> >(json);

                    return(records);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex);
                }
            }
            return(null);
        }
Beispiel #4
0
        protected override void ParseItems(CachedContent <TSchema> content, ItemViewModel selectedItem)
        {
            var parsedItems = new List <ItemViewModel>();
            IEnumerable <TSchema> sourceVisibleItems = null;

            if (_visibleItems == 0)
            {
                sourceVisibleItems = content.Items;
            }
            else
            {
                sourceVisibleItems = content.Items.Take(_visibleItems);
            }
            foreach (var item in sourceVisibleItems)
            {
                var parsedItem = new ItemViewModel
                {
                    Id             = item._id,
                    NavigationInfo = _sectionConfig.ListPage.NavigationInfo(item)
                };
                _sectionConfig.ListPage.LayoutBindings(parsedItem, item);
                parsedItems.Add(parsedItem);
            }

            Items.Sync(parsedItems);
            HasMoreItems = content.Items.Count() > Items.Count;
        }
Beispiel #5
0
        private void ParseItems <TSchema>(ListPageConfig <TSchema> listConfig, CachedContent <TSchema> content, Func <ItemViewModel, bool> filterFunc) where TSchema : SchemaBase
        {
            var parsedItems = new List <ItemViewModel>();

            foreach (var item in GetVisibleItems(content, _visibleItems))
            {
                var parsedItem = new ItemViewModel
                {
                    Id             = item._id,
                    NavigationInfo = listConfig.DetailNavigation(item)
                };
                listConfig.LayoutBindings(parsedItem, item);
                if (filterFunc == null)
                {
                    parsedItems.Add(parsedItem);
                }
                else if (filterFunc(parsedItem))
                {
                    parsedItems.Add(parsedItem);
                }
            }
            Items.Sync(parsedItems);
            HasItems     = Items.Count > 0;
            HasMoreItems = content.Items.Count() > Items.Count;
        }
Beispiel #6
0
 protected override void ParseItems(CachedContent <TouchDevelopSchema> content, ItemViewModel selectedItem)
 {
     Items.Clear();
     foreach (var item in content.Items)
     {
         Items.Add(item);
     }
 }
Beispiel #7
0
 private IEnumerable <TSchema> GetVisibleItems <TSchema>(CachedContent <TSchema> content, int visibleItems) where TSchema : SchemaBase
 {
     if (visibleItems == 0)
     {
         return(content.Items);
     }
     else
     {
         return(content.Items
                .Take(visibleItems));
     }
 }
        private void ParseItems <TSchema>(ListPageConfig <TSchema> listConfig, CachedContent <TSchema> content) where TSchema : SchemaBase
        {
            foreach (var item in content.Items)
            {
                var parsedItem = new ItemViewModel
                {
                    Id             = item._id,
                    NavigationInfo = listConfig.DetailNavigation(item)
                };
                listConfig.LayoutBindings(parsedItem, item);
                _items.Add(parsedItem);
            }

            HasItems = _items.Count > 0;
        }
Beispiel #9
0
        public static async Task <LoaderOutcome> LoadAsync <T>(LoaderSettings settings, Func <Task <IEnumerable <T> > > loadFunc, Action <IEnumerable <T> > parseItems) where T : SchemaBase
        {
            var outcome = new LoaderOutcome();

            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }
            if (loadFunc == null)
            {
                throw new ArgumentNullException("loadFunc");
            }
            if (parseItems == null)
            {
                throw new ArgumentNullException("parseItems");
            }

            CachedContent <T> dataInCache = null;

            if (!string.IsNullOrEmpty(settings.CacheKey))
            {
                dataInCache = await AppCache.GetItemsAsync <T>(settings.CacheKey);

                if (dataInCache != null)
                {
                    parseItems(dataInCache.Items);
                }
            }

            if (CanPerformLoad <T>(settings.NeedsNetwork) && (settings.ForceRefresh || DataNeedToBeUpdated(dataInCache, settings.CacheExpiration)))
            {
                dataInCache = dataInCache ?? new CachedContent <T>();

                outcome.IsFreshData   = true;
                dataInCache.Timestamp = DateTime.Now;
                dataInCache.Items     = await loadFunc();

                if (!string.IsNullOrEmpty(settings.CacheKey))
                {
                    await AppCache.AddItemsAsync(settings.CacheKey, dataInCache, settings.UseStorage);
                }

                parseItems(dataInCache.Items);
            }
            outcome.Timestamp = dataInCache?.Timestamp;
            return(outcome);
        }
Beispiel #10
0
        protected override void ParseItems(CachedContent<TSchema> content, ItemViewModel selectedItem)
        {

            foreach (var item in content.Items)
            {
                var composedItem = new ComposedItemViewModel
                {
                    Id = item._id
                };

                foreach (var binding in _sectionConfig.DetailPage.LayoutBindings)
                {
                    var parsedItem = new ItemViewModel
                    {
                        Id = item._id
                    };
                    binding(parsedItem, item);

                    composedItem.Add(parsedItem);
                }

                composedItem.Actions = _sectionConfig.DetailPage.Actions
                                                                    .Select(a => new ActionInfo
                                                                    {
                                                                        Command = a.Command,
                                                                        CommandParameter = a.CommandParameter(item),
                                                                        Style = a.Style,
                                                                        Text = a.Text,
                                                                        ActionType = ActionType.Primary
                                                                    })
                                                                    .ToList();

                Items.Add(composedItem);
            }
            if (selectedItem != null)
            {
                SelectedItem = Items.FirstOrDefault(i => i.Id == selectedItem.Id);
            }

        }
Beispiel #11
0
        string Download(string uri, bool avoidCache = false)
        {
            uri = uri.ToLowerInvariant();
            var cacheItem = Global.Db.CachedContent.FindOneById(uri);

            if (!avoidCache && cacheItem != null)
            {
                return(cacheItem.Content);
            }
            var req = WebRequest.CreateHttp(uri);
            var cc  = new CookieContainer();
            var c   = new Cookie("VM_134", "wwwboards5", "/", ".auto.ru");

            c.Expires = DateTime.Now.AddDays(1).Date;
            cc.Add(c);
            c         = new Cookie("cookie_test", "1", "/", ".auto.ru");
            c.Expires = DateTime.Now.AddDays(1).Date;
            cc.Add(c);
            req.CookieContainer = cc;
            var    resp = req.GetResponse();
            string text;

            using (var sr = new StreamReader(resp.GetResponseStream()))
            {
                text = sr.ReadToEnd();
            }

            //var wc = new WebClient();
            //var bytes = wc.DownloadData(uri);
            //var text = Encoding.UTF8.GetString(bytes);
            cacheItem = new CachedContent {
                Uri = uri, Timestamp = DateTime.UtcNow, Content = text
            };
            Global.Db.CachedContent.Save(cacheItem);
            System.Threading.Thread.Sleep(300);
            return(text);
        }
Beispiel #12
0
        private bool DeliverFromCache(HttpContext context,
                                      HttpRequest request, HttpResponse response,
                                      string cacheKey,
                                      string physicalFilePath, ResponseCompressionType compressionType)
        {
            CachedContent cachedContent = context.Cache[cacheKey] as CachedContent;

            if (null != cachedContent)
            {
                byte[] cachedBytes = cachedContent.ResponseBytes;

                // We have it cached
                this.ProduceResponseHeader(response, cachedBytes.Length, compressionType,
                                           physicalFilePath, cachedContent.LastModified);
                this.WriteResponse(response, cachedBytes, compressionType, physicalFilePath);

                Debug.WriteLine("StaticFileHandler: Cached: " + request.FilePath);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #13
0
 protected async override void OnNavigatedFrom(NavigationEventArgs e)
 {
     base.OnNavigatedFrom(e);
     CachedContent<string> cacheData = new CachedContent<string>() { Items = Items };
     await AppStudio.Common.Cache.AppCache.AddItemsAsync<string>("AppCacheList", cacheData);
 }
Beispiel #14
0
 private static bool DataNeedToBeUpdated <T>(CachedContent <T> dataInCache, TimeSpan expiration)
 {
     return(dataInCache == null || (DateTime.Now - dataInCache.Timestamp > expiration));
 }
        private void ParseDetailItems <TSchema>(DetailPageConfig <TSchema> detailConfig, CachedContent <TSchema> content, ItemViewModel selectedItem) where TSchema : SchemaBase
        {
            TSchema sourceSelected;

            ParseDetailItems(detailConfig, content, selectedItem, out sourceSelected);
        }
        private void ParseDetailItems <TSchema>(DetailPageConfig <TSchema> detailConfig, CachedContent <TSchema> content, ItemViewModel selectedItem, out TSchema sourceSelected) where TSchema : SchemaBase
        {
            sourceSelected = content.Items.FirstOrDefault(i => i._id == selectedItem.Id);

            foreach (var item in content.Items)
            {
                var composedItem = new ComposedItemViewModel
                {
                    Id = item._id
                };

                foreach (var binding in detailConfig.LayoutBindings)
                {
                    var parsedItem = new ItemViewModel
                    {
                        Id = item._id
                    };
                    binding(parsedItem, item);

                    composedItem.Add(parsedItem);
                }

                composedItem.Actions = detailConfig.Actions
                                       .Select(a => new ActionInfo
                {
                    Command          = a.Command,
                    CommandParameter = a.CommandParameter(item),
                    Style            = a.Style,
                    Text             = a.Text,
                    ActionType       = ActionType.Primary
                })
                                       .ToList();

                Items.Add(composedItem);
            }
            if (selectedItem != null)
            {
                SelectedItem = Items.FirstOrDefault(i => i.Id == selectedItem.Id);
            }
        }
        private void ParseRelatedItems <TSchema>(ListPageConfig <TSchema> listConfig, CachedContent <TSchema> content) where TSchema : SchemaBase
        {
            var parsedItems = new List <ItemViewModel>();

            foreach (var item in content.Items)
            {
                var parsedItem = new ItemViewModel
                {
                    Id             = item._id,
                    NavigationInfo = listConfig.DetailNavigation(item)
                };
                listConfig.LayoutBindings(parsedItem, item);
                parsedItems.Add(parsedItem);
            }

            RelatedItems.Sync(parsedItems);
        }
 public static async Task AddItemsAsync <T>(string key, CachedContent <T> data)
 {
     await AddItemsAsync <T>(key, data, true);
 }