Esempio n. 1
0
        public bool GetIsCacheExpired()
        {
            var cacheVersionstampFile = ApplicationPath.GetRepositoryDataFile(_client, "cache.versionstamp");

            if (!File.Exists(cacheVersionstampFile))
            {
                return(true);
            }

            try
            {
                using (var file = File.OpenRead(cacheVersionstampFile))
                {
                    var stamp = (CacheVersionStamp)CacheVersionStamp.Serializer.ReadObject(file);
                    if (stamp.GameVersion != _client.Version || stamp.CacheFormatVersion < CurrentCacheFormatVersion)
                    {
                        this.LogInfo("cache version stamp expired");
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                this.LogError("failed to read cache versionstamp: {0}", ex.Message);
                return(true);
            }

            return(false);
        }
Esempio n. 2
0
        internal void UpdateCacheVersionstamp()
        {
            var cacheVersionstampFile = ApplicationPath.GetRepositoryDataFile(_client, "cache.versionstamp");

            try
            {
                using (var file = File.Create(cacheVersionstampFile))
                {
                    CacheVersionStamp.Serializer.WriteObject(file, new CacheVersionStamp(CurrentCacheFormatVersion, _client.Version));
                }
            }
            catch (Exception ex)
            {
                this.LogError("failed to write xml cache versionstamp: {0}", ex.Message);
            }
        }
Esempio n. 3
0
        public T Load <T>(string cacheFilename, Func <T> buildAction, Func <Stream, T> loadAction, Action <Stream, T> saveAction)
        {
            var cacheFile = ApplicationPath.GetRepositoryDataFile(_client, cacheFilename);

            if (!this.IsCacheExpired)
            {
                if (File.Exists(cacheFile))
                {
                    try
                    {
                        using (var file = File.OpenRead(cacheFile))
                        {
                            return(loadAction(file));
                        }
                    }
                    catch (Exception ex)
                    {
                        this.LogError("failed to load cache file '{0}': {1}", cacheFilename, ex.Message);
                    }
                }
            }
            else
            {
                this.LogInfo("cache of '{0}' is expired, gonna rebuild", cacheFilename);
            }

            var result = buildAction();

            try
            {
                using (var file = File.Create(cacheFile))
                {
                    saveAction(file, result);
                }
            }
            catch (Exception ex)
            {
                this.LogError("failed to save cache for '{0}': {1}", cacheFilename, ex.Message);
            }

            return(result);
        }
Esempio n. 4
0
        private T LoadDataContractSerialized <T>(string cacheFilename)
        {
            var cacheFile = ApplicationPath.GetRepositoryDataFile(_client, cacheFilename);

            if (File.Exists(cacheFile))
            {
                try
                {
                    var serializer = new DataContractSerializer(typeof(T));
                    using (var file = File.OpenRead(cacheFile))
                    {
                        return((T)serializer.ReadObject(file));
                    }
                }
                catch (Exception ex)
                {
                    this.LogError("failed to load cache file '{0}': {1}", cacheFilename, ex.Message);
                    return(default(T));
                }
            }

            return(default(T));
        }
Esempio n. 5
0
        public XElement LoadXml(string cacheFilename, Func <XElement> buildAction)
        {
            var cacheFile = ApplicationPath.GetRepositoryDataFile(_client, cacheFilename);

            if (!this.IsCacheExpired)
            {
                if (File.Exists(cacheFile))
                {
                    try
                    {
                        return(XElement.Load(cacheFile));
                    }
                    catch (Exception ex)
                    {
                        this.LogError("failed to load xml cache file '{0}': {1}", cacheFilename, ex.Message);
                    }
                }
            }
            else
            {
                this.LogInfo("xml cache of '{0}' is expired, gonna rebuild", cacheFilename);
            }

            var element = buildAction();

            try
            {
                element.Save(cacheFile);
            }
            catch (Exception ex)

            {
                this.LogError("failed to save xml cache for '{0}': {1}", cacheFilename, ex.Message);
            }

            return(element);
        }