Multi.Dictionary组件过期,能否替换为ASP内置Scripting.Dictionary?
改用ASP内置Scripting.Dictionary的替代方案
完全可以用ASP内置的Scripting.Dictionary替代过期的MOTO BIT Mdict组件,以下是改写后的完整代码及关键改动说明:
改写后的代码
Function Write(strID, Comment) If ValidNumber(strID) Then ' 检查字典是否未初始化或处于调试模式 If (IsObject(Application("Translation_" & StoryCountry)) = False) OR Debugf = True Then ' 改用内置Scripting.Dictionary组件 SET objDic = Server.CreateObject("Scripting.Dictionary") ' 从数据库加载翻译数据 set toRs = conn.execute("EXEC dbo.GetTranslationsByCountryID " & StoryCountry) If Not toRs.Eof Then tRow = toRs.GetRows() ' 遍历数据填充字典 For tx = 0 To UBound(tRow, 2) strTranslationKey = tRow(0, tx) strTranslationText = tRow(1, tx) ' 简化存在性判断,避免空分支 If Not objDic.Exists(strTranslationKey) Then objDic.Add strTranslationKey, strTranslationText End If Next ' 加锁确保多请求下的线程安全(可选但推荐) Application.Lock() SET Application("Translation_" & StoryCountry) = objDic Application.UnLock() End If Set toRs = Nothing Set objDic = Nothing End If ' 读取翻译内容,键不存在时返回空值(可按需调整) If Application("Translation_" & StoryCountry).Exists(strID) Then Write = Application("Translation_" & StoryCountry)(strID) Else Write = "" ' 也可改为返回"Missing Translation"这类提示 End If Else Write = "Invalid" End If End Function
关键改动说明
- 组件替换:将原代码中的
Multi.Dictionary替换为Scripting.Dictionary,这是ASP默认内置组件,无需额外安装,不存在过期风险。 - 逻辑简化:删除原代码中空的
IF objDic.Exists(...) THEN分支,直接用Not objDic.Exists(...)判断是否添加键值对,代码更简洁。 - 线程安全优化:新增
Application.Lock()和Application.UnLock(),避免多请求同时初始化字典时出现的并发冲突(原代码未处理该场景,推荐添加)。 - 容错处理:新增键存在性判断,若请求的
strID不存在于字典中,返回空值或自定义提示,避免直接报错。
内容的提问来源于stack exchange,提问作者Lord Svendsen
相关产品推荐
相关产品推荐

