public void TestAddCache() { cache.AddItem("UnitTest2", "hello", 10); var item = cache.GetItem <string>("UnitTest2"); Assert.AreEqual("hello", item); cache.DelItem("UnitTest2"); cache.AddItem("UnitTest3", "hello", 1); Thread.Sleep(2000); item = cache.GetItem <string>("UnitTest3"); Assert.IsNull(item); }
public bool Save() { ISqlMapper mapper = MapperHelper.GetMapper(); WorkflowDefinitionDao wddao = new WorkflowDefinitionDao(mapper); ActivityDefinitionDao addao = new ActivityDefinitionDao(mapper); LinkDefinitionDao linkdao = new LinkDefinitionDao(mapper); ActivityAuthDefinitionDao aadd = new ActivityAuthDefinitionDao(mapper); var workflowdefinition = wddao.Query(new WorkflowDefinitionQueryForm { ID = this.value.ID }).FirstOrDefault(); if (workflowdefinition == null) { wddao.Add(this.value); } else { wddao.Update(new WorkflowDefinitionUpdateForm { Entity = this.value, WorkflowDefinitionQueryForm = new WorkflowDefinitionQueryForm { ID = this.value.ID } }); } var activityList = this.Root.GetList(); foreach (var a in activityList) { var acitivitydefinitioninstance = a as ActivityDefinitionModel; acitivitydefinitioninstance.Save(mapper); } foreach (var link in LinkDefinitionList) { link.Save(mapper); } var item = cache.GetItem(this.value.ID); if (item == null) { item = new CacheItem(this.value.ID, this); cache.AddItem(item, 30 * 60); } else { item.Value = this; cache.UpdateItem(item); } return(true); }
public QuoteBasicBase Load(string symbol, int interval, long?startTime, int maxCount = 500) { lock (this) { var files = this.FindQuoteFiles(symbol, interval); if (files == null || files.Count <= 0) { return(null); } var quotes = new List <QuoteBasicBase>(); var quote = new QuoteBasicBase(symbol, interval); files.Sort(); var count = 0; for (int i = files.Count - 1; i >= 0; i--) { var q = _cache.GetItem(files[i]); if (q == null) { q = QuoteBasicFileStore.LoadQuote(files[i]); _cache.AddItem(files[i], q); } quotes.Add(q); count += q.Count; if (startTime.HasValue && q.FirstTime <= startTime) { break; } if (count >= maxCount) { break; } } for (int i = 0; i < quotes.Count; i++) { quote.Append(quotes[i], false); } var ind1 = startTime.HasValue ? quote.FindIndexForGivenTime(startTime.Value) : 0; var ind2 = quote.Count - maxCount; var index = Math.Max(0, Math.Max(ind1, ind2)); if (index != 0) { return(quote.Extract(index, quote.Count - 1) as QuoteBasicBase); } else { return(quote); } } }
/// <summary> /// Return stale tracked item and update last access time /// </summary> /// <param name="key">key of item</param> /// <returns>The item if it is available otherwise null</returns> private object AccessItem(string key) { var item = cache.GetItem(key) as CacheElement; object result = null; if (item != null) { item.UpdateLastAccess(); cache.AddItem(key, DefaultTtl, item); result = item.Data; } return(result); }
public LogonResultForm GetUserInfo(string userid) { var mapper = Common.GetMapperFromSession(); UserDao userdao = new UserDao(mapper); var user = userdao.Query(new UserQueryForm { Name = userid }).FirstOrDefault(); if (user == null) { throw new Exception("用户:" + userid + "在系统中不存在!"); } if (user.Enabled == 0) { throw new Exception("该用户已被禁用,请联系管理员!"); } LogonResultForm result = new LogonResultForm(); UserInfoDao userInfoDao = new UserInfoDao(mapper); RoleDao roleDao = new RoleDao(mapper); LogonHistoryDao historyDao = new LogonHistoryDao(mapper); string token = Guid.NewGuid().ToString().Replace("-", ""); var userinfo = userInfoDao.Query(new UserInfoQueryForm { ID = user.ID }).FirstOrDefault(); UserEntireInfo u = new UserEntireInfo { User = user }; if (userinfo != null) { u.UserInfo = userinfo; } u.Role = roleDao.QueryRoleByUserID(u.User.ID); LogonHistory history = new LogonHistory { LogonTime = DateTime.Now, Token = token, UserID = user.ID, ActiveTime = DateTime.Now, }; historyDao.Add(history); result.token = token; result.UserInfo = userinfo; cache.AddItem(token, u, 30 * 60); MenuBLL menubll = new MenuBLL(); result.Menu = menubll.GetCurrentUserMenu(result.token); return(result); }
public void InsertMongoInfo() { RegisterDependency.Init(); ICache mongoDb = ServiceLocator.Current.GetInstance <ICache>(); var data = new ObjTest(); data.Clave = "clave"; data.Valor = "Vaqlor"; mongoDb.AddItem(key, data, ""); var data1 = (ObjTest)(mongoDb.GetData(key)); Assert.AreEqual(data1.Valor, data.Valor); }
public void SetItem(string key, T t) { var item = _cacheHandler.GetItem <Dictionary <string, T> >(_cacheKey); Dictionary <string, T> dicCache; if (item == null) { dicCache = new Dictionary <string, T>(); dicCache[key] = t; _cacheHandler.AddItem(_cacheKey, dicCache, -1); } else { dicCache = item; dicCache[key] = t; _cacheHandler.UpdateItem(_cacheKey, dicCache, -1); } }
public string Logon(string username, string password) { UserDao dao = new UserDao(); var users = dao.Query(new UserQueryForm { Name = username, Password = password }); if (users.Count > 0) { string token = Guid.NewGuid().ToString().Replace("-", ""); CacheItem item = new CacheItem(token, users[0]); cache.AddItem(item, 30 * 60); return(token); } else { return(null); } }
public async Task <RestaurantInfoResponse> GetRestaurantInfo(string postCode) { try { if (string.IsNullOrWhiteSpace(postCode)) { _logger.LogError("postCode is null {postCode}", postCode); return(null); } var response = new RestaurantInfoResponse(); //check if item exists in redis var cache = await GetFromCache(postCode); if (cache != null) { return(response = cache); } //Get info from info provider var restaurant = await _restaurant.GetRestaurantInfoByPostCode(postCode); if (restaurant != null && restaurant.MetaData.ResultCount > 0) { response.Datas.AddRange(restaurant.Restaurants.Select(x => new InfoData() { Name = x.Name, Rating = x.Rating, FoodTypes = x.CuisineTypes }).ToList()); await _cache.AddItem(postCode, JsonConvert.SerializeObject(response), TimeSpan.FromMinutes(30).TotalSeconds); return(response); } return(response); } catch (Exception ex) { _logger.LogError(ex, "Error occured in GetRestaurantInfo"); return(null); } }
public static void SetTokenIntoCache(string token) { _cache.AddItem(UrlConfig.TokenCacheName, token, 7200); }
public static List <TEntity> GetDataFromCache <TEntity>(Type daoType) where TEntity : SimpleEntity { string key = typeof(TEntity).FullName; ICache cache = CacheFactory.Create(); var item = cache.GetItem(key); List <TEntity> list = null; if (item == null) { list = Query <TEntity>(daoType); if (list.Count == 0) { return(list); } CacheEntity <TEntity> cacheentity = new CacheEntity <TEntity>(); cacheentity.List = list; cacheentity.LastUpdateTime = list[0].LastUpdateTime; foreach (var data in list) { if (cacheentity.LastUpdateTime < list[0].LastUpdateTime) { cacheentity.LastUpdateTime = list[0].LastUpdateTime; } } item = new CacheItem(key, cacheentity); cache.AddItem(item, 60 * 30); } else { object dao = Activator.CreateInstance(daoType, null); string tableName = typeof(TEntity).Name; var method = daoType.GetMethod("QueryMaxLastUpdateTime"); object result = method.Invoke(dao, null); CacheEntity <TEntity> cacheentity = item.Value as CacheEntity <TEntity>; DateTime?lastupdatetime = result as DateTime?; if (cacheentity.LastUpdateTime < lastupdatetime) { list = cacheentity.List; var newupdate = Query <TEntity>(daoType, new SimpleQueryForm { LastUpdateTime_Start = cacheentity.LastUpdateTime }); Type itemType = typeof(TEntity); PropertyInfo deleteProperty = itemType.GetProperty("IsDeleted", BindingFlags.Instance | BindingFlags.Public); foreach (var newitem in newupdate) { var olditem = list.Find(t => t.ID == newitem.ID); if (olditem != null) { if (deleteProperty != null) { bool deleted = Convert.ToBoolean(deleteProperty.GetValue(newitem, null)); if (deleted) { list.Remove(olditem); } } else { olditem = newitem; } } else { list.Add(newitem); } } cacheentity.LastUpdateTime = lastupdatetime; item.Value = cacheentity; cache.UpdateItem(item); } else { list = cacheentity.List; } } return(list); }
private void GetData(object criteria) { var key = Getkey(criteria); ICache cache = ServiceLocator.Current.GetInstance <ICache>(); if (cache.Contains(key)) { var objCache = (CacheListBase <TCr>)cache.GetData(key); _totalRegistros = objCache.TotalRegistros; foreach (var cr in objCache.Data) { Items.Add(cr); } } else { //crear la conexion a la base if (Db == null) { Db = DatabaseFactory.CreateDatabase(); } Comando = Db.CreateSPCommand(NombreProcedimiento); //setear los parametros MethodInfo methodInfo = GetType() .GetMethod(NombreMetodo, BindingFlags.NonPublic | BindingFlags.Instance, Type.DefaultBinder, new[] { criteria.GetType() }, null); if (methodInfo != null) { methodInfo.Invoke(this, new [] { criteria }); } else { if (criteria is int) { Db.AddParameterWithValue(Comando, "en_id", DbType.Int32, criteria); } else if (criteria is string) { Db.AddParameterWithValue(Comando, "ec_codigo", DbType.String, criteria); } else { throw new JusException( String.Format("No se implemento el metodo {0}, Ej: 'private void {0}({1} criteria)'", NombreMetodo, criteria.GetType())); } } Db.AddParameter(Comando, "sn_total_registros", DbType.Int32, ParameterDirection.Output); Db.AddParameter(Comando, "sq_resultado", DbType.Object, ParameterDirection.Output); using (IDataReader dr = Db.ExecuteDataReader(Comando)) { List <TCr> listaInfo = new List <TCr>(); Int32.TryParse(Db.GetOutputParameterValue(Comando, "sn_total_registros").ToString(), out _totalRegistros); while (dr.Read() && listaInfo.Count < 500) { listaInfo.Add(JusReadOnlyBase <TCr> .Get(dr)); } foreach (var info in OrdenarList(listaInfo)) { Add(info); } } if (Items.Count > 1) { var grupo = RootClass.Aggregate(string.Empty, (current, type) => current + type + ','); cache.AddItem(key, new CacheListBase <TCr> { Data = Items, TotalRegistros = _totalRegistros }, grupo.Trim(',')); } } }