金蝶云星空分布式缓存使用说明书目录1.概述 (3)1.1.目的 (3)1.2.范围 (3)1.3.适用对象 (3)1.4.参考资料 (3)2.问题与解决策略 (3)3.目标和约束 (3)4.部署Redis服务 (4)4.1.安装服务--service-install(安装必须的步骤) (4)4.2.卸载服务--service-uninstall (4)4.3.启动服务--service-start(安装必须的步骤) (4)4.4.停止服务--service-stop (5)4.5.服务配置修改(服务名和端口) (5)4.6.Redis-cli配置(如果4.1按照2,3小点进行了修改,此节配置可以忽略) (5)5.Redis配置文件修改 (5)6.Redis缓存代码示例 (6)6.1.1.写缓存和读缓存 (6)6.1.2.错误的读写缓存 (7)7.附录 (8)1.1. 目的为系统集群及分布式应用提供基础缓存服务。
1.2. 范围➢适用版本:V6.2和后续版本1.3. 适用对象本文档适用于:➢开发工程师:部署和开发指导。
➢实施人员:部署指导。
1.4. 参考资料➢Redis相关资料目标:➢提供单据和基础数据的分布式缓存支持;➢提供系统参数、单据参数、用户参数等数据的分布式缓存支持;➢提供分布式缓存接口为其他类型数据提供分布式缓存支持约束:➢分布式缓存开关并非默认开启;➢并非所有数据都使用了分布式缓存。
首先进入到产品安装目录K3Cloud/website/bin,将以下文件单独拷贝到缓存服务器,新建一目录,例如Redis,拷贝文件如下:EventLog.dll,redis.windows.conf,redis.windows-service.conf,redis-benchmark.exe,redis-check-aof.exe,redis-cli.exe,redis-server.exe然后以管理员身份进入dos 系统,将目录定位到Redis目录。
如未说明,后续命令都在该目录下执行。
4.1. 安装服务--service-install(安装必须的步骤)This must be the first argument on the redis-server command line. Arguments after this are passed in the order they occur to Redis when the service is launched. The service will be configured as Autostart and will be launched as "NT AUTHORITY\NetworkService". Upon successful installation a success message will be displayed and Redis will exit.This command does not start the service.For instance:redis-server --service-install redis.windows.conf --loglevel verbose默认配置的地址是本地地址127.0.0.1 端口是6379,如果多应用部署分布式缓存,需要在启动服务前先修改缓存服务器配置文件中的地址为具体的IP。
1)打开配置文件,找到bind 127.0.0.1,修改IP为具体的IP地址,例如:bind 172.17.2.1002)打开配置文件,找到protected-mode yes 修改为protected-mode noredis缓存服务和k3cloud不在一台服务器时,需要此项设置3)打开配置文件,找到stop-writes-on-bgsave-error no 修改为stop-writes-on-bgsave-error no 强制停止快照导致不能持久化错误处理4.2. 卸载服务--service-uninstallThis will remove the Redis service configuration information from the registry. Upon successful uninstallation a success message will be displayed and Redis will exit.This does command not stop the service.For instance:redis-server --service-uninstall4.3. 启动服务--service-start(安装必须的步骤)This will start the Redis service. Upon successful start, a success message will be displayed and Redis willbegin running.For instance:redis-server --service-start4.4. 停止服务--service-stopThis will stop the Redis service. Upon successful termination a success message will be displayed and Redis will exit.For instance:redis-server --service-stop4.5. 服务配置修改(服务名和端口)This optional argument may be used with any of the preceding commands to set the name of the installed service. This argument should follow the service-install, service-start, service-stop or service-uninstall commands, and precede any arguments to be passed to Redis via the service-install command.The following would install and start three separate instances of Redis as a service:redis-server --service-install --service-name redisService1 --port 10001redis-server --service-start --service-name redisService14.6. Redis-cli配置(如果4.1按照2,3小点进行了修改,此节配置可以忽略)使用redis-cli.exe进行配置时,需要先双击打开redis-server.exe(打开前如果之前已经启动了服务,先停止)打开redis-server后,再双击打开redis-cli.exe在窗口依次输入以下配置指令:1)config set protected-mode noredis缓存服务和k3cloud不在一台服务器时,需要此项设置2)config set stop-writes-on-bgsave-error no强制停止快照导致不能持久化错误处理在金蝶云星空website/app_data/KCache.config文件中:<RCacheSetting CacheTypeID="RedisCache" Switch="ON"LogSwitch="OFF"><RCachePath ID="Redis1" Switch ="ON" Timespan="28800" Host="127.0.0.1"Port="6379"/></RCacheSetting>端口号默认使用6379,可以配置多个缓存服务器,如果不想使用Redis缓存,则设置Swith="OFF",多应用服务器部署,需要明确Host的地址。
6.1.1.写缓存和读缓存调用参考:注意:业务插件代码读写缓存的时候,如果是自己使用KCacheManagerFactory来管理缓存,则需要替换成RCacheManagerFactory,其他的如Put和Get的写法保持不变。
使用CacheUtil的代码不需要处理,平台统一进行了处理。
另外如果是循环从缓存读写数据,需要参考以下写法,否则可能引发性能问题,如果循环读取使用的是CacheUtil,则建议在循环外使用RCacheManagerFactory来管理缓存。
循环读写缓存示例:(重点关注红色注释)// 使用using括起来,便于自动释放连接对象using (IKCacheManager kcmger =RCacheManagerFactory.Instance.GetCacheManager(strUserParameterRegion,ctx.GetAreaCacheKey())){IRCacheManager rcmger = kcmger as IRCacheManager;if (rcmger != null){// SetConnectionCloseFlag,设置Redis连接不自动关闭,减少每次访问Redis都去重新建立连接的开销rcmger.SetConnectionCloseFlag(false);}DynamicObjectCollection objs = DBUtils.ExecuteDynamicObject(ctx, sql, null, null, CommandType.Text, paramlist.ToArray());if (objs != null && objs.Count > 0){//省略代码foreach (DynamicObject item in objs){//尝试从缓存取long userId = long.Parse(item["FUserId"].ToString());string strCacheKey =UserParameterUtils.ReturnUserParameterCacheKey(userId, parameterFormId, parameterObjId, ERPARAMETER_KEY);DynamicObject obj = kcmger.Get(strCacheKey) as DynamicObject;//省略代码}//清除所有用户参数缓存string strUserId = "select FUserId from t_sec_user";List<long> userIds = DBUtils.ExecuteEnumerable(ctx, strUserId).Select(p => p.GetValue<long>("FUserId")).ToList();foreach (long userid in userIds){kcmger.Remove(UserParameterUtils.ReturnUserParameterCacheKey(userid, parameterFormId, parameterObjId, ERPARAMETER_KEY));}}6.1.2.错误的读写缓存IKCacheManager kcmgr = KCacheManagerFactory.Instance.GetCacheManager(region, ctx.DBId);if (null != kcmgr){try{kcmgr.Put(key, value);}catch { }}}以上代码两处需要调整:1、KCacheManagerFactory改成RCacheManagerFactory2、ctx.DBId应该使用ctx.GetAreaCacheKey。