public void Set(string key, object value, CachePolicy cachePolicy = null)
        {
            GuardHelper.ArgumentNotEmpty(() => key);
            var cacheKey = new CacheKey(key);

            Set(cacheKey, value, cachePolicy);
        }
Esempio n. 2
0
        public override T DoGet <T>(string key, Func <T> acquirer, CachePolicy cachePolicy, bool IsAllowDirtyRead)
        {
            GuardHelper.ArgumentNotEmpty(() => key);
            var cacheKey = new CacheKey(key);

            return(DoGet(cacheKey, acquirer, cachePolicy, IsAllowDirtyRead));
        }
Esempio n. 3
0
        public override void DoSet(string key, object value, CachePolicy cachePolicy)
        {
            GuardHelper.ArgumentNotEmpty(() => key);
            var cacheKey = new CacheKey(key);

            DoSet(cacheKey, value, cachePolicy);
        }
        public T Get <T>(string key, Func <T> acquirer, CachePolicy cachePolicy = null)
        {
            GuardHelper.ArgumentNotEmpty(() => key);
            var cacheKey = new CacheKey(key);

            return(Get(cacheKey, acquirer, cachePolicy));
        }
        public void Remove(string key)
        {
            GuardHelper.ArgumentNotEmpty(() => key);

            using (cacheProvider.EnterWriteLock())
            {
                cacheProvider.Remove(key);
            }
        }
Esempio n. 6
0
        public async Task PublishAsync <T>(T message, string topic) where T : class
        {
            GuardHelper.ArgumentNotNull(() => message);
            GuardHelper.ArgumentNotEmpty(() => topic);
            var messageType   = typeof(T);
            var rabbitMessage = GetRabbitMessage(message, messageType);
            var exchange      = await publishExchangeDeclareStrategy.DeclareExchangeAsync(
                advancedBus, messageType, ExchangeType.Topic).ConfigureAwait(false);

            await advancedBus.PublishAsync(exchange, topic, rabbitMessage).ConfigureAwait(false);
        }
 public T Get <T>(string key)
 {
     GuardHelper.ArgumentNotEmpty(() => key);
     if (cacheProvider.Contains(key))
     {
         return(cacheProvider.Get <T>(key));
     }
     else
     {
         return(default(T));
     }
 }
Esempio n. 8
0
        /// <summary>
        /// Mark plugin as uninstalled
        /// </summary>
        /// <param name="systemName">Plugin system name</param>
        public static void MarkPluginAsUninstalled(string systemName)
        {
            GuardHelper.ArgumentNotEmpty(() => systemName);

            var  installedPluginSystemNames = GetInstalledPluginNames();
            bool alreadyMarkedAsInstalled   = installedPluginSystemNames.Contains(systemName);

            if (alreadyMarkedAsInstalled)
            {
                installedPluginSystemNames.Remove(systemName);
            }
            PluginFileParser.SaveInstalledPluginsFile(installedPluginSystemNames);
        }
Esempio n. 9
0
        public override void DoRemove(string key)
        {
            GuardHelper.ArgumentNotEmpty(() => key);

            RealRemove(key);

            //如果採用臟讀寫入數據時,刪除時應該把相應的CacheSign的數據同時刪除
            var signKey = string.Format(CacheKeyTemplates.CacheSign, key);

            if (this.Contains(signKey))
            {
                RealRemove(signKey);
            }
        }
Esempio n. 10
0
        private Message <T> PrepareSend <T>(string queue, T message) where T : class
        {
            GuardHelper.ArgumentNotEmpty(() => queue);
            GuardHelper.ArgumentNotNull(() => message);
            DeclareQueue(queue);
            var wrappedMessage = new Message <T>(message)
            {
                Properties =
                {
                    DeliveryMode = messageDeliveryModeStrategy.GetDeliveryMode(typeof(T))
                }
            };

            return(wrappedMessage);
        }
Esempio n. 11
0
        public Type Deserialize(string typeName)
        {
            GuardHelper.ArgumentNotEmpty(() => typeName);

            return(deserializedTypes.GetOrAdd(typeName, t =>
            {
                var nameParts = t.Split(':');
                if (nameParts.Length != 2)
                {
                    throw new FrameworkException("type name {0}, is not a valid type name. Expected Type:Assembly", t);
                }
                var type = Type.GetType(nameParts[0] + ", " + nameParts[1]);
                if (type == null)
                {
                    throw new FrameworkException("Cannot find type {0}", t);
                }
                return type;
            }));
        }
