Rookey.Frame之数据库及缓存配置

      上一篇中讨论了Rookey.Frame框架菜单配置功能,这一节我们继续学习Rookey.Frame框架的数据库连接配置。

      之前介绍了Rookey.Frame框架支持跨多数据库,并且支持读写分离,不过目前还不是太完善,目前只对mssql server完全支持,对其他数据库还有部分功能未实现,在后续会逐渐完善。针对多数据库配置,框架中有两个地方配置数据库连接字符串,一个是网站根目前下的web.config,另外一个是Rookey.Frame.Web\Config\modelConfig.xml,在web.config中数据库连接配置如下:

<connectionStrings>
    <add name="DbReadConnString" connectionString="Data Source=.;Initial Catalog=Rookey_Frame;User ID=sa;Password=123456;Pooling=true;MAX Pool Size=512;Min Pool Size=50;Connection Lifetime=30" providerName="System.Data.SqlClient" />
  </connectionStrings>

web.config中配置的连接字符串为默认连接数据库,其中name为DbReadConnString的默认为读数据库,name为DbWriteConnString的为写数据库。

     在modelConfig.xml中配置如下:

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <BaseLogEntity isEnableCache="0" cacheType="0" dbType="MsSqlServer" readConnString="Data Source=.;Initial Catalog=Rookey_Log;User ID=sa;Password=123456;Pooling=true;MAX Pool Size=512;Min Pool Size=50;Connection Lifetime=30"></BaseLogEntity>
  <!--系统模块全部启用缓存-->
  <BaseSysEntity isEnableCache="1"  cacheType="0"></BaseSysEntity>
</Root>

     其中BaseLogEntity、BaseSysEntity分别为日志基类和系统模块基类,这里配置的意思是,系统日志所有模块对应的连接字符串为Data Source=.;Initial Catalog=Rookey_Log;User ID=sa;Password=123456;Pooling=true;MAX Pool Size=512;Min Pool Size=50;Connection Lifetime=30,对于系统模块则为默认的连接数据库,这里BaseLogEntity、BaseSysEntity也可以换成具体的模块类名(也即数据表名),那所配置的模块则是单独的数据库,也就是说系统中所有的功能模块都可以配置单独的数据库,系统查找数据库连接串时,会先找当前模块有没有配置数据库连接串,如果没有则找父类配置,父类找不到再找父类,目前支持三级查找,如果没有找到自定义配置数据库连接,则取默认数据库连接,在Rookey.Frame.DALFactory\OrmLiteDalFactory.cs类中有个专门获取连接字符串的方法:

        /// <summary>
        /// 获取连接字符串
        /// </summary>
        /// <param name="connString">数据库连接字符串</param>
        /// <param name="read">是否读</param>
        /// <returns></returns>
        private string GetConnString(string connString, bool read = true)
        {
            string lastConnStr = !string.IsNullOrEmpty(connString) ? connString : (read ? this.ReadConnectionString : this.WriteConnectionString);
            string modelConfigConnString = GetModelConfigConnString(read); //取模块自定义数据库连接配置
            if (string.IsNullOrEmpty(connString) && !string.IsNullOrEmpty(modelConfigConnString))
            {
                lastConnStr = modelConfigConnString;
            }
            NotNullCheck.NotEmpty(lastConnStr, "数据库连接字符串");
            return lastConnStr;
        }

      

        /// <summary>
        /// 获取实体连接字符串
        /// </summary>
        /// <param name="modelType">实体类型对象</param>
        /// <param name="dbType">数据库类型</param>
        /// <param name="read">读写分离标识,是否读数据库,为否则取写数据库</param>
        /// <returns></returns>
        public static string GetModelConnString(Type modelType, out string dbType, bool read = true)
        {
            dbType = string.Empty;
            string modelConfigPath = GetModelConfigXml();
            string node = string.Format("/Root/{0}", modelType.Name);
            bool nodeIsExists = XmlHelper.NodeIsExists(modelConfigPath, node);
            if (!nodeIsExists) //不存在实体节点配置信息,找对应基类的节点配置信息
            {
                //取实体基类
                Type baseType = modelType.BaseType;
                if (baseType != null) //存在基类
                {
                    node = string.Format("/Root/{0}", baseType.Name); //基类节点
                    nodeIsExists = XmlHelper.NodeIsExists(modelConfigPath, node);
                }
            }
            if (!nodeIsExists) return string.Empty;
            string tempConnStr = XmlHelper.Read(modelConfigPath, node, read ? "readConnString" : "writeConnString");
            if (!read && string.IsNullOrEmpty(tempConnStr))
            {
                tempConnStr = XmlHelper.Read(modelConfigPath, node, "readConnString");
            }
            dbType = XmlHelper.Read(modelConfigPath, node, "dbType");
            return tempConnStr;
        }

    另外isEnableCache为缓存配置标识,等于1时表示该基类下所有模块或该模块启用缓存,cacheType的值与以下枚举值对应:

    /// <summary>
    /// 缓存类型
    /// </summary>
    public enum CacheProviderType
    {
        /// <summary>
        /// 本地缓存
        /// </summary>
        LOCALMEMORYCACHE = 0,

        /// <summary>
        /// MemcachedCache分布式缓存
        /// </summary>
        MEMCACHEDCACHE = 1,

        /// <summary>
        /// redis分布式缓存
        /// </summary>
        REDIS = 2
    }

    另外缓存配置或数据库配置也可在系统界面上实现 :

    最后需要说明的是针对Memcached和Redis实现分布式缓存则还需要配置Rookey.Frame.Web\Config下的memcachedcache.config和rediscache.config

 memcachedcache.config:

<?xml version="1.0"?>
<MemcachedCacheConfigInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ServerList>
    <string>127.0.0.1:11211</string>
  </ServerList>
  <MinPoolSize>10</MinPoolSize>
  <MaxPoolSize>20</MaxPoolSize>
  <ConnectionTimeOut>10</ConnectionTimeOut>
  <QueueTimeOut>10</QueueTimeOut>
  <ReceiveTimeOut>10</ReceiveTimeOut>
  <DeadTimeOut>100</DeadTimeOut>
  <CacheTimeOut>3600</CacheTimeOut>
</MemcachedCacheConfigInfo>

rediscache.config:

<?xml version="1.0" encoding="utf-8"?>
<RedisConfigInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Host>127.0.0.1</Host>
  <Port>6379</Port>
  <Pwd></Pwd>
  <InitalDB>0</InitalDB>
</RedisConfigInfo>

 好了,今天的介绍就到此地,下一篇介绍系统初始化功能,祝大家生活愉快!

posted on 2016-07-21 09:19  rookey  阅读(1939)  评论(4编辑  收藏  举报

导航