/// <summary> /// Formats the enum. /// </summary> /// <param name="enumType">Type of the enum.</param> /// <param name="value">The value.</param> /// <returns></returns> public static string FormatEnum(Type enumType, object value) { if (value == null) { return(String.Empty); } if (enumType != null && enumType.IsEnum && System.Enum.GetName(enumType, value) != null) { var enumName = System.Enum.GetName(enumType, value); var enumKey = GetEnumTypeKey(enumType); var key = "Enums." + enumKey + "." + enumName; var text = LocalText.TryGet(key); if (text == null) { var memInfo = enumType.GetMember(enumName); if (memInfo != null && memInfo.Length == 1) { var attribute = memInfo[0].GetCustomAttribute <DescriptionAttribute>(false); if (attribute != null) { text = attribute.Description; Dependency.Resolve <ILocalTextRegistry>().Add(LocalText.InvariantLanguageID, key, text); } } } return(text ?? enumName); } else { return(value.ToString()); } }
public void Reset() { Dependency.Resolve <ILocalCache>().Remove(CacheKey); }
private static TItem GetInternal <TItem, TSerialized>(string cacheKey, TimeSpan localExpiration, TimeSpan remoteExpiration, string groupKey, Func <TItem> loader, Func <TItem, TSerialized> serialize, Func <TSerialized, TItem> deserialize) where TItem : class where TSerialized : class { ulong?groupGeneration = null; ulong?groupGenerationCache = null; string itemGenerationKey = cacheKey + GenerationSuffix; var localCache = Dependency.Resolve <ILocalCache>(); var distributedCache = Dependency.Resolve <IDistributedCache>(); // retrieves distributed cache group generation number lazily Func <ulong> getGroupGenerationValue = delegate() { if (groupGeneration != null) { return(groupGeneration.Value); } groupGeneration = distributedCache.Get <ulong?>(groupKey); if (groupGeneration == null || groupGeneration == 0) { groupGeneration = RandomGeneration(); distributedCache.Set(groupKey, groupGeneration.Value); } groupGenerationCache = groupGeneration.Value; // add to local cache, use 5 seconds from there LocalCache.Add(groupKey, groupGenerationCache, GenerationCacheExpiration); return(groupGeneration.Value); }; // retrieves local cache group generation number lazily Func <ulong> getGroupGenerationCacheValue = delegate() { if (groupGenerationCache != null) { return(groupGenerationCache.Value); } // check cached local value of group key // it expires in 5 seconds and read from server again groupGenerationCache = localCache.Get <object>(groupKey) as ulong?; // if its in local cache, return it if (groupGenerationCache != null) { return(groupGenerationCache.Value); } return(getGroupGenerationValue()); }; // first check local cache, if item exists and not expired (group version = item version) return it var cachedObj = localCache.Get <object>(cacheKey); if (cachedObj != null) { // check local cache, if exists, compare version with group one var itemGenerationCache = localCache.Get <object>(itemGenerationKey) as ulong?; if (itemGenerationCache != null && itemGenerationCache == getGroupGenerationCacheValue()) { // local cached item is not expired yet if (cachedObj == DBNull.Value) { return(null); } return((TItem)cachedObj); } // local cached item is expired, remove all information if (itemGenerationCache != null) { localCache.Remove(itemGenerationKey); } localCache.Remove(cacheKey); cachedObj = null; } // if serializer is null, than this is a local store only item if (serialize != null) { // no item in local cache or expired, now check distributed cache var itemGeneration = distributedCache.Get <ulong?>(itemGenerationKey); // if item has version number in distributed cache and this is equal to group version if (itemGeneration != null && itemGeneration.Value == getGroupGenerationValue()) { // get item from distributed cache var serialized = distributedCache.Get <TSerialized>(cacheKey); // if item exists in distributed cache if (serialized != null) { cachedObj = deserialize(serialized); LocalCache.Add(cacheKey, (object)cachedObj ?? DBNull.Value, localExpiration); LocalCache.Add(itemGenerationKey, getGroupGenerationValue(), localExpiration); return((TItem)cachedObj); } } } // couldn't find valid item in local or distributed cache, produce value by calling loader var item = loader(); // add item and its version to cache LocalCache.Add(cacheKey, (object)item ?? DBNull.Value, localExpiration); LocalCache.Add(itemGenerationKey, getGroupGenerationValue(), localExpiration); if (serialize != null) { var serializedItem = serialize(item); // add item and generation to distributed cache if (remoteExpiration == TimeSpan.Zero) { distributedCache.Set(cacheKey, serializedItem); distributedCache.Set(itemGenerationKey, getGroupGenerationValue()); } else { distributedCache.Set(cacheKey, serializedItem, remoteExpiration); distributedCache.Set(itemGenerationKey, getGroupGenerationValue(), remoteExpiration); } } return(item); }
/// <summary> /// Changes a group generation value, so that all items that depend on it are expired. /// </summary> /// <param name="groupKey">Group key</param> public static void ExpireGroupItems(string groupKey) { Dependency.Resolve <ILocalCache>().Remove(groupKey); DistributedCache.Set <object>(groupKey, null); }
/// <summary> /// Removes all items from the cache (avoid expect unit tests). /// </summary> public static void RemoveAll() { Dependency.Resolve <ILocalCache>().RemoveAll(); }
/// <summary> /// Returns true if current user has given permission. /// </summary> /// <param name="permission">Permission key (e.g. Administration)</param> public static bool HasPermission(string permission) { return(Dependency.Resolve <IPermissionService>().HasPermission(permission)); }
/// <summary> /// Reads the value of given type with specified key from the local cache. If the value doesn't exist or not /// of given type, it returns null. /// </summary> /// <typeparam name="TItem">Expected type</typeparam> /// <param name="cacheKey">Key</param> public static TItem TryGet <TItem>(string cacheKey) where TItem : class { return(Dependency.Resolve <ILocalCache>().Get <object>(cacheKey) as TItem); }
/// <summary> /// Removes the value with specified key from the local cache. If the value doesn't exist, no error is raised. /// </summary> /// <param name="cacheKey">Key</param> public static object Remove(string cacheKey) { return(Dependency.Resolve <ILocalCache>().Remove(cacheKey)); }
/// <summary> /// Adds a value to cache with a given key /// </summary> /// <param name="key">key</param> /// <param name="value">value</param> /// <param name="expiration">Expire time (Use TimeSpan.Zero to hold value with no expiration)</param> public static void Add(string key, object value, TimeSpan expiration) { Dependency.Resolve <ILocalCache>().Add(key, value, expiration); }
/// <summary> /// Writes the value to cache with specified key and /// expiration date. /// </summary> /// <typeparam name="TValue">Value type.</typeparam> /// <param name="key">Key</param> /// <param name="value">Value</param> /// <param name="expiresAt">The time the cached item will be expired on.</param> public static void Set <TValue>(string key, TValue value, TimeSpan expiration) { Dependency.Resolve <IDistributedCache>().Set(key, value, expiration); }
/// <summary> /// Writes the value to cache with specified key. /// </summary> /// <typeparam name="TValue">Value type.</typeparam> /// <param name="key">Key</param> /// <param name="value">Value</param> public static void Set <TValue>(string key, TValue value) { Dependency.Resolve <IDistributedCache>().Set(key, value); }
/// <summary> /// Reads the value with given key. If value didn't exist in cache, /// return the default(T) value. /// </summary> /// <typeparam name="TValue">Value type</typeparam> /// <param name="key">Key</param> /// <remarks>It may raise an exception if the value is not of type TValue.</remarks> public static TValue Get <TValue>(string key) { return(Dependency.Resolve <IDistributedCache>().Get <TValue>(key)); }
/// <summary> /// Increments value with specified key and returns the new value. /// If value doesn't exist, its new value will be 1. /// </summary> /// <param name="key">Key.</param> /// <param name="amount">Increase amount.</param> /// <returns>Increased amount, or 1 if it didn't exist before</returns> public static long Increment(string key, int amount = 1) { return(Dependency.Resolve <IDistributedCache>().Increment(key, amount)); }