Esempio n. 12
0
        public List <string> GetKeysToRemoveByPattern(string pattern)
        {
            GuardHelper.ArgumentNotEmpty(() => pattern);

            var regex = new Regex(pattern, RegexOptions.Singleline
                                  | RegexOptions.Compiled | RegexOptions.IgnoreCase);
            var keysToRemove = new List <string>();

            var allEntries = GetAllEntries();

            foreach (var item in allEntries)
            {
                if (regex.IsMatch(item.Key))
                {
                    keysToRemove.Add(item.Key);
                }
            }

            return(keysToRemove);
        }
Esempio n. 13
0
        public override T DoGet <T>(CacheKey key, Func <T> acquirer, CachePolicy cachePolicy, bool IsAllowDirtyRead)
        {
            var strKey = key.Key;

            GuardHelper.ArgumentNotEmpty(() => strKey);

            if (cachePolicy == null)
            {
                cachePolicy = new CachePolicy();
            }

            if (IsAllowDirtyRead)
            {
                return(GetCacheValueByDirtyRead <T>(key, acquirer, cachePolicy));
            }
            else
            {
                return(GetCahcheValueByCommonRead <T>(key, acquirer, cachePolicy));
            }
        }
Esempio n. 14
0
        public T Get <T>(CacheKey cacheKey, Func <T> acquirer, CachePolicy cachePolicy = null)
        {
            var strKey = cacheKey.Key;

            GuardHelper.ArgumentNotEmpty(() => strKey);

            if (cachePolicy == null)
            {
                cachePolicy = new CachePolicy();
            }

            if (cacheProvider.Contains(strKey))
            {
                var value = cacheProvider.Get <T>(strKey);
                if (cacheProvider.CacheType == CacheType.Redis &&
                    cachePolicy.ExpirationType == CacheExpirationType.Sliding)
                {
                    this.Set(cacheKey, value, cachePolicy);
                }
                return(value);
            }
            else
            {
                if (acquirer == null)
                {
                    return(default(T));
                }
                using (cacheProvider.EnterReadLock())
                {
                    if (!cacheProvider.Contains(strKey))
                    {
                        var value = acquirer();
                        this.Set(cacheKey, value, cachePolicy);

                        return(value);
                    }
                }

                return(cacheProvider.Get <T>(strKey));
            }
        }
Esempio n. 15
0
        public override void DoSet(CacheKey cacheKey, object value, CachePolicy cachePolicy)
        {
            var strKey = cacheKey.Key;

            GuardHelper.ArgumentNotEmpty(() => strKey);

            if (value == null)
            {
                return;
            }

            if (cachePolicy == null)
            {
                cachePolicy = new CachePolicy();
            }

            using (cacheProvider.EnterWriteLock())
            {
                cacheProvider.Set(cacheKey, value, cachePolicy);
            }
        }
Esempio n. 16
0
        public ISubscriptionResult SubscribeAsync <T>(string subscriptionId, Func <T, Task> onMessage)
            where T : class
        {
            GuardHelper.ArgumentNotEmpty(() => subscriptionId);
            GuardHelper.ArgumentNotNull(() => onMessage);

            var    queueName    = Conventions.QueueNamingConvention(typeof(T), subscriptionId);
            var    exchangeName = Conventions.ExchangeNamingConvention(typeof(T));
            IQueue queue        = QueueDeclare(queueName);
            var    exchange     = ExchangeDeclare(exchangeName);

            foreach (var topic in subConfigure.Topics.DefaultIfEmpty("#"))
            {
                advancedBus.Bind(exchange, queue, topic);
            }

            var consumerCancellation = advancedBus.Consume <T>(
                queue,
                (message, messageReceivedInfo) => onMessage(message.Body),
                x => { InitIConsumerConfiguration(x); });

            return(new SubscriptionResult(exchange, queue, consumerCancellation));
        }
Esempio n. 17
0
 public Queue(string name, bool isExclusive)
 {
     GuardHelper.ArgumentNotEmpty(() => name);
     Name        = name;
     IsExclusive = isExclusive;
 }
