Ejemplo n.º 1
0
        public static void UpdateCache(int localeId = -1, bool fullUpdate = false)
        {
            if (localeId <= 0 && fullUpdate)
            {
                Initialize();
                return;
            }

            if (localeId > 0)
            {
                var locale       = Cache.Locales.Find(i => i.LocaleId == localeId);
                var fingerPrints = ServiceAdapterUtil.GetList(locale.LastUpdated, LocaleId);
                locale.LastUpdated = DateTime.Now.AddSeconds(-15);
                UpdateLocaleCache(locale, fingerPrints);
            }
            else
            {
                var fingerPrints = ServiceAdapterUtil.GetList(Cache.LastFullUpdate);
                foreach (var locale in Cache.Locales)
                {
                    locale.LastUpdated = DateTime.Now.AddSeconds(-15);
                    var pfs = fingerPrints.FindAll(i => i.LocaleId == locale.LocaleId);
                    UpdateLocaleCache(locale, pfs);
                }
                Cache.LastFullUpdate = DateTime.Now.AddSeconds(-15);
            }
            SetupThreadDistribution();
        }
Ejemplo n.º 2
0
        private static void CacheFingerprints()
        {
            var container    = new Dictionary <int, List <MemberFingerprint> >();
            var fingerPrints = new List <MemberFingerprint>();

            Cache = new FingerprintCache();

            var dataDir = "";

            if (BenchmarkMode && Directory.Exists(dataDir = Path.Combine(StartupPath, "Data")))
            {
                var files = Directory.EnumerateFiles(dataDir);
                int i     = 0;
                foreach (var file in files)
                {
                    i++;
                    var print = File.ReadAllBytes(file);
                    fingerPrints.Add(new MemberFingerprint {
                        Id = i, MemberId = i, Fingerprint = print, Size = print.Length
                    });
                }
            }
            else
            {
                if (Mode != MatcherModes.ClientNoCache)
                {
                    if (Mode == MatcherModes.ClientLocaleCache)
                    {
                        fingerPrints = ServiceAdapterUtil.GetList(DateTime.MinValue, LocaleId);
                        if (Cache.Fingerprints.ContainsKey(LocaleId))
                        {
                            Cache.Locales[LocaleId].LastUpdated = DateTime.Now.AddSeconds(-15);
                        }
                    }
                    else if (Mode == MatcherModes.ServerFullCache)
                    {
                        fingerPrints         = ServiceAdapterUtil.GetList(DateTime.MinValue);
                        Cache.LastFullUpdate = DateTime.Now.AddSeconds(-15);
                    }

                    if (SaveTempalates && BenchmarkMode)
                    {
                        if (!Directory.Exists(dataDir))
                        {
                            Directory.CreateDirectory(dataDir);
                        }

                        int i = 0;
                        foreach (var data in fingerPrints)
                        {
                            i++;
                            File.WriteAllBytes(Path.Combine(dataDir, string.Format("{0}.dat", i.ToString("D4"))), data.Fingerprint);
                        }
                    }

                    Cache.Initialize(fingerPrints);

                    if (false && fingerPrints.Count > 0 && BenchmarkMode)
                    {
                        if (!Directory.Exists(dataDir))
                        {
                            Directory.CreateDirectory(dataDir);
                        }

                        int i = 0;
                        foreach (var fp in fingerPrints)
                        {
                            i++;
                            File.WriteAllBytes(Path.Combine(dataDir, string.Format("{0}.dat", i.ToString("D4"))), fp.Fingerprint);
                        }
                    }
                }
            }

            if (BenchmarkMode)
            {
                bool suffleLastRecord = CacheSize > 0;
                var  initialCount     = fingerPrints.Count;

                var last = fingerPrints[initialCount - 1];
                if (suffleLastRecord)
                {
                    fingerPrints[initialCount - 1] = fingerPrints[0];
                }

                var index     = 0;
                var cacheSize = CacheSize > initialCount ? CacheSize : initialCount;
                for (int i = initialCount; i < cacheSize; i++)
                {
                    fingerPrints.Add(fingerPrints[index]);
                    index++;
                    if (index == initialCount)
                    {
                        index = 0;
                    }
                }
                Cache.Samples = fingerPrints;

                if (suffleLastRecord)
                {
                    var random = new Random().Next(0, fingerPrints.Count - 1);
                    fingerPrints[random]    = last;
                    Cache.RandomSampleIndex = random;
                }

                // Set Locales
                var totalThreads = ProcessorCores;
                var fpPerThread  = fingerPrints.Count / totalThreads;

                var currThread = 0;
                var counter    = 0;

                var localeFp = new List <MemberFingerprint>();
                Cache.Locales.Add(new LocaleInfo(currThread));
                Cache.Fingerprints.Add(currThread, localeFp);

                foreach (var item in fingerPrints)
                {
                    if (counter < fpPerThread)
                    {
                        item.LocaleId = currThread;
                        counter++;
                        localeFp.Add(item);
                    }
                    else
                    {
                        counter = 0;
                        currThread++;

                        localeFp = new List <MemberFingerprint>();
                        Cache.Locales.Add(new LocaleInfo(currThread));
                        Cache.Fingerprints.Add(currThread, localeFp);
                        item.LocaleId = currThread;
                    }
                }
            }
        }