//更新职工类型 public void setSTAFF() { string strSql = "select t.id,t.category,t.dep_id from STAFF t where t.status=1 "; DataSet set = sys_oracle.DbHelperOra.Query(strSql); if (set != null) { //foreach(DataRow dr in set.Tables[0].Rows) //{ // string strKey = "ls:"+dr["dep_id"] + ""; // StackExchangeRedisHelper.Set(strKey, dr["category"] + ""); //更新 //} //{"code":200,"error":"","data": DataTable dt_all = set.Tables[0]; List <string> list = GetDistinctSelf(set.Tables[0], "dep_id"); foreach (string str in list) { DataRow[] dr_tmp = dt_all.Select($"dep_id ='{str}'"); string strJsonData = JsonConvert.SerializeObject(ToDataTable(dr_tmp), new DataTableConverter()); string strValue = $"{{\"code\":200,\"error\":\"\",\"data\":{strJsonData}}}"; string strKey = "ls:" + str; StackExchangeRedisHelper.Set(strKey, strValue); //更新 } } }
public override async Task OnConnected() { log.Info($"MessageHub-OnConnected {Context.ConnectionId}"); try { IDBHelper db = new MssqlHelper(StaticObject.ConnectionStrings["Kf"]); var data = await db.FindOneAsync <CustomerServiceOnline>($"SELECT TOP 1 * FROM CustomerServiceOnlines WHERE ConnectId='{Context.ConnectionId}'", null, false); if (data != null) { await db.ExcuteNonQueryAsync($"Insert INTO CustomerInOutLogs (CustomerId,InOutState,CreationTime) VALUES ({data.Id},{(int)InOutState.Login},GetDate())", null, false); await db.ExcuteNonQueryAsync($"Update CustomerServiceOnlines SET ConnectState={(int)ConnectState.Connect} WHERE ConnectId='{Context.ConnectionId}'", null, false); data.ConnectState = (int)ConnectState.Connect; //memberCache.Set(data.OpenID, data, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(StaticObject.CacheDictionary[StaticObject.Cache_Kf_OpenId2Customer])); //redisCache.RemoveValue(data.OpenID, StaticObject.Cache_OpenId2Customer); await StackExchangeRedisHelper.Set(data.OpenID, StaticObject.Cache_Kf_OpenId2Customer, JsonConvert.SerializeObject(data), StaticObject.CacheDictionary[StaticObject.Cache_Kf_OpenId2Customer]); await StackExchangeRedisHelper.Remove(data.OpenID, StaticObject.Cache_OpenId2Customer); } await base.OnConnected(); } catch (Exception ex) { log.Error("MessageHub-OnConnected错误:", ex); throw ex; } }
public T Get <T>(string key, bool isDelay = false, int seconds = 30) { if (isDelay) { StackExchangeRedisHelper.Instance().SetExpire(key, seconds); } return(StackExchangeRedisHelper.Instance().Get <T>(key)); }
/// <summary> /// 跳过式调用,如果事情正在被调用,直接跳过 /// </summary> /// <param name="resource">锁定资源的标识</param> /// <param name="expiryTime">锁过期时间</param> /// <param name="work"></param> public static bool OverlappingWork(string resource, TimeSpan expiryTime, Action work) { var redLock = StackExchangeRedisHelper.Instance().GetRedlock(); Lock lockObject; bool locked; GetLockObjetc(resource, expiryTime, new TimeSpan(0), redLock, out lockObject, out locked); LockExec(work, redLock, lockObject, locked); return(locked); }
public bool Set(string key, string value, int seconds = 30) { bool flag = StackExchangeRedisHelper.Instance().Set(key, value); if (seconds > 0) { StackExchangeRedisHelper.Instance().SetExpire(key, seconds); } return(flag); }
//private RedisHelper redisCache = RedisHelper.GetInstance(StaticObject.RedisServer, StaticObject.RedisPort ?? 0, StaticObject.RedisPassword, StaticObject.RedisDb ?? 0); //private WebCacheHelper memberCache = new WebCacheHelper(); public async Task applyToken() { var token = Guid.NewGuid().ToString(); //memberCache.Set(token, Context.ConnectionId, DateTime.Now.AddSeconds(StaticObject.CacheDictionary[StaticObject.Cache_Kf_LoginToken]), TimeSpan.Zero); await StackExchangeRedisHelper.Set(token, StaticObject.Cache_Kf_LoginToken, Context.ConnectionId, StaticObject.CacheDictionary[StaticObject.Cache_Kf_LoginToken]); await Clients.Client(Context.ConnectionId).getToken(token); }
public async Task setState(string messageToken, int state) { try { var customer = await GetCustomer(messageToken); if (customer != null) { IDBHelper db = new MssqlHelper(StaticObject.ConnectionStrings["Kf"]); await db.ExcuteNonQueryAsync($"Update CustomerServiceOnlines SET OnlineState={state},ConnectId='{Context.ConnectionId}',ConnectState={(int)ConnectState.Connect} WHERE ID='{customer.Id}'", null, false); customer.OnlineState = state; customer.ConnectId = Context.ConnectionId; customer.ConnectState = 1; //memberCache.Set(messageToken, customer.OpenID, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(StaticObject.CacheDictionary[StaticObject.Cache_Kf_MessageToken2CustomerOpenId])); //memberCache.Set(customer.OpenID, customer, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(StaticObject.CacheDictionary[StaticObject.Cache_Kf_OpenId2Customer])); //redisCache.RemoveValue(customer.OpenID, StaticObject.Cache_OpenId2Customer); await StackExchangeRedisHelper.Set(messageToken, StaticObject.Cache_Kf_MessageToken2CustomerOpenId, customer.OpenID, StaticObject.CacheDictionary[StaticObject.Cache_Kf_MessageToken2CustomerOpenId]); await StackExchangeRedisHelper.Set(customer.OpenID, StaticObject.Cache_Kf_OpenId2Customer, JsonConvert.SerializeObject(customer), StaticObject.CacheDictionary[StaticObject.Cache_Kf_OpenId2Customer]); await StackExchangeRedisHelper.Remove(customer.OpenID, StaticObject.Cache_OpenId2Customer); var a = -1; if (state == (int)OnlineState.OnLine) { a = (int)InOutState.online; } if (state == (int)OnlineState.Leave) { a = (int)InOutState.leave; } if (state == (int)OnlineState.Quit) { a = (int)InOutState.quit; } await db.ExcuteNonQueryAsync($"Insert INTO CustomerInOutLogs (CustomerId,InOutState,CreationTime) VALUES ({customer.Id},{a},GetDate())", null, false); } } catch (Exception ex) { log.Error("MessageHub-setState错误:", ex); throw; } }
public void DeleteUser(string key) { var redisHelper = new StackExchangeRedisHelper(); conn = redisHelper.SafeConn;//create safe connection var database = conn.GetDatabase(); if (redisHelper.DeleteKey(database, key)) { Console.WriteLine("Delete OK"); } else { Console.WriteLine("Delete failed"); } }
private void muilThreadSetClass(List <string> list, int start, int end) { // foreach (string str in list) for (; start < end; start++) { try { string strSql = $"select wm_concat(grade_no) grade_no from (select d.value grade_no from SYS_DICTIONARY d left join CLASS_NO cn on cn.grade_no = d.value where cn.status = 1 and dep_id = '{list[start]}' group by d.value,d.code order by to_number(d.code))";//得到排序后的聚合 DataTable dt = sys_oracle.DbHelperOra.Query(strSql).Tables[0]; // string strJsonData = JsonConvert.SerializeObject(dt, new DataTableConverter()); string strValue = $"{{\"code\":200,\"error\":\"\",\"data\":{dt.Rows[0]["grade_no"]}}}"; string strKey = "nj:" + list[start]; StackExchangeRedisHelper.Set(strKey, strValue); //更新 Thread.Sleep(10); } catch { } } }
public void Update(string key, string name, DateTime birthday) { var redisHelper = new StackExchangeRedisHelper(); conn = redisHelper.SafeConn;//create safe connection IDatabase database = conn.GetDatabase(); var data = redisHelper.Get <User>(database, key); if (data == null) { Console.WriteLine("No Data"); } else { data.Name = name; data.Birthday = birthday; redisHelper.Add <User>(database, key, data); } }
private async Task <CustomerServiceOnline> GetCustomer(string messageToken) { if (!string.IsNullOrEmpty(messageToken)) { //获取messageToken对应的客服 //var openid = memberCache.Get<string>(messageToken); var openid = await StackExchangeRedisHelper.Get(messageToken, StaticObject.Cache_Kf_MessageToken2CustomerOpenId); if (!string.IsNullOrEmpty(openid)) { //检查messageToken是否过期(客服1天之内可能会多次登录,识别messagetoken是否为客服最后一次登录生成) //var newCustomer = memberCache.Get<CustomerServiceOnline>(openid); var newCustomer = JsonConvert.DeserializeObject <CustomerServiceOnline>(await StackExchangeRedisHelper.Get(openid, StaticObject.Cache_Kf_OpenId2Customer)); if (newCustomer != null && newCustomer.MessageToken == messageToken) { return(newCustomer); } } } return(null); }
public void GetUser(RedisKey[] keys) { var redisHelper = new StackExchangeRedisHelper(); conn = redisHelper.SafeConn;//create safe connection var database = conn.GetDatabase(); var datas = redisHelper.Get <User>(database, keys); if (datas == null) { Console.WriteLine("No Data"); } else { foreach (var data in datas) { Console.WriteLine("ID:{0} ,Name:{1}, Birthday:{2}, Sex:{3}", data.ID, data.Name, data.Birthday.ToShortDateString(), data.Sex); } } }
//得到学校及对应的区域 public void setSchoolInfo() { try { string strSql = "select t.id,t.name,t.code,t.province,t.city,t.county,t.nature from SYS_DEP t where t.status=1"; DataSet set = sys_oracle.DbHelperOra.Query(strSql); if (set != null) { foreach (DataRow dr in set.Tables[0].Rows) { // string strJsonData = JsonConvert.SerializeObject(dr, new DataTableConverter()); string strJsonData = $"{{\"id\":\"{dr["id"]}\",\"name\":\"{dr["name"]}\",\"code\":\"{dr["code"]}\",\"province\":\"{dr["province"]}\",\"city\":\"{dr["city"]}\",\"county\":\"{dr["county"]}\",\"nature\":\"{dr["nature"]}\"}}"; // string strJsonData = JsonConvert.SerializeObject(); string strValue = $"{{\"code\":200,\"error\":\"\",\"data\":{strJsonData}}}"; string strKey = "xx:" + dr["id"]; StackExchangeRedisHelper.Set(strKey, strValue); //更新 Thread.Sleep(10); } } } catch (Exception ex) { } }
public Redis(QueueConfig queueConfig) { client = new StackExchangeRedisHelper(""); _queueConfig = queueConfig; }
public bool Delete(string key) { StackExchangeRedisHelper.Instance().Remove(key); return(true); }
public void Insert() { var seRedisHelper = new StackExchangeRedisHelper(); conn = seRedisHelper.SafeConn;//create safe connection if (conn == null || !conn.IsConnected) { Console.WriteLine("No connection is available"); return; } var database = conn.GetDatabase(); var user1 = new User { ID = 1, Name = "rico", Birthday = DateTime.Parse("1982-01-01 12:12:12"), Sex = true }; var user2 = new User { ID = 2, Name = "sherry", Birthday = DateTime.Parse("1982-02-01 12:12:12"), Sex = false }; var user3 = new User { ID = 3, Name = "fifi", Birthday = DateTime.Parse("1982-03-01 12:12:12"), Sex = false }; //key要注意大小寫 if (seRedisHelper.Add(database, "User" + user1.ID, user1)) { Console.WriteLine("User" + user1.ID + " has inserted"); } else { Console.WriteLine("User" + user1.ID + " insert failed"); } if (seRedisHelper.Add(database, "User" + user2.ID, user2)) { Console.WriteLine("User" + user2.ID + " has inserted"); } else { Console.WriteLine("User" + user2.ID + " insert failed"); } if (seRedisHelper.Add(database, "User" + user3.ID, user3)) { Console.WriteLine("User" + user3.ID + " has inserted"); } else { Console.WriteLine("User" + user3.ID + " insert failed"); } }