asp.net mvc---session cache (10)
1、记录用户访问人数
首先定义一个用户访问类型
public enum VisitorTypes {
UnknownVisitor = 0,
KnownVisitor = 1
}
UnknownVisitor = 0,
KnownVisitor = 1
}
然后建立一个类
public class SiteVisitors {
private static SiteVisitors instance = new SiteVisitors();
private SiteVisitors() { }
public int UnknownVisitorCount { get; private set; }
public int KnownVisitorCount { get; private set; }
public static SiteVisitors Instance {
get {
return instance;
}
}
private static SiteVisitors instance = new SiteVisitors();
private SiteVisitors() { }
public int UnknownVisitorCount { get; private set; }
public int KnownVisitorCount { get; private set; }
public static SiteVisitors Instance {
get {
return instance;
}
}
//user arrives
public void UnknownVisitorArrived() {
SessionWrapper.SetUnknownType();
UnknownVisitorCount++;
}
//known user arrives
public void KnownVisitorArrives() {
SessionWrapper.SetKnownType();
KnownVisitorCount++;
}
//user logs in
public void UnknownVisitorLoggedOn() {
SessionWrapper.SetKnownType();
UnknownVisitorCount--;
KnownVisitorCount++;
}
//user logs out
public void KnownVisitorLoggedOut() {
SessionWrapper.SetUnknownType();
UnknownVisitorCount++;
KnownVisitorCount--;
}
//user session expires
public void VisitorLeft() {
if (SessionWrapper.GetType() ==
VisitorTypes.KnownVisitor)
KnownVisitorLeft();
else
UnknownVisitorLeft();
}
SessionWrapper.SetUnknownType();
UnknownVisitorCount++;
}
//known user arrives
public void KnownVisitorArrives() {
SessionWrapper.SetKnownType();
KnownVisitorCount++;
}
//user logs in
public void UnknownVisitorLoggedOn() {
SessionWrapper.SetKnownType();
UnknownVisitorCount--;
KnownVisitorCount++;
}
//user logs out
public void KnownVisitorLoggedOut() {
SessionWrapper.SetUnknownType();
UnknownVisitorCount++;
KnownVisitorCount--;
}
//user session expires
public void VisitorLeft() {
if (SessionWrapper.GetType() ==
VisitorTypes.KnownVisitor)
KnownVisitorLeft();
else
UnknownVisitorLeft();
}
}
建立一个静态类
public static class SessionWrapper {
private const string c_visitorType = "VisitorType";
public static void SetUnknownType() {
HttpContext.Current.Session.Add(c_visitorType,
VisitorTypes.UnknownVisitor);
}
public static void SetKnownType() {
HttpContext.Current.Session.Add(c_visitorType,
VisitorTypes.KnownVisitor);
}
public static new VisitorTypes GetType() {
VisitorTypes result = VisitorTypes.UnknownVisitor;
if (HttpContext.Current.Session[c_visitorType] != null)
result = (VisitorTypes)HttpContext.Current.Session
[c_visitorType];
return result;
}}
private const string c_visitorType = "VisitorType";
public static void SetUnknownType() {
HttpContext.Current.Session.Add(c_visitorType,
VisitorTypes.UnknownVisitor);
}
public static void SetKnownType() {
HttpContext.Current.Session.Add(c_visitorType,
VisitorTypes.KnownVisitor);
}
public static new VisitorTypes GetType() {
VisitorTypes result = VisitorTypes.UnknownVisitor;
if (HttpContext.Current.Session[c_visitorType] != null)
result = (VisitorTypes)HttpContext.Current.Session
[c_visitorType];
return result;
}}
使用:
protected void Session_Start() {
if(Request.IsAuthenticated)
SiteVisitors.Instance.KnownVisitorArrives();
else
SiteVisitors.Instance.UnknownVisitorArrived();
}
protected void Session_End() {
SiteVisitors.Instance.VisitorLeft();
}
SiteVisitors.Instance.KnownVisitorArrives();
else
SiteVisitors.Instance.UnknownVisitorArrived();
}
protected void Session_End() {
SiteVisitors.Instance.VisitorLeft();
}
前台调用
<div id="logindisplay">
<% Html.RenderPartial("LogOnUserControl"); %><br />
Known visitor count: <%:
StateExamples.Models.SiteVisitors.Instance
.KnownVisitorCount %> - Unknown visitor count:
<%:
StateExamples.Models.SiteVisitors.Instance.UnknownVisitorCount
%>
</div>
Known visitor count: <%:
StateExamples.Models.SiteVisitors.Instance
.KnownVisitorCount %> - Unknown visitor count:
<%:
StateExamples.Models.SiteVisitors.Instance.UnknownVisitorCount
%>
</div>
2、使用sessionwap记录访问信息
public static class SessionWrapper {
private const string c_account = "Account";
public static Account Account {
get { return GetObjectFromSession(c_account) as Account; }
set {
if (value == null)
ClearItemFromSession(c_account);
else
SetItemInSession(value, c_account);
}
}
private static string GetStringFromSession(string key) {
return GetObjectFromSession(key).ToString();
}
private static int GetIntFromSession(string key) {
return (int)GetObjectFromSession(key);
}
private static object GetObjectFromSession(string key) {
return HttpContext.Current.Session[key];
}
public static Account Account {
get { return GetObjectFromSession(c_account) as Account; }
set {
if (value == null)
ClearItemFromSession(c_account);
else
SetItemInSession(value, c_account);
}
}
private static string GetStringFromSession(string key) {
return GetObjectFromSession(key).ToString();
}
private static int GetIntFromSession(string key) {
return (int)GetObjectFromSession(key);
}
private static object GetObjectFromSession(string key) {
return HttpContext.Current.Session[key];
}
private static void SetItemInSession(object item, string key) {
HttpContext.Current.Session.Add(key, item);
}
private static void ClearItemFromSession(string key) {
HttpContext.Current.Session.Remove(key);
}
}
在登录上使用
SessionWrapper.Account = new AccountService() .GetAccountByUsername(userName);
在前台使用 :
<div id="logindisplay">
<% Html.RenderPartial("LogOnUserControl"); %><br />
<%= StateExamples.Models.SessionWrapper.Account != null
? "Welcome " +
StateExamples.Models.SessionWrapper.Account.Username + "
from session wrapper!" : "" %>
<% Html.RenderPartial("LogOnUserControl"); %><br />
<%= StateExamples.Models.SessionWrapper.Account != null
? "Welcome " +
StateExamples.Models.SessionWrapper.Account.Username + "
from session wrapper!" : "" %>
</div>
3、使用cache
public class CacheWrapper
public class CacheWrapper {
private List<object> GetCacheItems(string[] keys) {
IDictionaryEnumerator theCache =
HttpContext.Current.Cache.GetEnumerator();
List<object> results = new List<object>();
while (theCache.MoveNext()) {
if (keys.Contains(theCache.Key))
results.Add(theCache.Value);
}
return results;
}
private List<object> GetCacheItems(string[] keys) {
IDictionaryEnumerator theCache =
HttpContext.Current.Cache.GetEnumerator();
List<object> results = new List<object>();
while (theCache.MoveNext()) {
if (keys.Contains(theCache.Key))
results.Add(theCache.Value);
}
return results;
}
private object GetCachItem(string key) {
if (HttpContext.Current.Cache[key] != null)
return HttpContext.Current.Cache[key];
return null;
}
private void AddCacheItems(Dictionary<string, object> items) {
foreach (KeyValuePair<string, object> item in items) {
AddCacheItem(item.Key, item.Value);
}
}
private void AddCacheItem(string key, object item) {
HttpContext.Current.Cache.Add(key, item, null, //dependencies
DateTime.MaxValue, //absolute expiration
new TimeSpan(0, 1, 0, 0), //sliding expiration
CacheItemPriority.Default, //priority
null); //callback
}
return HttpContext.Current.Cache[key];
return null;
}
private void AddCacheItems(Dictionary<string, object> items) {
foreach (KeyValuePair<string, object> item in items) {
AddCacheItem(item.Key, item.Value);
}
}
private void AddCacheItem(string key, object item) {
HttpContext.Current.Cache.Add(key, item, null, //dependencies
DateTime.MaxValue, //absolute expiration
new TimeSpan(0, 1, 0, 0), //sliding expiration
CacheItemPriority.Default, //priority
null); //callback
}
public void PutProducts(List<Product> products) {
Dictionary<string, object> itemsToCache = new
Dictionary<string, object>();
foreach (Product product in products) {
itemsToCache.Add(product.Name, product);
}
AddCacheItems(itemsToCache);
}
public List<Product> GetProducts(string[] productNames) {
List<Product> results = new List<Product>();
List<object> cacheItems = GetCacheItems(productNames);
foreach (object cacheItem in cacheItems) {
results.Add(cacheItem as Product);
}
return results.OrderBy(p => p.Name).ToList();
}
把产品预装
private void PreLoadProductCatalog() {
List<Product> products = new ProductService().GetProducts();
new CacheWrapper().PutProducts(products);
new CacheWrapper().PutProducts(products);
}
然后如何获取
List<Product> products = new CacheWrapper().GetProducts(new[] {
"Name1", "Name5", "Name98", "Name39", "Name88", "Name34"
});
"Name1", "Name5", "Name98", "Name39", "Name88", "Name34"
});
return View(products);
4、使用页面缓存
在action 上加
[OutputCache(Duration = (60 * 60), VaryByParam = "none")]
5、