I like to create a session value that expires in 5 minutes.
How to do that, do I need to manually compare creation time when I read it?
Session("sessionval") = myvariable
Session("sessioncreated") = now
I like to create a session value that expires in 5 minutes.
How to do that, do I need to manually compare creation time when I read it?
Session("sessionval") = myvariable
Session("sessioncreated") = now
you can not define custom timeout to a session variable.
Maybe you can use cache for this, with a unique key dependent to session.
You can set timeout to a cached item.
Cache.Insert("key_dependent_to_session", value, Nothing,
DateTime.Now.AddMinutes(5), TimeSpan.Zero)
That is correct. Session state doesn't have an Expiration concept.
If your data are not per-user, you can use Cache instead. You could even use Cache, but include the user name as part of the key, which would sort of give you an expiring session state.
If you are asking how to do it on a single variable I do not think it is possible, but you can define the session timeout period using the web.config
<system.web>
<sessionState timeout="5" />
<system.web>
this would affect all of your session objects, not just a single one.
My suggestion for giving a custom expiration date to a single item is to create a cookie, that way you can actually set it's expiration value.
Response.Cookies.Add(New Web.HttpCookie("AdminID", admin.AdminID))
Response.Cookies("AdminID").Expires = Now.AddMinutes(5)
1) Put this in web.config
<configuration>
<system.web>
<sessionState mode="InProc" timeout="5">
</sessionState>
</system.web>
</configuration>
2) Set System.Web.SessionState.HttpSessionState.Timeout = 5 mins.
I tend to agree with TheTXI.
Although a single Session item cannot be assigned a custom timeout, you should note that in InProc session mode, Sessionstate is itself added as an item to the ASP.NET cache. This enables it to have a "timeout".
In your particular scenario, a Cache object is the best suited solution.
If you want to use the SessionState you could create your own wrapper so instead of caching the actual item, you can cache the wrapper. Here is a quick dirty untested example of a wrapper that checks if the item is null, or if it has expired it will call a Func that you can provide a refreshed item.
The Func takes the last set value so if you can determine if the value is still valid you could avoid reloading it.
public class TimeExpirationItem<T>
{
private T _item=default(T);
private TimeSpan _expirationDuration;
private Func<T> _getItemFunc;
private DateTime _expiresTime;
public TimeExpirationItem(TimeSpan expirationDuration, Func<T, T> getItemFunc)
{
this._getItemFunc = getItemFunc;
this._expirationDuration = expirationDuration;
}
public T Item
{
get
{
if (_item == null || ItemIsExpired())
{
_item = _getItemFunc(_item);
_expiresTime = DateTime.Now.Add(_expirationDuration);
}
return _item;
}
}
public bool ItemIsExpired()
{
return DateTime.Now > _expiresTime;
}
}
Again this code is provided as is with no warranty and it is untested but is an example of the things you can do.
Using it would be something like the following:
Session.Add("ObjectKey",new TimeExpirationItem<MyObject>(new TimeSpan(0,0,5),mo=>MyObjectRepository.GetItemByLogin(Request.User.Identity.Name));