/// <summary> /// Exports current items in the cache /// </summary> /// <param name="cacheType">Specifies what type of cache items will be exported</param> /// <returns>Collection of <see cref="ExportableCI"/> containing all the items currently in the cache</returns> public async Task <IEnumerable <ExportableCI> > CacheExportAsync(CacheType cacheType) { var tasks = new List <Task <IEnumerable <ExportableCI> > >(); if (cacheType.HasFlag(CacheType.SportData)) { tasks.Add(_sportDataCache.ExportAsync()); } if (cacheType.HasFlag(CacheType.SportEvent)) { tasks.Add(_sportEventCache.ExportAsync()); } if (cacheType.HasFlag(CacheType.Profile)) { tasks.Add(_profileCache.ExportAsync()); } tasks.ForEach(t => t.ConfigureAwait(false)); return((await Task.WhenAll(tasks)).SelectMany(e => e)); }
/// <summary> /// Caches an object to the specified cache, using the specified key /// </summary> /// <typeparam name="T">Object type</typeparam> /// <param name="Object">Object to cache</param> /// <param name="Type">Caching type</param> /// <param name="Key">Key to cache the item under</param> public static void Cache <T>(this T Object, string Key, CacheType Type) { if (HttpContext.Current == null && !Type.HasFlag(CacheType.Internal)) { return; } if (HttpContext.Current != null && Type.HasFlag(CacheType.Cache)) { HttpContext.Current.Cache.Add(Key, Object, null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.Normal, null); } else if (HttpContext.Current != null && Type.HasFlag(CacheType.Item)) { HttpContext.Current.Items[Key] = Object; } else if (HttpContext.Current != null && Type.HasFlag(CacheType.Session)) { HttpContext.Current.Session[Key] = Object; } else if (HttpContext.Current != null && Type.HasFlag(CacheType.Cookie)) { HttpContext.Current.Response.Cookies.Add(new HttpCookie(Key, Object.ToString())); } else if (Type.HasFlag(CacheType.Internal)) { new Cache <string>().Add(Key, Object); } }
/// <summary> /// Gets the specified object from the cache if it exists, otherwise the default value is returned /// </summary> /// <typeparam name="T">Object type</typeparam> /// <param name="Key">Key that the object is under</param> /// <param name="Type">Cache types to search</param> /// <param name="DefaultValue">Default value to return</param> /// <returns>The specified object if it exists, otherwise the default value</returns> public static T GetFromCache <T>(this string Key, CacheType Type, T DefaultValue = default(T)) { if (HttpContext.Current == null && !Type.HasFlag(CacheType.Internal)) { return(DefaultValue); } if (HttpContext.Current != null && Type.HasFlag(CacheType.Cache)) { return(HttpContext.Current.Cache.Get(Key).To(DefaultValue)); } if (HttpContext.Current != null && Type.HasFlag(CacheType.Item)) { return(HttpContext.Current.Items[Key].To(DefaultValue)); } else if (HttpContext.Current != null && Type.HasFlag(CacheType.Session)) { return(HttpContext.Current.Session[Key].To(DefaultValue)); } else if (HttpContext.Current != null && Type.HasFlag(CacheType.Cookie)) { return(HttpContext.Current.Response.Cookies[Key].Value.To(DefaultValue)); } else if (Type.HasFlag(CacheType.Internal)) { return(new Cache <string>().Get <T>(Key)); } return(DefaultValue); }
/// <summary> /// Gets the specified object from the cache if it exists, otherwise the default value is returned /// </summary> /// <typeparam name="T">Object type</typeparam> /// <param name="key">Key that the object is under</param> /// <param name="type">Cache types to search</param> /// <param name="defaultValue">Default value to return</param> /// <returns>The specified object if it exists, otherwise the default value</returns> public static T GetFromCache <T>(this string key, CacheType type, T defaultValue = default(T)) { if (HttpContext.Current == null && !type.HasFlag(CacheType.Internal)) { return(defaultValue); } if (HttpContext.Current != null && type.HasFlag(CacheType.Cache)) { return(HttpContext.Current.Cache.Get(key).TryTo(defaultValue)); } if (HttpContext.Current != null && type.HasFlag(CacheType.Item)) { return(HttpContext.Current.Items[key].TryTo(defaultValue)); } if (HttpContext.Current != null && type.HasFlag(CacheType.Session)) { return(HttpContext.Current.Session[key].TryTo(defaultValue)); } if (HttpContext.Current != null && type.HasFlag(CacheType.Cookie)) { return(HttpContext.Current.Response.Cookies[key].Value.TryTo(defaultValue)); } if (type.HasFlag(CacheType.Internal)) { return(new Cache <string>().Get <T>(key)); } return(defaultValue); }