打印采用 MemoryCache 替代原有 Web 缓存,,提升兼容性与性能。报表数据与参数缓存方式由 Session 直接存储对象改为存储缓存键,实际数据存入 MemoryCache,避免 Session 大对象。

This commit is contained in:
李鹏飞 2025-12-31 16:31:30 +08:00
parent e5a6b65a13
commit 07539fbcca
14 changed files with 256 additions and 165 deletions

View File

@ -0,0 +1,7 @@
{
"ExpandedNodes": [
""
],
"SelectedNode": "\\SGGL.sln",
"PreviewInSolutionExplorer": false
}

View File

@ -0,0 +1,8 @@
{
"permissions": {
"allow": [
"Bash(cat:*)",
"Bash(find:*)"
]
}
}

File diff suppressed because one or more lines are too long

View File

@ -95,6 +95,7 @@
<Reference Include="System.DirectoryServices" />
<Reference Include="System.Drawing" />
<Reference Include="System.Management" />
<Reference Include="System.Runtime.Caching" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Web" />
@ -114,7 +115,6 @@
<Compile Include="API\APIUnitInfoService.cs" />
<Compile Include="API\APIWeldServices.cs" />
<Compile Include="AttachFile\AttachFileService.cs" />
<Compile Include="CacheHelper.cs" />
<Compile Include="Common\ADomainService.cs" />
<Compile Include="Common\BaseInfo\AudiFlowService.cs" />
<Compile Include="Common\BaseInfo\AuditFlowApproveService.cs.cs" />
@ -125,6 +125,7 @@
<Compile Include="Common\BaseInfo\Base_TeamGroupService.cs" />
<Compile Include="Common\BaseInfo\Base_UnitService.cs" />
<Compile Include="Common\BaseInfo\ProjectPlanService.cs" />
<Compile Include="Common\CacheHelper.cs" />
<Compile Include="Common\ChartControlService.cs" />
<Compile Include="Common\ConstValue.cs" />
<Compile Include="Common\FastReportService.cs" />

View File

@ -1,89 +0,0 @@
using System;
using System.Web;
using System.Collections;
public class CacheHelper
{
/// <summary>
/// 获取数据缓存
/// </summary>
/// <param name="CacheKey">键</param>
/// <returns></returns>
public static object GetCache(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
}
/// <summary>
/// 设置数据缓存
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
public static void SetCache(string CacheKey, object objObject)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject);
}
/// <summary>
/// 设置数据缓存
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
/// <param name="Timeout"></param>
public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
}
/// <summary>
/// 设置数据缓存
/// </summary>
/// <param name="CacheKey"></param>
/// <param name="objObject"></param>
/// <param name="absoluteExpiration"></param>
/// <param name="slidingExpiration"></param>
public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
}
/// <summary>
/// 移除指定数据缓存
/// </summary>
/// <param name="CacheKey"></param>
public static void RemoveAllCache(string CacheKey)
{
System.Web.Caching.Cache _cache = HttpRuntime.Cache;
_cache.Remove(CacheKey);
}
/// <summary>
/// 移除全部缓存
/// </summary>
public static void RemoveAllCache()
{
System.Web.Caching.Cache _cache = HttpRuntime.Cache;
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
_cache.Remove(CacheEnum.Key.ToString());
}
}
/// <summary>
/// 获取缓存
/// </summary>
private static object GetPrivate(string key)
{
return string.IsNullOrEmpty(key) ? null : HttpRuntime.Cache.Get(key);
}
}

View File

@ -0,0 +1,102 @@
using System;
using System.Runtime.Caching;
namespace BLL
{
public static class CacheHelper
{
private static readonly ObjectCache Cache = MemoryCache.Default;
/// <summary>
/// 添加缓存项
/// </summary>
/// <param name="key">缓存键</param>
/// <param name="value">缓存值</param>
/// <param name="absoluteExpiration">绝对过期时间</param>
public static void Add(string key, object value, DateTimeOffset absoluteExpiration)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException(nameof(key));
}
Cache.Set(key, value, absoluteExpiration);
}
/// <summary>
/// 获取缓存项(泛型)
/// </summary>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="key">缓存键</param>
/// <returns>缓存值</returns>
public static T Get<T>(string key)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException(nameof(key));
}
object value = Cache[key];
if (value is T tValue)
{
return tValue;
}
return default(T);
}
/// <summary>
/// 获取缓存项
/// </summary>
/// <param name="key">缓存键</param>
/// <returns>缓存值</returns>
public static object Get(string key)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException(nameof(key));
}
return Cache[key];
}
/// <summary>
/// 移除缓存项
/// </summary>
/// <param name="key">缓存键</param>
public static void Remove(string key)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException(nameof(key));
}
Cache.Remove(key);
}
/// <summary>
/// 清空所有缓存
/// </summary>
public static void Clear()
{
foreach (var item in Cache)
{
Cache.Remove(item.Key);
}
}
/// <summary>
/// 检查缓存项是否存在
/// </summary>
/// <param name="key">缓存键</param>
/// <returns>是否存在</returns>
public static bool Exists(string key)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException(nameof(key));
}
return Cache.Contains(key);
}
}
}

