Which code segment should you add?

You are implementing an ASP.NET application that includes the following requirements.
Retrieve the number of active bugs from the cache, if the number is present.
If the number is not found in the cache, call a method named GetActiveBugs, and save the result under the ActiveBugs cache key.
Ensure that cached data expires after 30 seconds.
You need to add code to fulfill the requirements.
Which code segment should you add?

You are implementing an ASP.NET application that includes the following requirements.
Retrieve the number of active bugs from the cache, if the number is present.
If the number is not found in the cache, call a method named GetActiveBugs, and save the result under the ActiveBugs cache key.
Ensure that cached data expires after 30 seconds.
You need to add code to fulfill the requirements.
Which code segment should you add?

A.
int? numOfActiveBugs = (int?)Cache[“ActiveBugs”];
if (!numOfActiveBugs.HasValue)
{
int result = GetActiveBugs();
Cache.Insert(“ActiveBugs”, result, null,
DateTime.Now.AddSeconds(30), Cache.NoSlidingExpiration);
numOfActiveBugs = result;
}
ActiveBugs = numOfActiveBugs.Value;

B.
int numOfActiveBugs = (int) Cache.Get(“ActiveBugs”);
if (numOfActiveBugs != 0)
{
int result = GetActiveBugs();
Cache.Insert(“ActiveBugs”, result, null,
DateTime.Now.AddSeconds(30), Cache.NoSlidingExpiration);
numOfActiveBugs = result;
}
ActiveBugs = numOfActiveBugs;

C.
int numOfActiveBugs = 0;
if (Cache[“ActiveBugs”] == null)
{
int result = GetActiveBugs();
Cache.Add(“ActiveBugs”, result, null, DateTime.Now.AddSeconds(30), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
numOfActiveBugs = result;
}
ActiveBugs = numOfActiveBugs;

D.
int numOfActiveBugs = (int?)Cache[“ActiveBugs”];
if (!numOfActiveBugs.HasValue)
{
int result = GetActiveBugs();
Cache.Insert(“ActiveBugs”, result, null,
Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(30));
numOfActiveBugs = result;
}
ActiveBugs = numOfActiveBugs.Value;



Leave a Reply 1

Your email address will not be published. Required fields are marked *


Fred

Fred

Copy paste failure in responce C remove “Cache.NoSlidingExpiration, CacheItemPriority.Normal, null); “