/// <summary>
        /// Update the cache within the list of records updated by the transaction
        /// </summary>
        /// <param name="records"></param>
        /// <param name="customerId"></param>
        /// <param name="tick"></param>
        public void UpdateCache(List <Tuple <string, DSRecord, InformationRecord> > records, int customerId, int tick)
        {
            if (!_enable)
            {
                return;
            }

            Lock(customerId); // lock critical section

            // Check if the customer is currently loaded

            if (!_tick.ContainsKey(customerId))
            {
                Unlock(customerId); // unlock critical section
                return;
            }

            try
            {
                // Update the cache within the list of records from the transaction currently executed

                Dictionary <string, Dictionary <int, Tuple <DSRecord, InformationRecord> > > currentTable = _records[customerId];

                foreach (Tuple <string, DSRecord, InformationRecord> record in records)
                {
                    if (!currentTable.ContainsKey(record.Item1))
                    {
                        continue;
                    }

                    currentTable[record.Item1][record.Item2.Id] = Tuple.Create(DSRecord.Copy(record.Item2), InformationRecord.Copy(record.Item3));
                }

                _tick[customerId] = tick;

                if (IsDebug())
                {
                    Debug($"[{customerId}] Cache updated in tick {_tick[customerId]}");
                }
            }
            catch (System.Exception ex)
            {
                Exception($"[{customerId}] Unable to update the cache", ex);
            }

            Unlock(customerId); // unlock critical section
        }
Exemplo n.º 2
0
        /// <summary>
        /// Load all labels if it's necessary
        /// </summary>
        /// <param name="database"></param>
        /// <param name="customerId"></param>
        private void Load(DatabaseContext database, int?customerId)
        {
            int lastUpdate = 0;

            _mutex.Wait(); // lock critical section

            if (customerId == null)
            {
                // Load all tickId for each customer or create it if it doesn't exist

                foreach (CustomerRecord customer in database.Customer.ToList())
                {
                    int tick = 0;

                    // Retrieve the tick of the database

                    string          tickKey   = $"Database.Tick.{customer.Id}";
                    ParameterRecord parameter = database._Parameter.FirstOrDefault(e => e.Key.Equals(tickKey));
                    if (parameter != null)
                    {
                        int.TryParse(parameter.Value, out tick);
                    }

                    // Update or retrieve the tick of the language

                    tickKey   = $"Language.Tick.{customer.Id}";
                    parameter = database._Parameter.FirstOrDefault(e => e.Key.Equals(tickKey));
                    if (parameter == null)
                    {
                        database._Parameter.Add(new ParameterRecord()
                        {
                            Key = tickKey, Value = tick.ToString()
                        });
                    }
                    else
                    {
                        parameter.Value = tick.ToString();
                    }

                    _lastUpdate[customer.Id] = tick;
                }

                database.SaveChanges();
            }
            else
            {
                string          tickKey   = $"Language.Tick.{customerId.Value}";
                ParameterRecord parameter = database._Parameter.SingleOrDefault(e => e.Key.Equals(tickKey));

                if (parameter == null)
                {
                    Warn($"Please, create the parameter '{tickKey}' into the database to avoid loading labels every time!");
                }

                if (parameter != null && !String.IsNullOrWhiteSpace(parameter.Value) && !int.TryParse(parameter.Value, out lastUpdate))
                {
                    lastUpdate = 0;
                }

                if (parameter != null && _lastUpdate.ContainsKey(customerId.Value) && lastUpdate == _lastUpdate[customerId.Value] && lastUpdate >= 0)
                {
                    // no changes ...
                    _mutex.Release(); // unlock critical section
                    return;
                }

                // Update the language tick

                if (parameter != null && lastUpdate < 0)
                {
                    string          dbTickKey     = $"Database.Tick.{customerId.Value}";
                    ParameterRecord parameterTick = database._Parameter.FirstOrDefault(e => e.Key.Equals(dbTickKey));
                    if (parameterTick != null)
                    {
                        parameter.Value = parameterTick.Value;
                        database.SaveChanges();
                    }
                }
            }

            if (customerId == null)
            {
                Info("Loading all labels ...");

                try
                {
                    int i = 0;
                    Dictionary <int, List <LanguageRecord> > newLabels = new Dictionary <int, List <LanguageRecord> >();

                    foreach (LanguageRecord label in database.Language.ToList())
                    {
                        if (!newLabels.ContainsKey(label.CustomerId))
                        {
                            newLabels[label.CustomerId] = new List <LanguageRecord>();
                        }

                        newLabels[label.CustomerId].Add(DSRecord.Copy(label) as LanguageRecord);

                        if (IsDebug() && customerId == null && label.CustomerId == 1)
                        {
                            Debug($"{label.Key.Trim()} = {label}");
                        }

                        i++;
                    }

                    Info($"{i} labels loaded");

                    _labels = newLabels;
                }
                catch (Exception ex)
                {
                    Exception("Unable to load all labels", ex);
                }
            }
            else
            {
                Info($"Loading labels because they recently change ('{lastUpdate}') for the customer '{customerId.Value}' ...");

                try
                {
                    int i = 0;
                    List <LanguageRecord> newLabels = new List <LanguageRecord>();

                    foreach (LanguageRecord label in database.Language.Where(l => l.CustomerId == customerId.Value).ToList())
                    {
                        newLabels.Add(DSRecord.Copy(label) as LanguageRecord);
                        i++;
                    }

                    Info($"{i} labels loaded");

                    _labels[customerId.Value]     = newLabels;
                    _lastUpdate[customerId.Value] = lastUpdate;
                }
                catch (Exception ex)
                {
                    Exception($"Unable to load labels for the customer '{customerId.Value}'", ex);
                }
            }

            _mutex.Release(); // unlock critical section

            if (customerId != null)
            {
                DatabaseCacheManager.Instance.Reload(database, customerId.Value, "Language");
            }

            return;
        }