缓存问题 [英] Cache Issues

查看:60
本文介绍了缓存问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在从数据库中检索数据并使用

将其存储在缓存对象上以下代码:


HttpContext.Current.Cache.Insert(

cacheItemKey,

contentDS,//带有我的数据的数据集

null,

DateTime.Now.AddSeconds(600),

TimeSpan.Zero

);


它工作正常但我有一个奇怪的行为我找不到原因:


我点击网站周围来测试缓存的速度和使用情况(使用

TRACE)。


我随机得到不同的结果(来自我的追踪):


1已加载从缓存2.251844

这是太长了,因为其他时间我得到:


1从缓存加载0.002361

没有什么变化但是出于某种原因,突然间需要花费0.002

秒的操作决定花费2.25秒。


在这两个实例中,数据都是从c加载的疼痛,否则我的

跟踪会输出从资源中加载。


有什么想法吗?


谢谢,

费尔南多

解决方案

服务器上是否还有其他事情导致延迟?

也许你可以告诉我们代码..


-

Scott
http://www.OdeToCode.com/blogs/scott/


2004年10月23日星期六21:20:33 -0700,Fernando Chilvarguer

< fe ****** @ nospam-impex.com>写道:



我正在从数据库中检索数据并使用以下代码将其存储在Cache对象中:

HttpContext.Current.Cache.Insert(
cacheItemKey,
contentDS,//带有我的数据的数据集
null,
DateTime.Now.AddSeconds( 600),
TimeSpan.Zero


它工作正常,但我有一个奇怪的行为,我找不到原因:

我点击网站来测试缓存的速度和使用情况(使用
TRACE)。

我随机得到不同的结果(来自我的追踪):

1从缓存加载2.251844

这是太长了,因为其他时间我得到:

1从缓存加载0.002361

没有什么变化但出于某种原因,突然间需要0.002秒的操作决定耗时2.25秒。

在这两种情况下,数据都是从缓存中加载的,否则我的跟踪会输出加载f任何想法?

感谢,
Fernando

你好费尔南多,


至于你提到的问题,我认为通常缓存数据'

的加载行为应该是在缓存对象的生命周期中也一样。

你在同一个地方提到的问题(在同一页的代码中

你加载缓存的地方)对象)或在不同的地方?另外,你会提供详细的代码如何跟踪应用程序的时间

从缓存中加载数据吗?

如果你有任何其他调查结果,请随时在此发布。谢谢。

问候,


Steven Cheng

微软在线支持


获取安全! www.microsoft.com/security

(此帖子按原样提供,不作任何保证,并且不授予

权利。)


Hi Steven,


问题出现在不同的页面上。所有页面都访问我创建的相同的

缓存函数。

行为是随机的。所以我测试了第1页。我得到了不同的行为,

然后我测试第2页我得到了同样有趣的行为。

换句话说,有时缓存需要2秒以上才能返回数据,

有时需要0.002秒。

我确保我停止了机器中任何可能影响

结果的过程,因为它可以在后台进行操作(例如ActiveSync,打印

后台处理程序等等) 。)。

(WinXP Pro,.Net 1.1 VS 2003)


这是代码。


1.我创建了一个委托函数:


公共委托对象CacheLoader(string cacheItemKey);


2.然后我创建了方法实现缓存加载模式


公共静态对象GetCacheItem(string cacheItemKey,CacheLoader

anyCacheLoader)

{

HttpContext context = HttpContext.Current;

//获取缓存项的本地引用

object cacheItem = context.Cache [cacheItemKey];

//如果本地引用为null,则加载c疼痛

if(cacheItem == null)

{

#region从CacheLoader委托加载

cacheItem = anyCacheLoader (cacheItemKey);

// trace

context.Trace.Warn(cacheItemKey +"从资源加载);

#endregion

}

其他

{

// trace

context.Trace.Warn(cacheItemKey +从缓存中加载);

}

//返回

返回cacheItem;

}


3.这是检索数据的方法


公共静态对象GetPageContent(string cacheItemKey)

{

#region创建与SQL Server的连接

SqlConnection myConn;

string sConnString ="" ;;

sConnString =" SERVER =" + ConfigurationSettings.AppSettings [" sServer"] +

ETC ....

myConn = new SqlConnection(sConnString);

try

{

myConn.Open();

}

catch

{< br $>
....

}

#endregion

string sqlstr =" select * from XXX其中WWW_Status = 1 AND Page_ID =" +

cacheItemKey +"按Content_OrderBy排序" ;;

SqlCommand myCommand = new SqlCommand(sqlstr,myConn);

SqlDataAdapter myDA = new SqlDataAdapter();

myDA。 SelectCommand = myCommand;

DataSet contentDS = new DataSet();

myDA.Fill(contentDS," Content");


#region绝对过期插入缓存

HttpContext.Current.Cache.Insert(

cacheItemKey,

contentDS,

null,

DateTime.Now.AddSeconds(600),

TimeSpan.Zero);

#endregion

返回contentDS;

}


4.这就是我从页面调用所有内容的方式:


public DataSet getGenericContent(string strPageID)

{

DataSet contentDS =(DataSet)CacheLoaders.GetCacheItem(strPageID,new

CacheLoader(CacheLoaders) .GetPageContent));

返回contentDS;

}


除了这个奇怪的例外,一切都很好用行动。


感谢您的帮助。


Fernando

" Steven Cheng [MSFT]" <,V - ****** @ online.microsoft.com>在消息中写道

新闻:NY ************** @ cpmsftngxa10.phx.gbl ...

