Я бы сделал это с ASP.NET Cache API - вот как.
Imports System.Web
Public Class CacheManager
Private ListKey As String = "MyList"
Public Shared ReadOnly Property TypedList As List(Of Integer)
Dim cachedObject As Object
Dim myList As List (Of Integer)
Dim userCacheKey As String = ListKey & HttpContext.Current.User.Identity.Name
'First check to see if List is in the cache already
cachedObject = HttpRuntime.Cache.Get(userCacheKey)
If cachedObject Is Nothing Then
'If List isn't in the cache already then get it...
myList = Code to retrieve list members goes here
' ...and now we've got it put it in the cache
HttpRuntime.Cache..Add(key:=userCacheKey, value:=myList, absoluteExpiration:=HttpRuntime.Cache.NoAbsoluteExpiration, slidingExpiration:=New TimeSpan(0,5,0), dependencies:=Nothing, onRemoveCallback:=Nothing, priority:=CacheItemPriority.Default)
Else
'List is already in the cache but everything comes out of the cache as System.Object so cast it to List (Of Integer)
myList = DirectCast(cachedObject, List (Of Integer))
End If
'Now we have List, return it to the caller
Return myList
End Property
End Class
Это дает нам класс, который будет хранить экземпляр List <> на пользователя, который существует в памяти в течение пяти минут после последнего обращения к нему - вы можете сделать это, просто изменив длину объекта TimeSpan в slideExpiration параметр при добавлении списка в кэш.
Тогда ваше использование на странице просто:
Public Sub Page_Load (arguments)
Dim myList As List(Of Integer)
...
myList = CacheManager.TypedList
...
End Sub
<WebMethod()> Public Sub MyEventMethod(arguments)
Dim myList As List(Of Integer)
...
myList = CacheManager.TypedList
...
End Sub
Из вашего вопроса не совсем понятно (для меня), могут ли пользователи изменять свой индивидуальный список или глобальный список. Если они изменяют свой индивидуальный список, который легко обслуживать - измените свойство TypedList следующим образом:
Imports System.Web
Public Class CacheManager
Private ListKey As String = "MyList"
Public Shared Property TypedList As List(Of Integer)
Get
Dim cachedObject As Object
Dim myList As List (Of Integer)
Dim userCacheKey As String = ListKey & HttpContext.Current.User.Identity.Name
'First check to see if List is in the cache already
cachedObject = HttpRuntime.Cache.Get(userCacheKey)
If cachedObject Is Nothing Then
'If List isn't in the cache already then get it...
myList = Code to retrieve list members goes here
' ...and now we've got it put it in the cache
HttpRuntime.Cache.Add(key:=userCacheKey, value:=myList, absoluteExpiration:=HttpRuntime.Cache.NoAbsoluteExpiration, slidingExpiration:=New TimeSpan(0,5,0), dependencies:=Nothing, onRemoveCallback:=Nothing, priority:=CacheItemPriority.Default)
Else
'List is already in the cache but everything comes out of the cache as System.Object so cast it to List (Of Integer)
myList = DirectCast(cachedObject, List (Of Integer))
End If
'Now we have List, return it to the caller
Return myList
End Get
Set (ByVal value As List(Of Integer))
Dim userCacheKey As String = ListKey & HttpContext.Current.User.Identity.Name
HttpRuntime.Cache.Insert(key:=userCacheKey, value:=value, absoluteExpiration:=HttpRuntime.Cache.NoAbsoluteExpiration, slidingExpiration:=New TimeSpan(0,5,0), dependencies:=Nothing, onRemoveCallback:=Nothing, priority:=CacheItemPriority.Default)
End Set
End Property
End Class
Если какой-либо пользователь, вносящий изменения в список, изменит его для всех, я бы посмотрел на использование CacheDependency .