存储在缓存中的数据始终为空
本文关键字:数据 缓存 存储 | 更新日期: 2023-09-27 18:35:00
我正在做一个网络项目。 我想存储我的数据(来自 WCFservice(。 然后,当来自客户端的请求时,我想检查数据库值是否已更改。 我正在通过 WCF 方法进行此检查。 当缓存数据中的时间字段不等于来自 WCF 方法的时间值时, 代码应该调用 WCF 方法来从数据库中获取新数据。新数据将存储在缓存中,并显示在页面上。
呜呜... :/我运行项目显示我的 XXX 页面,然后显示另一个页面,然后重新显示我的 XXX 页面,那是什么!!我的数据(存储在缓存中(绝对,真的,非常空...代码在这里:听到你的完美建议,我会很高兴。为什么它应该是空的,我怎样才能从缓存中检索它。第一种方法在WCF中,第2种方法已经加载了有问题的页面
public MeasurementSystemPageWcf GetActiveSystemMeasurementPage(bool RequestOnlyInfo)
{
Session session = DBUtil.GetSession();
XPClassInfo MeasurementSystemPageClass = session.GetClassInfo(typeof(MeasurementSystemPage));
string QCriteria = "ISACTIVE =='true'";
CriteriaOperator criteria = CriteriaOperator.Parse(QCriteria);
SortingCollection sortProps = new SortingCollection(null);
sortProps.Add(new SortProperty("", SortingDirection.Ascending));
CollectionCriteriaPatcher patcher = new CollectionCriteriaPatcher(false, session.TypesManager);
ICollection MeasurementSystemPageCollection = session.GetObjects(MeasurementSystemPageClass, criteria, sortProps, 0, patcher, true);
MeasurementSystemPageWcf RetVal = new MeasurementSystemPageWcf();
if (MeasurementSystemPageCollection != null && MeasurementSystemPageCollection.Count != 0)
{
if (!RequestOnlyInfo)
{
foreach (MeasurementSystemPage item in MeasurementSystemPageCollection)
{
// sadece bir tane aktif sayfa gelmesi gerektiği varsayılarak kolleksiyondaki ilk sayfa alınacak. break ile sağlanıyor
RetVal.PAGEHEADER = item.PAGEHEADER;
RetVal.CONTENT = item.CONTENT;
RetVal.CREATEDATE = item.CREATEDATE;
RetVal.EDITDATE = item.EDITDATE;
break;
}
}
else if (RequestOnlyInfo)
{
foreach (MeasurementSystemPage item in MeasurementSystemPageCollection)
{
// sadece bir tane aktif sayfa gelmesi gerektiği varsayılarak kolleksiyondaki ilk sayfa alınacak. break ile sağlanıyor
RetVal.EDITDATE = item.EDITDATE;
break;
}
}
}
else
{
RetVal.ER.HasError = true;
RetVal.ER.ErrorMethod = "MeasurementSystemPageWcf GetActiveSetMeasurementPage()";
RetVal.ER.ErrorMessage = "Henüz İçerik Girilmemiş";
}
return RetVal;
}
protected void Page_Load(object sender, EventArgs e)
{
XSensServiceClient client = new XSensServiceClient();
MeasurementSystemPageWcf page_in_cache = (MeasurementSystemPageWcf)Cache["MeasurementSystemPage"];
if (page_in_cache == null)
{
MeasurementSystemPageWcf active_page = client.GetActiveSystemMeasurementPage(false);
if (active_page.ER.HasError)
{
lbl_Body.Text = active_page.ER.ErrorMessage;
Page.Title = active_page.ER.ErrorMessage;
}
else
{
Cache.Insert("MeasurementSystemPage", active_page, null, DateTime.Now.AddDays(7), TimeSpan.Zero, CacheItemPriority.NotRemovable, null);
//Cache["MeasurementSystemPage"] = active_page;
Page.Title = active_page.PAGEHEADER;
lbl_Body.Text = active_page.CONTENT;
}
}
else
{
MeasurementSystemPageWcf active_page_from_wcf = client.GetActiveSystemMeasurementPage(true); // buraya sadece edit zamanını çeken bir method yazılacak, her karşılaştırmada bütün sayfa verisini çekmemek için.
MeasurementSystemPageWcf active_page_from_cache = (MeasurementSystemPageWcf)Cache["MeasurementSystemPage"];
if (active_page_from_cache.EDITDATE < active_page_from_wcf.EDITDATE)
{
MeasurementSystemPageWcf updated_page = client.GetActiveSystemMeasurementPage(false);
Cache.Insert("MeasurementSystemPage", updated_page, null, DateTime.Now.AddDays(7), TimeSpan.Zero, CacheItemPriority.NotRemovable, null);
Page.Title = updated_page.PAGEHEADER;
lbl_Body.Text = updated_page.CONTENT;
}
else
{
Page.Title = active_page_from_cache.PAGEHEADER;
lbl_Body.Text = active_page_from_cache.CONTENT;
}
}
web.config 如下:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="devExpress">
<section name="themes" type="DevExpress.Web.ASPxClasses.ThemesConfigurationSection, DevExpress.Web.v13.1, Version=13.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false"/>
<section name="compression" type="DevExpress.Web.ASPxClasses.CompressionConfigurationSection, DevExpress.Web.v13.1, Version=13.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false"/>
<section name="settings" type="DevExpress.Web.ASPxClasses.SettingsConfigurationSection, DevExpress.Web.v13.1, Version=13.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false"/>
<section name="errors" type="DevExpress.Web.ASPxClasses.ErrorsConfigurationSection, DevExpress.Web.v13.1, Version=13.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" requirePermission="false"/>
</sectionGroup>
</configSections>
<system.web>
<!--caching nodu sonradan eklendi-->
<caching>
<cache disableExpiration="true"/>
</caching>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="DevExpress.Web.v13.1, Version=13.1.6.0, Culture=neutral, PublicKeyToken=B88D1754D700E49A"/>
<add assembly="DevExpress.Data.v13.1, Version=13.1.6.0, Culture=neutral, PublicKeyToken=B88D1754D700E49A"/>
<add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
<add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
<add assembly="DevExpress.RichEdit.v13.1.Core, Version=13.1.6.0, Culture=neutral, PublicKeyToken=B88D1754D700E49A"/>
</assemblies>
</compilation>
<httpModules>
<add type="DevExpress.Web.ASPxClasses.ASPxHttpHandlerModule, DevExpress.Web.v13.1, Version=13.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" name="ASPxHttpHandlerModule"/>
</httpModules>
<httpHandlers>
<add type="DevExpress.Web.ASPxClasses.ASPxHttpHandlerModule, DevExpress.Web.v13.1, Version=13.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" verb="GET,POST" path="DX.ashx" validate="false"/>
</httpHandlers>
<pages>
<controls>
<add tagPrefix="ajaxToolkit" assembly="AjaxControlToolkit" namespace="AjaxControlToolkit"/>
</controls>
</pages>
</system.web>
<system.webServer>
<modules>
<add type="DevExpress.Web.ASPxClasses.ASPxHttpHandlerModule, DevExpress.Web.v13.1, Version=13.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" name="ASPxHttpHandlerModule"/>
</modules>
<validation validateIntegratedModeConfiguration="false"/>
<handlers>
<add type="DevExpress.Web.ASPxClasses.ASPxHttpHandlerModule, DevExpress.Web.v13.1, Version=13.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" verb="GET,POST" path="DX.ashx" name="ASPxHttpHandlerModule" preCondition="integratedMode"/>
</handlers>
</system.webServer>
<devExpress>
<themes enableThemesAssembly="true" styleSheetTheme="" theme="" customThemeAssemblies=""/>
<compression enableHtmlCompression="false" enableCallbackCompression="true" enableResourceCompression="true" enableResourceMerging="true"/>
<settings rightToLeft="false" doctypeMode="Xhtml"/>
<errors callbackErrorRedirectUrl=""/>
</devExpress>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IXSensService" allowCookies="true" maxReceivedMessageSize="20000000" maxBufferPoolSize="20000000">
<readerQuotas maxDepth="32" maxArrayLength="200000000" maxStringContentLength="200000000"/>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://127.0.0.1/XSensWCFService/XSENSWCFService.XSensService.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IXSensService"
contract="XSensService.IXSensService" name="WSHttpBinding_IXSensService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
DbUtil.GetSession(( 是什么样子的?问题可能在那里。您没有指定是否在 ASP.NET 中使用会话状态来保存会话,您还应该发布 web.config 会话状态设置以查看它是否已正确配置并启用
另外,您如何将数据存储在缓存中?