Пример #1
0
        /// <summary>
        /// loads the contents of the secondary cache into the primary cache (if its still current)
        /// </summary>
        /// <param name="books"></param>
        /// <param name="textSearches"></param>
        /// <param name="similaritySearches"></param>
        public void LoadCache(Dictionary<string, Book> books,
            Dictionary<string, List<Book>> textSearches,
            Dictionary<string, List<Book>> similaritySearches)
        {
            books.Clear();
            textSearches.Clear();
            similaritySearches.Clear();

            List<Book> currentTextSearch = null;
            List<Book> currentSimilaritySearch = null;

            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                //there is no cache so quit early
                if (isoStore.GetFileNames(Constants.SECONDARY_CACHE_FILE).Length == 0)
                {
                    return;
                }

                using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(Constants.SECONDARY_CACHE_FILE, FileMode.Open, isoStore))
                {
                    using (XmlReader reader = XmlReader.Create(isoStream))
                    {
                        while (reader.Read())
                        {
                            //get the timestamp and quit early if the cache has been invalidated
                            if (reader.IsStartElement("tarantula"))
                            {
                                if (!string.IsNullOrEmpty(reader.GetAttribute("notimeout")))
                                {
                                    _notimeout = true;
                                }

                                //ignore timeouts if the override has been applied to the secondary cache
                                if (!_notimeout)
                                {
                                    long cacheTimeStamp = long.Parse(reader.GetAttribute("timestamp"));
                                    cacheTimeStamp += (long) (_timeOut*TimeSpan.TicksPerSecond);
                                    if (DateTime.Now.Ticks > cacheTimeStamp)
                                    {
                                        return;
                                    }
                                }
                            }

                            #region read a book into the primary cache
                            if (reader.IsStartElement("book"))
                            {
                                Book newBook = new Book();
                                newBook.ItemID = reader.GetAttribute("itemid");
                                newBook.Author = reader.GetAttribute("author");
                                newBook.DetailURL = reader.GetAttribute("detailurl");
                                newBook.LargeImageURL = reader.GetAttribute("largeimageurl");
                                newBook.LowestNewPrice = reader.GetAttribute("lowestnewprice");
                                newBook.LowestUsedPrice = reader.GetAttribute("lowestusedprice");
                                newBook.SmallImageURL = reader.GetAttribute("smallimageurl");
                                newBook.Title = reader.GetAttribute("title");
                                books.Add(newBook.ItemID, newBook);
                            }
                            #endregion

                            #region begin reading a new text search into the primary cache
                            if (reader.IsStartElement("textsearch"))
                            {
                                currentTextSearch = new List<Book>();
                                textSearches.Add(reader.GetAttribute("searchtext"), currentTextSearch);
                            }

                            if (reader.IsStartElement("textsearchbook"))
                            {
                                currentTextSearch.Add(books[reader.GetAttribute("itemid")]);
                            }
                            #endregion

                            #region begin reading a new similarity search into the primary cache
                            if (reader.IsStartElement("similaritysearch"))
                            {
                                currentSimilaritySearch = new List<Book>();
                                similaritySearches.Add(reader.GetAttribute("similaritytext"), currentSimilaritySearch);
                            }

                            if (reader.IsStartElement("similaritysearchbook"))
                            {
                                currentSimilaritySearch.Add(books[reader.GetAttribute("itemid")]);
                            }
                            #endregion

                        }
                    }
                }
            }
        }
Пример #2
0
 private void AddBooks(Book[] books)
 {
     foreach (Book book in books)
     {
         if (!_books.ContainsKey(book.ItemID))
         {
             _books.Add(book.ItemID, book);
         }
     }
 }