2015-10-01 34 views
0

我有一個WCF,每隔10-15分鐘由ASP.NET應用程序調用以通過電子郵件通知客戶有關內容。我需要該收集來識別郵件已經發送給哪個用戶。所以如果下一個電話來了,我可以查詢這個集合,如果郵件發送之前和最後一次是什麼時間(我設置了一個時間間隔,因此用戶不會每10-15分鐘收到一次郵件)。將數據存儲在WCF的全局集合中

我的問題是 - 當我存儲它全局,當通話結束(collection =空)時,這個集合是否到期? 將.svc類中的全局集合設置爲靜態列表是否足夠了,還是必須設置DataMember屬性? 代碼將如何查找?

那樣?

public class Service1 : IService1 
{ 
     private static List<customer> lastSendUserList = new List<customer>(); 

     void SendMail(int customerid) 
     { 
      lastSendUserList.FirstOrDefault(x => x.id == customerid); 
      . 
      // proof date etc. here and send or send not 
     } 
} 

這是否lastSendUserList留在RAM /高速緩存,直到我將其設置爲null(或重啓服務器等),所以每次電話打進來時我可以查詢?或者這個列表每當通話結束時都被gc清除?

編輯

因此,新的代碼是這樣的?

public class Service1 : IService1 
{ 
     private static List<customer> lastSendUserList = new List<customer>(); 

     void SendMail(int customerid, int setInterval) 
     { 
      customer c; 
      c = lastSendUserList.FirstOrDefault(x => x.id == customerid); 

      if(c != null && c.lastSendDate + setInterval > DateTime.Now) 
      { 
       lastSendUserList.Remove(x => x.id == c.id); 

       // start with sending EMAIL 

       lastSendUserList.Add(new customer() { id = customerid, lastSendDate = DateTime.Now }); 
      } 
     } 
} 

回答

1

假設你添加到lastSendUserList這將是始終可用,直到IIS停止/重新啓動你的工作進程。

但願customerLastSendDate !!!

此外,你應該修剪最後,以便它不會太大。所以我的代碼看起來像。

TimeSpan const ttl = TimeSpan.FromMinutes(15); 
lock (lastSendUserList) 
{ 
    lastSendUserList.Remove(x => x.lastSendDate + ttl < DateTime.Now); 
    if (lastSendUserList.Any(x => x.id == customerid)) 
    return; 
} 

// ... send the email 

lock (lastSendUserList) 
{ 
     customer.lastSendDate = DateTime.Now; 
     lastSendUserList.Add(c); 
} 

由於您處於WCF服務中,因此您必須是線程安全的。那爲什麼我有一個locklastSendUserList

+0

'customer'有一個'id'和'lastSendDate'。我通過'id'查詢獲取日期。檢查從這個日期到現在的經過時間,並將其與我設置的「時間間隔」進行比較。如果經過的時間(分鐘)大於間隔(以分鐘爲單位),我發送郵件並更新客戶'lastSendDate'。 這樣就可以像我上面的問題那樣工作了? –

+0

這個方法在設定的時間被一個單一的aspx站點調用一次 - 這個站點被服務器上的計劃任務調用(每10-15分鐘運行一次),所以我想它是線程安全的?! –

相關問題