嗨Fernando,你提到的问题是什么在同一个地方(在同一页面的代码中加载缓存对象)或在不同的地方?另外,
会提供详细的代码如何跟踪应用程序从缓存中加载数据的时间?
如果您有任何其他发现,请随意在这里发帖。
谢谢。

问候,

Steven Cheng
微软在线支持

获得安全! www.microsoft.com/security
(此帖已提供按原样,没有保证,也没有赋予
权利。)




Hi,

I''m retriving data from a database and storing it on the Cache Object using
the following code:

HttpContext.Current.Cache.Insert(
cacheItemKey,
contentDS, //THE DATASET WITH MY DATA
null,
DateTime.Now.AddSeconds(600),
TimeSpan.Zero
);

It works fine but I''m having a strange behaviour I cannot find the cause:

I click around the site to test the speed and the usage of the cache (using
TRACE).

I get to different results at random (from my trace):

1 loaded from cache 2.251844
this is TOO LONG since the other times I get:

1 loaded from cache 0.002361
Nothing changes but for some reason, suddenly the operation that takes 0.002
seconds decide to take 2.25 seconds.

In both instances the data is being loaded from the cache, otherwise my
trace would output "loaded from resource".

Any ideas?

Thanks,
Fernando

解决方案

Is there anything else happening on the server to cause latency?
Perhaps you could show us the code..

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Sat, 23 Oct 2004 21:20:33 -0700, "Fernando Chilvarguer"
<fe******@nospam-impex.com> wrote:

Hi,

I''m retriving data from a database and storing it on the Cache Object using
the following code:

HttpContext.Current.Cache.Insert(
cacheItemKey,
contentDS, //THE DATASET WITH MY DATA
null,
DateTime.Now.AddSeconds(600),
TimeSpan.Zero
);

It works fine but I''m having a strange behaviour I cannot find the cause:

I click around the site to test the speed and the usage of the cache (using
TRACE).

I get to different results at random (from my trace):

1 loaded from cache 2.251844
this is TOO LONG since the other times I get:

1 loaded from cache 0.002361
Nothing changes but for some reason, suddenly the operation that takes 0.002
seconds decide to take 2.25 seconds.

In both instances the data is being loaded from the cache, otherwise my
trace would output "loaded from resource".

Any ideas?

Thanks,
Fernando




Hi Fernando,

As for the problem you mentioned, I think generally the cache data''s
loading behavior should be the same during the cached object''s lifecycle.
Is the problem you mentioned in the same place(in the same page''s code
where you load the cache object) or in different place? In addition, would
you provide the detailed code how to trace the time when your application
loading the data from cache?
If you have any other findings, please also feel free to post here. Thanks.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Hi Steven,

The problem occurs on different pages. All pages are accessing the same
cache functions I created.
The behavior is random. So I test "page 1" and I get the different behavior,
then I test "page 2" and I get the same interesting behavior.
In other words, sometimes the cache takes over 2 seconds to return the data,
sometimes it takes 0.002 seconds.
I made sure I stopped any process in the machine that could influence the
result by sudenlly acting on the background (such as ActiveSync, print
Spooler, etc.).
(WinXP Pro, .Net 1.1 VS 2003)

Here''s the code.

1. I created a delegate function:

public delegate object CacheLoader(string cacheItemKey);

2. Then I created the Method implementing Cache Loading Pattern

public static object GetCacheItem(string cacheItemKey, CacheLoader
anyCacheLoader)
{
HttpContext context = HttpContext.Current;
// acquire local reference of cache item
object cacheItem = context.Cache[cacheItemKey];
// if local reference is null, load the cache
if (cacheItem==null)
{
#region Load from CacheLoader delegate
cacheItem = anyCacheLoader(cacheItemKey);
// trace
context.Trace.Warn(cacheItemKey + " loaded from resource");
#endregion
}
else
{
// trace
context.Trace.Warn(cacheItemKey + " loaded from cache");
}
// return
return cacheItem;
}

3. This is the method that retrieves the data

public static object GetPageContent(string cacheItemKey)
{
#region Create connection to SQL Server
SqlConnection myConn;
string sConnString = "";
sConnString = "SERVER=" + ConfigurationSettings.AppSettings["sServer"] +
ETC....
myConn = new SqlConnection(sConnString);
try
{
myConn.Open();
}
catch
{
....
}
#endregion
string sqlstr = "select * from XXX where WWW_Status = 1 AND Page_ID=" +
cacheItemKey + " order by Content_OrderBy";
SqlCommand myCommand = new SqlCommand(sqlstr,myConn);
SqlDataAdapter myDA = new SqlDataAdapter();
myDA.SelectCommand = myCommand;
DataSet contentDS = new DataSet();
myDA.Fill(contentDS, "Content");

#region Insert into Cache with absolute expiration
HttpContext.Current.Cache.Insert(
cacheItemKey,
contentDS,
null,
DateTime.Now.AddSeconds(600),
TimeSpan.Zero);
#endregion
return contentDS;
}

4. This is how I call everything from the page:

public DataSet getGenericContent(string strPageID)
{
DataSet contentDS = (DataSet)CacheLoaders.GetCacheItem(strPageID, new
CacheLoader(CacheLoaders.GetPageContent));
return contentDS;
}

It all works great with the exception of this weird behavior.

THANK YOU for any help.

Fernando
"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:NY**************@cpmsftngxa10.phx.gbl...

Hi Fernando,

As for the problem you mentioned, I think generally the cache data''s
loading behavior should be the same during the cached object''s lifecycle.
Is the problem you mentioned in the same place(in the same page''s code
where you load the cache object) or in different place? In addition,
would
you provide the detailed code how to trace the time when your application
loading the data from cache?
If you have any other findings, please also feel free to post here.
Thanks.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)




这篇关于缓存问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