我使用的是asp.net mvc。如何或在哪裏存儲單個數據?例如。 SubscriptionFee
或IsSiteOffline
。哪裏存儲網站的單一數據類型
我問了一個關於用戶設置的問題here。我是否應該爲網站設置做這樣的事情,或者除了數據庫以外還有其他方法嗎?我希望我的用戶從網站本身更改這些設置。
我將使用EntityFramework code-first,並會喜歡我是否可以執行如下操作:settings.SubscriptionFee
。
我使用的是asp.net mvc。如何或在哪裏存儲單個數據?例如。 SubscriptionFee
或IsSiteOffline
。哪裏存儲網站的單一數據類型
我問了一個關於用戶設置的問題here。我是否應該爲網站設置做這樣的事情,或者除了數據庫以外還有其他方法嗎?我希望我的用戶從網站本身更改這些設置。
我將使用EntityFramework code-first,並會喜歡我是否可以執行如下操作:settings.SubscriptionFee
。
通常,您將這些設置放入Web.config文件的<appSettings>
部分。
一個標準的ASP.NET MVC 3應用程序自帶了一些設置已經(內<configuration>
元素):
<appSettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="MyCustomSetting" value="abcd1234" />
</appSettings>
引用它們在你的應用程序中使用ConfigurationManager中類:
using System.Configuration;
string value = ConfigurationManager.AppSettings["MyCustomSetting"];
正如之前所說的,雖然,最好在數據後端創建一個配置表(即SQL Server或任何你使用的)並從那裏抓取它們。
在我的一個(非MVC)應用程序中,我創建了一個靜態SysProperties類,它將使用應用程序的緩存來保持它們緩存至少5分鐘。這個例子沒有使用實體框架,但它可以很容易地適應:
public static class SysProperties
{
public static string SMTPServer
{
get
{
return GetProperty("SMTPServer").ToString();
}
}
public static decimal TicketFee
{
get
{
return Convert.ToDecimal(GetProperty("TicketFee"));
}
}
private static object GetProperty(string PropertyName)
{
object PropertyValue = null;
if (HttpContext.Current != null)
PropertyValue = HttpContext.Current.Cache[PropertyName];
if (PropertyValue == null)
{
SqlCommand cmSQL = new SqlCommand("SELECT Value FROM tblProperty WHERE Name = @PropertyName");
cmSQL.Parameters.AddWithValue("@PropertyName", PropertyName);
DataTable dt = Functions.RunSqlCommand(cmSQL);
PropertyValue = dt.Rows[0][0];
if (HttpContext.Current != null)
HttpContext.Current.Cache.Insert(PropertyName, PropertyValue, null, DateTime.UtcNow.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);
return PropertyValue;
}
else
{
return PropertyValue;
}
}
}
注意:您可以使用此與ConfigurationManager中相同的技術來從Web.config文件,而不是從檢索這些值數據庫。
只是另一個免費的,這是我用來利用SQL Server的SqlCacheDependency的一些代碼。您必須啓用SQL Server Broker,但這可以讓您將值緩存到內存中,直到SQL Server中的值發生更改。那樣,你沒有任意的5分鐘到期。
此函數旨在檢索具有兩部分標識符(如用戶的全名)的事物,因此它需要三個參數: - 在未緩存該值的情況下運行的SQL查詢 - The (即用戶ID) - 標識數據類型的任意字符串(即「UserFullName」,「UserEmail」)
你可以很容易地適應這個用單部分標識符檢索東西:
// Static constructor ensures that SqlDependency.Start is called before
// we try to use any SqlCacheDependencies
static Functions()
{
SqlDependency.Start(ConnectionString);
}
public static string GetUserFullName(string UserName)
{
return GetSqlCachedValue("SELECT FirstName + ' ' + LastName FROM dbo.tblUser WHERE UserName = @Id", UserName, "UserFullName").ToString();
}
public static string GetEventNameFromId(int Id)
{
return GetSqlCachedValue("SELECT EventName FROM dbo.tblEvents WHERE EventID = @Id", Id, "EventName").ToString();
}
private static object GetSqlCachedValue(string Query, object Id, string CacheName)
{
// Get the cache
System.Web.Caching.Cache currentCache = HttpContext.Current.Cache;
object Value = null;
// We use a standard naming convention for storing items in the application's cache
string cacheKey = string.Format("{0}_{1}", CacheName, Id.ToString());
// Attempt to retrieve the value
if (currentCache != null && currentCache[cacheKey] != null)
Value = currentCache[cacheKey];
// If the value was not stored in cache, then query the database to get the value
if (Value == null)
{
// Run the query provided to retrieve the value. We always expect the query to have the @Id parameter specified, that we can use
// to plug-in the Id parameter given to this function.
SqlCommand Command = new SqlCommand(Query);
Command.Parameters.AddWithValue("@Id", Id);
// Generate a cache dependency for this query
System.Web.Caching.SqlCacheDependency dependency = new System.Web.Caching.SqlCacheDependency(Command);
// Run the query
DataTable dt = RunSqlCommand(Command);
if (dt.Rows.Count == 1)
{
// Grab the value returned
Value = dt.Rows[0][0];
// Save the value in the cache, so next time we don't have to query SQL Server
if (currentCache != null)
{
currentCache.Insert(cacheKey, Value, dependency);
}
}
}
// return the final value
return Value;
}
最好是,特別是因爲您希望允許用戶更改這些設置,請將它們存儲在您作爲後端的任何數據存儲中,如SQL或其他。
要使用設置,您可以將它們置於應用程序緩存中,並創建對數據存儲的依賴關係,以便任何更新都會使緩存過期。你甚至可以使用靜態類,但你必須自己實現管理。