View File

@ -1,40 +1,85 @@
using System.Web;
using System.Web.Security;
using FastReport;
using NPOI.POIFS.Crypt;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using FastReport;
using System.Web;
using System.Web.Security;
namespace BLL.Common
{
public static class FastReportService
{
// 缓存过期时间30分钟
private static readonly int CacheExpirationMinutes = 5;
/// <summary>
/// 重置数据
/// 重置数据 - 清理缓存和Session
/// </summary>
public static void ResetData()
{
HttpContext.Current.Session["ReportDataTables"] = null;
HttpContext.Current.Session["ReportParameterValues"] = null;
if (HttpContext.Current?.Session == null)
{
return;
}
// 清理Session中的缓存键引用
var cacheKeys = HttpContext.Current.Session["ReportDataCacheKeys"] as List<string>;
if (cacheKeys != null)
{
foreach (var key in cacheKeys)
{
try
{
CacheHelper.Remove(key);
}
catch
{
// 忽略缓存删除异常
}
}
}
HttpContext.Current.Session["ReportDataCacheKeys"] = null;
HttpContext.Current.Session["ReportParameterCacheKey"] = null;
}
/// <summary>
/// 添加报表数据表到缓存
/// </summary>
public static void AddFastreportTable(DataTable dataTable)
{
List<DataTable> dataTables = (List<DataTable>)HttpContext.Current.Session["ReportDataTables"];
if (dataTables == null)
// 生成唯一缓存键
string cacheKey = $"ReportData_{Guid.NewGuid()}_{DateTime.Now.Ticks}";
// 将数据表存储到缓存中设置5分钟过期
CacheHelper.Add(cacheKey, dataTable, DateTimeOffset.Now.AddMinutes(CacheExpirationMinutes));
List<string> cacheKeys = HttpContext.Current.Session["ReportDataCacheKeys"] as List<string>;
if (cacheKeys==null)
{
dataTables = new List<DataTable>();
cacheKeys = new List<string>();
}
dataTables.Add(dataTable);
HttpContext.Current.Session["ReportDataTables"] = dataTables;
cacheKeys.Add(cacheKey);
HttpContext.Current.Session["ReportDataCacheKeys"] = cacheKeys;
}
public static void AddFastreportParameter(Dictionary<string, string> Dicparames)
{
HttpContext.Current.Session["ReportParameterValues"] = Dicparames;
/// <summary>
/// 添加报表参数到缓存
/// </summary>
public static void AddFastreportParameter(Dictionary<string, string> Dicparames)
{
// 生成唯一缓存键
string cacheKey = $"ReportParam_{Guid.NewGuid()}_{DateTime.Now.Ticks}";
// 将参数存储到缓存中设置5分钟过期
CacheHelper.Add(cacheKey, Dicparames, DateTimeOffset.Now.AddMinutes(CacheExpirationMinutes));
// Session只存储缓存键的引用轻量级
HttpContext.Current.Session["ReportParameterCacheKey"] = cacheKey;
}
/// <summary>
@ -82,16 +127,8 @@ namespace BLL.Common
}
}
if (printType == "1")
{
FastReport.Export.Pdf.PDFExport exp = new FastReport.Export.Pdf.PDFExport();
report.Export(exp, path);
}
else if (printType == "2")
{
FastReport.Export.OoXML.Word2007Export exportBase = new FastReport.Export.OoXML.Word2007Export();
report.Export(export: exportBase, fileName: path.Replace(oldValue: "pdf", newValue: "docx"));
}
FastReport.Export.Pdf.PDFExport exp = new FastReport.Export.Pdf.PDFExport();
report.Export(exp, path);
}
}
}

View File

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0"/>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup></configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /></startup></configuration>

View File

@ -4,4 +4,5 @@
<package id="NPOI" version="2.5.5" targetFramework="net461" />
<package id="Portable.BouncyCastle" version="1.8.9" targetFramework="net461" />
<package id="SharpZipLib" version="1.3.2" targetFramework="net461" />
<package id="System.Runtime.Caching" version="4.7.0" targetFramework="net461" />
</packages>

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<UseIISExpress>true</UseIISExpress>
<LastActiveSolutionConfig>Release|Any CPU</LastActiveSolutionConfig>
<LastActiveSolutionConfig>Debug|Any CPU</LastActiveSolutionConfig>
<Use64BitIISExpress />
<IISExpressSSLPort />
<IISExpressAnonymousAuthentication />

View File