Esempio n. 18
0
        private static LoadPluginResult LoadPluginFromFolder(string pluginFolderPath, ICollection <string> installedPluginSystemNames)
        {
            GuardHelper.ArgumentNotEmpty(() => pluginFolderPath);

            var folder = new DirectoryInfo(pluginFolderPath);

            if (!folder.Exists)
            {
                return(null);
            }

            var descriptionFile = new FileInfo(Path.Combine(pluginFolderPath, "Description.txt"));

            if (!descriptionFile.Exists)
            {
                return(null);
            }

            // load descriptor file (Description.txt)
            var descriptor = PluginFileParser.ParsePluginDescriptionFile(descriptionFile.FullName);

            // some validation
            if (descriptor.SystemName.IsEmpty())
            {
                throw new Exception("The plugin descriptor '{0}' does not define a plugin system name. Try assigning the plugin a unique name and recompile.".FormatInvariant(descriptionFile.FullName));
            }
            if (descriptor.PluginFileName.IsEmpty())
            {
                throw new Exception("The plugin descriptor '{0}' does not define a plugin assembly file name. Try assigning the plugin a file name and recompile.".FormatInvariant(descriptionFile.FullName));
            }

            var result = new LoadPluginResult
            {
                DescriptionFile = descriptionFile,
                Descriptor      = descriptor
            };

            //ensure that version of plugin is valid
            if (!IsAssumedCompatible(descriptor))
            {
                result.IsIncompatible = true;
                return(result);
            }

            if (_referencedPlugins.ContainsKey(descriptor.SystemName))
            {
                throw new Exception(string.Format("A plugin with system name '{0}' is already defined", descriptor.SystemName));
            }

            if (installedPluginSystemNames == null)
            {
                installedPluginSystemNames = PluginFileParser.ParseInstalledPluginsFile();
            }

            // set 'Installed' property
            descriptor.Installed = installedPluginSystemNames.Contains(descriptor.SystemName);

            try
            {
                // get list of all DLLs in plugin folders (not in 'bin' or '_Backup'!)
                var pluginBinaries = descriptionFile.Directory.GetFiles("*.dll", SearchOption.AllDirectories)
                                     // just make sure we're not registering shadow copied plugins
                                     .Where(x => IsPackagePluginFolder(x.Directory))
                                     .ToList();

                // other plugin description info
                var mainPluginFile = pluginBinaries.Where(x => x.Name.IsCaseInsensitiveEqual(descriptor.PluginFileName)).FirstOrDefault();
                descriptor.OriginalAssemblyFile = mainPluginFile;

                // shadow copy main plugin file
                descriptor.ReferencedAssembly = Probe(mainPluginFile);

                if (!descriptor.Installed)
                {
                    _inactiveAssemblies.Add(descriptor.ReferencedAssembly);
                }

                // load all other referenced assemblies now
                var otherAssemblies = from x in pluginBinaries
                                      where !x.Name.IsCaseInsensitiveEqual(mainPluginFile.Name)
                                      select x;

                foreach (var assemblyFile in otherAssemblies)
                {
                    if (!IsAlreadyLoaded(assemblyFile))
                    {
                        Probe(assemblyFile);
                    }
                }

                // init plugin type (only one plugin per assembly is allowed)
                var  exportedTypes   = descriptor.ReferencedAssembly.ExportedTypes;
                bool pluginFound     = false;
                bool preStarterFound = !descriptor.Installed;
                foreach (var t in exportedTypes)
                {
                    if (typeof(IPlugin).IsAssignableFrom(t) && !t.IsInterface && t.IsClass && !t.IsAbstract)
                    {
                        descriptor.PluginType     = t;
                        descriptor.IsConfigurable = typeof(IConfigurable).IsAssignableFrom(t);
                        pluginFound = true;
                    }
                    else if (descriptor.Installed && typeof(IPreApplicationStart).IsAssignableFrom(t) && !t.IsInterface && t.IsClass && !t.IsAbstract && t.HasDefaultConstructor())
                    {
                        try
                        {
                            var preStarter = Activator.CreateInstance(t) as IPreApplicationStart;
                            preStarter.Start();
                        }
                        catch { }
                        preStarterFound = true;
                    }
                    if (pluginFound && preStarterFound)
                    {
                        break;
                    }
                }

                result.Success = true;
            }
            catch (ReflectionTypeLoadException ex)
            {
                var msg = string.Empty;
                foreach (var e in ex.LoaderExceptions)
                {
                    msg += e.Message + Environment.NewLine;
                }

                var fail = new Exception(msg, ex);
                Debug.WriteLine(fail.Message, fail);

                throw fail;
            }

            return(result);
        }
Esempio n. 19
0
 /// <summary>
 /// Gets a value indicating whether a plugin
 /// is registered and installed.
 /// </summary>
 /// <param name="systemName">The system name of the plugin to check for</param>
 /// <returns><c>true</c> if the plugin exists, <c>false</c> otherwise</returns>
 public static bool PluginExists(string systemName)
 {
     GuardHelper.ArgumentNotEmpty(() => systemName);
     return(_referencedPlugins.ContainsKey(systemName));
 }
Esempio n. 20
0
 public ILogger GetLogger(string name)
 {
     GuardHelper.ArgumentNotEmpty(() => name);
     return(GetLoggerInternal(name));
 }
Esempio n. 21
0
 public SystemNameAttribute(string name)
 {
     GuardHelper.ArgumentNotEmpty(() => name);
     Name = name;
 }
Esempio n. 22
0
 public QueueDeclareParam(string name)
 {
     GuardHelper.ArgumentNotEmpty(() => name);
     Name    = name;
     Durable = true;
 }