protected virtual ICachePolicy GetCachePolicy(string key)
        {
            EnsureFrameworkConfig();

            // Figure out the type of the cache key
            Type realType        = null;
            var  getTypeFromJson = CacheKey.GetAndRemoveTypeFromJson(key, out realType);

            if (realType == null || getTypeFromJson.IsNullOrWhiteSpace())
            {
                return(StaticCachePolicy.CreateDefault());
            }

            try
            {
                var jsonSerializerSettings = new JsonSerializerSettings()
                {
                    MissingMemberHandling  = MissingMemberHandling.Error,
                    ObjectCreationHandling = ObjectCreationHandling.Replace
                };
                var realKey = JsonConvert.DeserializeObject(getTypeFromJson, realType, jsonSerializerSettings) as CacheKey;
                if (realKey == null)
                {
                    return(StaticCachePolicy.CreateDefault());
                }

                // Figure out which type of cache provider we are, i.e. which rules to load
                var policy = GetCachePolicyForKey(realKey);
                return(policy);
            }
            catch
            {
                return(StaticCachePolicy.CreateDefault());
            }
        }
        public virtual ICachePolicy GetCachePolicyForKey(CacheKey realKey)
        {
            EnsureFrameworkConfig();

            var extendedType = _config.CacheProviders.ExtendedLifetime.GetProviderType();

            var provider = _config.CacheProviders.LimitedLifetime;

            if (extendedType == this.GetType())
            {
                provider = _config.CacheProviders.ExtendedLifetime;
            }

            // Avoid people having to put Original. in front of all their expressions so that the dynamic expression parser can find the properties
            var tryCast = realKey as IClassKey <object>;

            if (tryCast != null)
            {
                return(provider.GetPolicyFor(tryCast.Original) ?? StaticCachePolicy.CreateDefault());;
            }
            var policy = provider.GetPolicyFor(realKey);

            return(policy ?? StaticCachePolicy.CreateDefault());;
        }
Example #3
0
 public CacheValueOf(T value)
     : this(value, StaticCachePolicy.CreateDefault())
 {
 }