예제 #1
0
        public static ICacheProvider Create(Configuration.Cache cache)
        {
            ICacheProvider cacheProvider = null;

            switch (cache.Type)
            {
            case "Lru":
            {
                cacheProvider = new LruCacheProvider();
                break;
            }

            case "Fifo":
            {
                cacheProvider = new FifoCacheProvider();
                break;
            }

            default:
            {
                Type cacheProviderType = TypeUtils.GetType(cache.Type);
                cacheProvider = EmitObjectFactoryBuilder.Instance
                                .GetObjectFactory(cacheProviderType, Type.EmptyTypes)(null) as ICacheProvider;
                break;
            }
            }

            cacheProvider.Initialize(cache.Parameters);
            return(cacheProvider);
        }
예제 #2
0
 private void FlushCache(Configuration.Cache cache)
 {
     if (_logger.IsEnabled(LogLevel.Debug))
     {
         _logger.LogDebug($"FlushCache Cache.Id:{cache.Id} .");
     }
     cache.Provider.Flush();
     if (_cacheMappedLastFlushTime.TryGetValue(cache, out var lastFlushTime))
     {
         _cacheMappedLastFlushTime[cache] = DateTime.Now;
     }
 }
예제 #3
0
        private void BuildCache(SqlMap sqlMap, Type interfaceType)
        {
            var cacheAttrs = interfaceType.GetCustomAttributes <CacheAttribute>();

            foreach (var cacheAttribute in cacheAttrs)
            {
                Configuration.Cache cache = new Configuration.Cache
                {
                    FlushInterval = new FlushInterval(), Id = ParseCacheFullId(sqlMap.Scope, cacheAttribute.Id)
                };
                if (sqlMap.Caches.ContainsKey(cache.Id))
                {
                    throw new SmartSqlException($"Cache.FullId:[{cache.Id}] already exists!");
                }

                if (cacheAttribute.FlushInterval > 0)
                {
                    cache.FlushInterval.Seconds = cacheAttribute.FlushInterval;
                }

                if (cacheAttribute.FlushOnExecutes != null && cacheAttribute.FlushOnExecutes.Length > 0)
                {
                    cache.FlushOnExecutes = cacheAttribute.FlushOnExecutes.Select(m => new FlushOnExecute
                    {
                        Statement = m.IndexOf('.') > 0 ? m : $"{sqlMap.Scope}.{m}"
                    }).ToList();
                }

                cache.Parameters = new Dictionary <string, object>
                {
                    { nameof(CacheAttribute.CacheSize), cacheAttribute.CacheSize }
                };
                cache.Type     = cacheAttribute.Type;
                cache.Provider = CacheProviderUtil.Create(cache);
                sqlMap.Caches.Add(cache.Id, cache);
            }
        }
예제 #4
0
        private void BuildCache(XmlElement cacheNode)
        {
            cacheNode.Attributes.TryGetValueAsString(nameof(Configuration.Cache.Id), out var id,
                                                     SmartSqlConfig.Properties);
            cacheNode.Attributes.TryGetValueAsString(nameof(Configuration.Cache.Type), out var type,
                                                     SmartSqlConfig.Properties);

            var cache = new Configuration.Cache
            {
                Id              = id,
                Type            = type,
                Parameters      = new Dictionary <String, Object>(),
                FlushOnExecutes = new List <FlushOnExecute>()
            };

            if (cache.Id.IndexOf('.') < 0)
            {
                cache.Id = $"{SqlMap.Scope}.{cache.Id}";
            }

            foreach (XmlNode childNode in cacheNode.ChildNodes)
            {
                switch (childNode.Name)
                {
                case "Property":
                {
                    childNode.Attributes.TryGetValueAsString("Name", out var name, SmartSqlConfig.Properties);
                    childNode.Attributes.TryGetValueAsString("Value", out var propVal, SmartSqlConfig.Properties);
                    cache.Parameters.Add(name, propVal);
                    break;
                }

                case "FlushInterval":
                {
                    childNode.Attributes.TryGetValueAsInt32("Hours", out var hours, SmartSqlConfig.Properties);
                    childNode.Attributes.TryGetValueAsInt32("Minutes", out var minutes, SmartSqlConfig.Properties);
                    childNode.Attributes.TryGetValueAsInt32("Seconds", out var seconds, SmartSqlConfig.Properties);
                    cache.FlushInterval = new FlushInterval
                    {
                        Hours   = hours,
                        Minutes = minutes,
                        Seconds = seconds
                    };

                    cache.Parameters.Add("FlushInterval", cache.FlushInterval);
                    break;
                }

                case "FlushOnExecute":
                {
                    childNode.Attributes.TryGetValueAsString("Statement", out var statementId,
                                                             SmartSqlConfig.Properties);
                    if (!String.IsNullOrEmpty(statementId))
                    {
                        if (statementId.IndexOf('.') < 0)
                        {
                            statementId = $"{SqlMap.Scope}.{statementId}";
                        }

                        cache.FlushOnExecutes.Add(new FlushOnExecute
                            {
                                Statement = statementId
                            });
                    }

                    break;
                }
                }
            }

            cache.Parameters.Add("Cache.Id", cache.Id);

            cache.Provider = CacheProviderUtil.Create(cache);
            SqlMap.Caches.Add(cache.Id, cache);
        }