@ -11,7 +11,7 @@
<FineUIPro DebugMode="false" Theme="Cupertino"/>
<appSettings>
<!--连接字符串-->
<add key="ConnectionString" value="Server=.\SQL2022;Database=HJGLDB_DS;Integrated Security=False;User ID=sa;Password=1111;MultipleActiveResultSets=true;Max Pool Size = 1000;Connect Timeout=1200"/>
<add key="ConnectionString" value="Server=.\MSSQLSERVER2022;Database=HJGLDB_DS;Integrated Security=False;User ID=sa;Password=1111;MultipleActiveResultSets=true;Max Pool Size = 1000;Connect Timeout=1200"/>
<!--系统名称-->
<add key="SystemName" value="诺必达焊接管理系统"/>
<add key="ChartImageHandler" value="storage=file;timeout=20;url=~/Images/;"/>
@ -67,7 +67,7 @@
<add path="ChartImg.axd" verb="GET,POST,HEAD" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
<add path="FastReport.Export.axd" verb="*" type="FastReport.Web.Handlers.WebExport"/>
</httpHandlers>
<compilation debug="false" targetFramework="4.6.1">
<compilation debug="true" targetFramework="4.6.1">
<assemblies>
<add assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>

View File

@ -29,14 +29,35 @@ namespace FineUIPro.Web.common.ReportPrint
{
get
{
return (List<DataTable>)Session["ReportDataTables"];
List<DataTable> dataTables = new List<DataTable>();
var cacheKeys = HttpContext.Current.Session["ReportDataCacheKeys"] as List<string>;
if (cacheKeys != null)
{
foreach (var key in cacheKeys)
{
var dataTable = CacheHelper.Get<DataTable>(key);
if (dataTable != null)
{
dataTables.Add(dataTable);
}
}
}
return dataTables;
}
}
public Dictionary<string, string> ParameterValues
{
get
{
return (Dictionary<string, string>)Session["ReportParameterValues"];
var cacheKey = Session["ReportParameterCacheKey"] as string;
if (!string.IsNullOrEmpty(cacheKey))
{
return CacheHelper.Get<Dictionary<string, string>>(cacheKey);
}
return null;
}
}
protected void Page_Load(object sender, EventArgs e)
@ -58,7 +79,10 @@ namespace FineUIPro.Web.common.ReportPrint
WebReport1.Report.Save(ReportPath);
}
WebReport1.ReportFile = ReportPath;
WebReport1.Prepare();
System.Threading.Tasks.Task.Run(() =>
{
WebReport1.Prepare();
});
// WebReport1.ExportPdf();
}
private void demo()

View File

@ -1,79 +1,79 @@
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
https://go.microsoft.com/fwlink/?LinkId=301879
-->
<configuration>
<appSettings>
<add key="webpages:Version" value="3.0.0.0"/>
<add key="webpages:Enabled" value="false"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<!--连接字符串-->
<add key="ConnectionString" value="Server=.\MSSQLSERVER01;Database=HJGLDB_DS;Integrated Security=False;User ID=sa;Password=1111;MultipleActiveResultSets=true;Connect Timeout=1200"/>
<add key="ConnectionString" value="Server=.\MSSQLSERVER01;Database=HJGLDB_DS;Integrated Security=False;User ID=sa;Password=1111;MultipleActiveResultSets=true;Connect Timeout=1200" />
<!--版本号-->
<add key="SystemVersion" value="WebApi_V2022-10-07-001"/>
<add key="SystemVersion" value="WebApi_V2022-10-07-001" />
<!--附件上传物理路径-->
<add key="localRoot" value="D:\HJGL\HJGL\FineUIPro.Web\"/>
<add key="localRoot" value="D:\HJGL\HJGL\FineUIPro.Web\" />
<!--每页数量-->
<add key="PageSize" value="15"/>
<add key="PageSize" value="15" />
</appSettings>
<system.web>
<compilation debug="false" targetFramework="4.6.1"/>
<httpRuntime targetFramework="4.6.1"/>
<compilation debug="false" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
</system.web>
<system.webServer>
<!-- 跨域访问必须添加 -->
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type"/>
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS"/>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0"/>
<remove name="OPTIONSVerbHandler"/>
<remove name="TRACEVerbHandler"/>
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f"/>
<bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2"/>
<assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
<bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed"/>
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0"/>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0"/>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930"/>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
<bindingRedirect oldVersion="1.0.0.0-5.2.4.0" newVersion="5.2.4.0"/>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.4.0" newVersion="5.2.4.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
</compilers>
</system.codedom>
</configuration>

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<LastActiveSolutionConfig>Release|Any CPU</LastActiveSolutionConfig>
<LastActiveSolutionConfig>Debug|Any CPU</LastActiveSolutionConfig>
<NameOfLastUsedPublishProfile>FolderProfile</NameOfLastUsedPublishProfile>
<UseIISExpress>true</UseIISExpress>
<Use64BitIISExpress />