打印采用 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
+64 -27
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);
}
}
}