public void should_not_add_if_cache_disabled() { // Arrange CacheMock cache = new CacheMock(); ApplicationSettings settings = new ApplicationSettings() { UseObjectCache = false }; List <string> tagCacheItems = new List <string>() { "1", "2" }; ListCache listCache = new ListCache(settings, cache); // Act listCache.Add("all.tags", tagCacheItems); // Assert var tags = listCache.Get <string>("all.tags"); Assert.That(tags, Is.Null); IEnumerable <string> keys = listCache.GetAllKeys(); Assert.That(keys.Count(), Is.EqualTo(0)); }
private void UpdateBattles(int deltaTime) { using (var rmvs = ListCache <string> .Get()) { foreach (var kv in battles) { try { var battle = kv.Value; battle.Update(deltaTime); if (battle.Destroyable) { rmvs.Add(kv.Key); } } catch (Exception ex) { Logger.Exception(ex); } } for (var i = rmvs.Count - 1; i >= 0; i--) { DeleteBattle(rmvs[i]); } } }
/// <summary> /// Retrieves a list of all pages in the system. /// </summary> /// <returns>An <see cref="IEnumerable{PageViewModel}"/> of the pages.</returns> /// <exception cref="DatabaseException">An databaseerror occurred while retrieving the list.</exception> public IEnumerable <PageViewModel> AllPages(bool loadPageContent = false) { try { string cacheKey = ""; IEnumerable <PageViewModel> pageModels; if (loadPageContent) { cacheKey = CacheKeys.AllPagesWithContent(); pageModels = _listCache.Get <PageViewModel>(cacheKey); if (pageModels == null) { IEnumerable <Page> pages = Repository.AllPages().OrderBy(p => p.Title); pageModels = from page in pages select new PageViewModel(Repository.GetLatestPageContent(page.Id), _markupConverter); _listCache.Add <PageViewModel>(cacheKey, pageModels); } } else { cacheKey = CacheKeys.AllPages(); pageModels = _listCache.Get <PageViewModel>(cacheKey); if (pageModels == null) { IEnumerable <Page> pages = Repository.AllPages().OrderBy(p => p.Title); pageModels = from page in pages select new PageViewModel() { Id = page.Id, Title = page.Title }; _listCache.Add <PageViewModel>(cacheKey, pageModels); } } return(pageModels); } catch (DatabaseException ex) { throw new DatabaseException(ex, "An error occurred while retrieving all pages from the database"); } }
/// <summary> /// Gets alls the pages created by a user. /// </summary> /// <param name="userName">Name of the user.</param> /// <returns>All pages created by the provided user, or an empty list if none are found.</returns> /// <exception cref="DatabaseException">An databaseerror occurred while retrieving the list.</exception> public IEnumerable <RelViewModel> AllRelsCreatedBy(string userName) { try { string cacheKey = string.Format("allrels.createdby.{0}", userName); IEnumerable <RelViewModel> models = _listCache.Get <RelViewModel>(cacheKey); if (models == null) { IEnumerable <Relationship> rels = Repository.FindRelsCreatedBy(userName); models = from rel in rels select new RelViewModel(Repository.GetRelById(rel.id)); } return(models); } catch (DatabaseException ex) { throw new DatabaseException(ex, "An error occurred while retrieving all relationships created by {0} from the database", userName); } }
public override ICharSequence GetItemTextFormatted(int pos) { if (pos == 0) { return(new String(_context.GetString(Resource.String.categoryAll))); } var item = _cache.Get(pos - 1); return(item == null ? new String() : new String(item.Name)); }
/// <summary> /// Gets alls the pages created by a user. /// </summary> /// <param name="userName">Name of the user.</param> /// <returns>All pages created by the provided user, or an empty list if none are found.</returns> /// <exception cref="DatabaseException">An databaseerror occurred while retrieving the list.</exception> public IEnumerable <PageViewModel> AllPagesCreatedBy(string userName) { try { string cacheKey = string.Format("allpages.createdby.{0}", userName); IEnumerable <PageViewModel> models = _listCache.Get <PageViewModel>(cacheKey); if (models == null) { IEnumerable <Page> pages = Repository.FindPagesCreatedBy(userName); models = from page in pages select new PageViewModel(Repository.GetLatestPageContent(page.Id), _markupConverter); _listCache.Add <PageViewModel>(cacheKey, models); } return(models); } catch (DatabaseException ex) { throw new DatabaseException(ex, "An error occurred while retrieving all pages created by {0} from the database", userName); } }
/// <summary> /// 获得视野范围内距离最近的敌人 /// </summary> /// <returns></returns> public Unit FindNearestEnemyInFov() { Unit target = null; using (var enemies = ListCache <Unit> .Get()) { this.GetAllLivingEnemys(enemies); for (int i = enemies.Count - 1; i >= 0; i--) { target = GetNearerUnitInFov(target, enemies[i]); } } return(target); }
public void Should_Get_Item() { // Arrange CacheMock cache = new CacheMock(); ApplicationSettings settings = new ApplicationSettings() { UseObjectCache = true }; List<string> tagCacheItems = new List<string>() { "1", "2" }; AddToCache(cache, "all.tags", tagCacheItems); ListCache listCache = new ListCache(settings, cache); // Act var tags = listCache.Get<string>("all.tags"); // Assert Assert.That(tags, Is.EqualTo(tagCacheItems)); }
/// <summary> /// 读取缓存的ids(缺省读取前1000个) /// </summary> /// <param name="bean"></param> /// <param name="orderby"></param> /// <param name="count"></param> /// <returns></returns> public long[] GetCacheIds(T bean, string orderby = null, uint count = 1000) { if (bean == null) { throw new Exception("bean 不能为 NULL"); } long[] ids = null; string ck = "ids_" + CombineCacheKey(bean, orderby, count); if (ListCache.Get(ck, ref ids)) { return(ids); } if (!_lock.Add(ck)) { System.Threading.Thread.Sleep(5); if (ListCache.Get(ck, ref ids)) { return(ids); } } try { ids = GetIds(bean, orderby, count); if (ids == null) { ListCache.Add(ck, null, 5); } else { ListCache.Add(ck, ids); } } finally { _lock.Remove(ck); } return(ids); }
private void OnCategorySelected(object sender, WearableNavigationDrawerView.ItemSelectedEventArgs e) { if (e.Pos > 0) { var category = _categoryCache.Get(e.Pos - 1); if (category == null) { return; } _authSource.SetCategory(category.Id); } else { _authSource.SetCategory(null); } _authListAdapter.NotifyDataSetChanged(); UpdateViewState(); }
public void should_get_item() { // Arrange CacheMock cache = new CacheMock(); ApplicationSettings settings = new ApplicationSettings() { UseObjectCache = true }; List <string> tagCacheItems = new List <string>() { "1", "2" }; AddToCache(cache, "all.tags", tagCacheItems); ListCache listCache = new ListCache(settings, cache); // Act var tags = listCache.Get <string>("all.tags"); // Assert Assert.That(tags, Is.EqualTo(tagCacheItems)); }
/// <summary> /// 读取缓存的ids(缺省读取前1000个) /// </summary> /// <param name="orderby"></param> /// <param name="count"></param> /// <returns></returns> public long[] GetCacheIds(string orderby = null, uint count = 1000) { long[] ids = null; string ck = "ids_" + CombineCacheKey(orderby, count); if (ListCache.Get(ck, ref ids)) { return(ids); } if (!_lock.Add(ck)) { System.Threading.Thread.Sleep(5); if (ListCache.Get(ck, ref ids)) { return(ids); } } try { ids = GetIds(orderby, count); if (ids == null) { ListCache.Add(ck, null, 5); } else { ListCache.Add(ck, ids); } } finally { _lock.Remove(ck); } return(ids); }
/// <summary> /// 读取缓存的ids(缺省读取前1000个) /// </summary> /// <param name="cond"></param> /// <param name="from"></param> /// <returns></returns> public long[] GetCacheIds(Sql cond = null) { long[] ids = null; string ck = "ids_" + CombineCacheKey(cond); if (ListCache.Get(ck, ref ids)) { return(ids); } if (!_lock.Add(ck)) { System.Threading.Thread.Sleep(5); if (ListCache.Get(ck, ref ids)) { return(ids); } } try { ids = GetIds(cond); if (ids == null) { ListCache.Add(ck, null, 5); } else { ListCache.Add(ck, ids); } } finally { _lock.Remove(ck); } return(ids); }
public void Should_Not_Add_If_Cache_Disabled() { // Arrange CacheMock cache = new CacheMock(); ApplicationSettings settings = new ApplicationSettings() { UseObjectCache = false }; List<string> tagCacheItems = new List<string>() { "1", "2" }; ListCache listCache = new ListCache(settings, cache); // Act listCache.Add("all.tags", tagCacheItems); // Assert var tags = listCache.Get<string>("all.tags"); Assert.That(tags, Is.Null); IEnumerable<string> keys = listCache.GetAllKeys(); Assert.That(keys.Count(), Is.EqualTo(0)); }