private void WithLock(Action <StackExchange.Redis.IDatabase> action) { var redisDB = redis.GetDatabase(0); string lockId = Guid.NewGuid().ToString("N"); while (redisDB.LockTake($"{this.redisDownloadListKey}-lock", lockId, TimeSpan.FromSeconds(20)) == false) { System.Threading.Thread.Sleep(100); } action(redisDB); redisDB.LockRelease($"{this.redisDownloadListKey}-lock", lockId); }
/// <summary> /// Obtenir du cache partagé /// </summary> /// <param name="DirectInvoiceid"></param> /// <returns></returns> public DirectInvoice GetDirectInvoice(string iddirectinvoice) { DirectInvoice retour = null; try { // Obtien d'abord l'objet sur MemCached si Existe if (env != null) { StackExchange.Redis.ConfigurationOptions redisconfig = new StackExchange.Redis.ConfigurationOptions(); redisconfig.EndPoints.Add("22ec9a54-2921-43af-a4e9-9b1e7aff2b9b.pdb.ovh.net", 21784); redisconfig.Password = "******"; StackExchange.Redis.ConnectionMultiplexer redis = StackExchange.Redis.ConnectionMultiplexer.Connect(redisconfig); StackExchange.Redis.IDatabase redisDb = redis.GetDatabase(); StackExchange.Redis.RedisKey key = iddirectinvoice; StackExchange.Redis.RedisValue val = redisDb.StringGet(key, StackExchange.Redis.CommandFlags.None); if (!string.IsNullOrWhiteSpace(val)) { retour = new DirectInvoice(); retour.FromStringData(val); } } // sinon regarde en base if (retour == null) { Dictionary <string, object> ins = new Dictionary <string, object>(); ins.Add("iddirectinvoice", iddirectinvoice); retour = this.GetOneDefault <DirectInvoice>(ins); } return(retour); } catch (Exception ex) { throw new Exception("GetDirectInvoice " + ex.Message, ex); } }
/// <summary> /// enregistre en base et dans le cache partagé /// </summary> public void SaveDirectInvoice(DirectInvoice directinvoice) { try { // Enregistrement des clef (données persistantes) en bases System.Data.DataRowState rowstate = directinvoice.GetRow().RowState; if (rowstate == System.Data.DataRowState.Detached || rowstate == System.Data.DataRowState.Added) { this.InsertBubble(directinvoice, true, false); } else { this.SaveBubble(directinvoice); } // Enregistrement des données non persistantes sur MemCached if (env != null) { StackExchange.Redis.ConnectionMultiplexer redis = StackExchange.Redis.ConnectionMultiplexer.Connect(GetRedisConfig()); StackExchange.Redis.IDatabase redisDb = redis.GetDatabase(); StackExchange.Redis.RedisValue val = directinvoice.ToStringData(); StackExchange.Redis.RedisKey key = directinvoice.IdDirectInvoice; redisDb.StringSet(key, val, null, StackExchange.Redis.When.Always, StackExchange.Redis.CommandFlags.None); // redng75Oj82p // !!! stocker dans memecached } } catch (Exception ex) { throw new Exception("SaveDirectInvoice " + ex.Message, ex); } }
public RedisDal() { _db = _cm.GetDatabase(Properties.Settings.Default.RedisDatabase); }