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

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

解决方案goocz2025-04-11 15:02:2219A+A-

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

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


    
  

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

在modelConfig.xml中配置如下:



  
  
  

其中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类中有个专门获取连接字符串的方法:

        /// 
        /// 获取连接字符串
        /// 
        /// 数据库连接字符串
        /// 是否读
        /// 
        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;
        }
        /// 
        /// 获取实体连接字符串
        /// 
        /// 实体类型对象
        /// 数据库类型
        /// 读写分离标识,是否读数据库,为否则取写数据库
        /// 
        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的值与以下枚举值对应:

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

        /// 
        /// MemcachedCache分布式缓存
        /// 
        MEMCACHEDCACHE = 1,

        /// 
        /// redis分布式缓存
        /// 
        REDIS = 2
    }

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

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

memcachedcache.config:



  
    127.0.0.1:11211
  
  10
  20
  10
  10
  10
  100
  3600

rediscache.config:



  127.0.0.1
  6379
  
  0

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

点击这里复制本文地址 以上内容由goocz整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!

果子教程网 © All Rights Reserved.  蜀ICP备2024111239号-5