第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

重新進入頁面時Asp.Net緩存被清除

重新進入頁面時Asp.Net緩存被清除

C#
30秒到達戰(zhàn)場 2023-07-22 16:15:29
我有一個 asp.net 函數(shù),用作報告的數(shù)據(jù)源。當(dāng)我第一次運行該函數(shù)時,我緩存了數(shù)據(jù)集,發(fā)現(xiàn)緩存創(chuàng)建成功,因為緩存計數(shù)為1。但是,當(dāng)我再次進入該函數(shù)時,我無法獲取緩存內(nèi)容。并且緩存計數(shù)為零。似乎由于某種原因緩存被清除了。如何找出重新進入頁面時緩存計數(shù)為零的原因以及如何使緩存發(fā)揮作用?這是我的代碼://using System.Web.Caching;using System.Runtime.Caching;namespace Sustainability.BusinessObject.Client{    public class ReportManager    {        protected static MemoryCache CACHE = new MemoryCache("MySQLDataProvider_Cache");        public static DataSet KPISummaryReport(int companyID, int fromYear, int fromMonth, int toYear, int toMonth, string locationIDs, bool hideIfNoValue, string lang)        {            HttpResponseMessage result = null;                        DataSet ds = null;             try            {                string cacheKey = "kpi_" + companyID + "_" + fromYear + "_" + fromMonth + "_" + toYear + "_" + toMonth + "_" + locationIDs;                Logger.Log(string.Format("Cache count of reentering the code {0}", CACHE.GetCount()));                ds = CACHE.Get(cacheKey) as DataSet;                if (ds != null)                {                    return ds;                }                else                {                    ds = Util.GetData(_sustainabilityServiceURL, requestUri, out result);                    var policy = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now.AddMinutes(60d) };                    CACHE.Add(cacheKey, ds, policy);                    DataSet ds1 = CACHE.Get(cacheKey) as DataSet;                    if (ds1 != null)                    {                        Logger.Log("Create Cache Succesfully");                    }                    else                    {                        Logger.Log("Create Cache Failed");                    }                    Logger.Log(string.Format("Cache count after create cache {0}",CACHE.GetCount()));                }         }   }
查看完整描述

1 回答

?
哈士奇WWW

TA貢獻1799條經(jīng)驗 獲得超6個贊

檢查您用于緩存的數(shù)據(jù)庫,我的第一個猜測是實際上沒有存儲任何內(nèi)容。


使用 IoC,正確的設(shè)置看起來像是向服務(wù)添加分布式緩存


services.AddDistributedSqlServerCache(options =>

{

? ? options.ConnectionString = _config["SQLDataProvider_Cache"];

? ? options.SchemaName = "dbo";

? ? options.TableName = "TestCache";

});

您可以使用兩種類型的緩存策略:


(DateTimeOffSet) CacheItemPolicy.AbsoluteExpiration 在初始設(shè)置的固定時間后過期


(DateTimeOffSet) CacheItemPolicy.SlidingExpiration 自上次訪問起的固定時間后過期


通常您會想要使用 SlidingExpiration 之一,但當(dāng)您定義絕對時,注冊將


public void Configure(IApplicationBuilder app, IHostingEnvironment env,?

? ? IApplicationLifetime lifetime, IDistributedCache cache)

{

? ? lifetime.ApplicationStarted.Register(() =>

? ? {

? ? ? ? var currentTimeUTC = DateTime.UtcNow.ToString();

? ? ? ? var encodedCurrentTimeUTC = Encoding.UTF8.GetBytes(currentTimeUTC);

? ? ? ? var options = new DistributedCacheEntryOptions()

? ? ? ? ? ? .SetAbsoluteExpiration(TimeSpan.FromHours(1));

? ? ? ? cache.Set("cachedTimeUTC", encodedCurrentTimeUTC, options);

? ? });

存儲庫本身不應(yīng)包含靜態(tài)成員(或僅包含記錄器)。為該存儲庫添加接口將改進測試和模擬功能,并且成為使用 IoC 傳遞此信息的默認方式


public class ReportRepository : IReportRepository

{

? ? private readonly IAppCache _cache;

? ? private readonly ILogger _logger;

? ? private SomeService _service;


? ? public string ServiceUrl { get; set; }

? ? public string RequestUri { get; set; }

?

? ? public ReportRepository(IAppCache appCache, ILogger<ShowDatabase> logger, SomeService service)

? ? {

? ? ? ? _service = service;

? ? ? ? _logger = logger;

? ? ? ? _cache = appCache;

? ? }

?

? ? public async Task<List<Show>> GetShows()

? ? {? ??

? ? ? ? var cacheKey = "kpi_{companyID}_{fromYear}_{fromMonth}_{toYear}_{toMonth}_{locationIDs}";

? ? ? ? Func<Task<List<DataSet>>> reportFactory = () => PopulateReportCache();

? ? ? ? //if you do it in DistributedCacheEntryOptions you do not need to set it here

? ? ? ? var absoluteExpiration = DateTimeOffset.Now.AddHours(1);

? ? ? ? var result = await _cache.GetOrAddAsync(cacheKey, reportFactory, absoluteExpiration);

? ? ? ? return result;

? ? }

??

? ? private async Task<List<DataSet>> PopulateReportCache()

? ? {

? ? ? ? List<DataSet> reports = await _service.GetData(ServiceUrl, RequestUri, out result);

? ? ? ? _logger.LogInformation($"Loaded {reports.Count} report(s)");

? ? ? ? return reports.ToList(); //I would have guessed it returns out parameter result ...

? ? }

}

查看完整回答
反對 回復(fù) 2023-07-22
  • 1 回答
  • 0 關(guān)注
  • 171 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號