예제 #1
0
        public bool ObjectHasChanged <T>(string key, string type, T obj)
        {
            if (obj == null)
            {
                return(true);
            }
            key = key.ToLower();
            string objSer = obj.Serialize <T>();

            if (String.IsNullOrEmpty(objSer))
            {
                return(true);
            }

            CacheObj foundCacheObj = ObjectHash.Find(a => a.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase) && a.Type.Equals(type, StringComparison.InvariantCultureIgnoreCase));

            if (foundCacheObj == null)
            {
                ObjectHash.Add(new CacheObj()
                {
                    Key = key, Type = type, Hash = GetSHA1Hash(objSer, Encoding.UTF8).ToString()
                });
                return(true);
            }
            else if (foundCacheObj.Hash != GetSHA1Hash(objSer, Encoding.UTF8).ToString())
            {
                foundCacheObj.Hash = GetSHA1Hash(objSer, Encoding.UTF8).ToString();
                return(true);
            }
            return(false);
        }
예제 #2
0
        public bool RemoveFromCache(string key, string type)
        {
            CacheObj foundCacheObj = ObjectHash.Find(a => a.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase) && a.Type.Equals(type, StringComparison.InvariantCultureIgnoreCase));

            if (foundCacheObj != null)
            {
                ObjectHash.Remove(foundCacheObj);
            }
            return(true);
        }
        private static void loadCacheObjFromFile()
        {
            var path = Path.Combine(AssemblyUtils.getCurrentDirectory(), "hashcache.json");

            if (File.Exists(path))
            {
                cache = JsonConvert.DeserializeObject <CacheObj>(File.ReadAllText(path), new JsonSerializerSettings()
                {
                    ObjectCreationHandling = ObjectCreationHandling.Replace
                });
            }
        }
예제 #4
0
        public void Add(string key, object value, DateTime expire)
        {
            if (expire == default(DateTime))
            {
                return;
            }

            var cache = new CacheObj <object> {
                Expire = expire, Value = value
            };

            _dict[key] = cache;
        }
예제 #5
0
        private static async Task <string> GetData(string url)
        {
            if (Cache.TryGetValue(url, out var cache) && cache.Valid)
            {
                return(cache.Data);
            }
            var request = CreateRequest(url);

            using (var response = (HttpWebResponse)await request.GetResponseAsync())
                using (var responseStream = response.GetResponseStream())
                    using (var reader = new StreamReader(responseStream))
                    {
                        var data = reader.ReadToEnd();
                        Cache[url] = new CacheObj(data);
                        return(data);
                    }
        }
예제 #6
0
        public static async Task <bool> IsStreaming(int userId)
        {
            try
            {
                var data = await GetData(Urls.Stream(userId));

                var  dynData     = JsonConvert.DeserializeObject <dynamic>(data.Data);
                bool isStreaming = dynData?.stream != null;
                if (isStreaming)
                {
                    Cache[StreamDataKey] = new CacheObj(data);
                }
                return(isStreaming);
            }
            catch (Exception e)
            {
                Log.Error(e);
                return(false);
            }
        }
예제 #7
0
        /// <summary>
        /// 执行前读取站点配置信息
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            CacheObj obj = cacheService.Get(Constant.CacheKey.SiteConfigCacheKey);

            this.Config = (obj != null && obj.value != null) ? (obj.value as SiteConfig) : null;
            if (this.Config == null)
            {
                this.Config = siteConfigService.LoadConfig(Constant.SiteConfigPath);
                cacheService.Add(Constant.CacheKey.SiteConfigCacheKey,
                                 new CacheObj()
                {
                    value = Config,
                    AbsoluteExpiration = new TimeSpan(1, 0, 0, 0)
                });
            }
            if (this.Config != null)
            {
                ViewData["_SiteConfig"] = this.Config;
            }
        }
예제 #8
0
        protected virtual string GetCurrentTheme()
        {
            string theme = "";
            //SiteConfig config = CacheHelper.GetCache(Constant.CacheKey.SiteConfigCacheKey) as SiteConfig;
            CacheObj   obj    = cacheService.Get(Constant.CacheKey.SiteConfigCacheKey);
            SiteConfig config = (obj != null && obj.value != null) ? (obj.value as SiteConfig) : null;

            if (config == null)
            {
                config = siteConfigService.LoadConfig(Constant.SiteConfigPath);
                cacheService.Add(Constant.CacheKey.SiteConfigCacheKey,
                                 new CacheObj()
                {
                    value = config,
                    AbsoluteExpiration = new DateTimeOffset(DateTime.Now).AddDays(1)
                });
                //CacheHelper.SetCache(Constant.CacheKey.SiteConfigCacheKey, config);
            }
            theme = config.SiteTheme;
            return(theme);
        }
예제 #9
0
        public HTools.CacheObj Get(string key)
        {
            //内存和磁盘双重获取
            if (string.IsNullOrEmpty(key))
            {
                return(null);
            }
            CacheObj obj = m_mcache.Get(key);

            if (obj == null)
            {
                obj = m_dcache.Get(key);
                if (obj != null)
                {
                    m_mcache.Add(key, obj);
                }
                return(obj);
            }
            else
            {
                return(obj);
            }
        }