Example #1
0
        public void Update <T>(T objectToCache, string ID)
        {
            if (cacheDuration <= 0)
            {
                return;
            }

            var existingCache = cache.Where(kvp => kvp.Key.ID == ID && kvp.Key.typeOfObject == typeof(T));

            if (existingCache.Count() > 0)
            {
                cache.Remove(existingCache.Single().Key);
            }

            try
            {
                string pathToFile = Path.Combine(Path.GetTempPath(), $"crapiwrapper-{ID}.{typeof(T).ToString()}");

                File.WriteAllText(pathToFile + tempFileSuffix, CacheSerializer.SerializeObject(objectToCache));

                cache.Add(new CacheKey()
                {
                    ID = ID, typeOfObject = typeof(T)
                }, new CacheDetails()
                {
                    cached = DateTime.Now, path = pathToFile
                });
            }
            catch { }

            if ((DateTime.Now - lastCacheCheck).TotalMinutes > cacheCheckInterval)
            {
                RemoveOutdatedCache();
            }
        }
Example #2
0
        public T GetFromCache <T>(string ID)
        {
            if (cacheDuration <= 0)
            {
                return(default(T));
            }

            var result = cache.Where(kvp => kvp.Key.ID == ID && kvp.Key.typeOfObject == typeof(T));

            if (result.Count() == 0)
            {
                return(default(T));
            }

            var cacheResult = result.Single();

            if ((DateTime.Now - cacheResult.Value.cached).TotalMinutes > cacheDuration)
            {
                return(default(T));
            }

            try
            {
                string fileString = File.ReadAllText(cacheResult.Value.path + tempFileSuffix);
                return((T)CacheSerializer.DeserializeObject(fileString));
            }
            catch
            {
                return(default(T));
            }
        }