-
Mar 18th, 2020, 08:09 AM
#1
Thread Starter
Junior Member
System.Runtime.Caching for a Datatable in a WinForms App
I have a winforms application that has an sql backend. I have a dataset for the database in my project and use table adapters. My database has a roster table that is only updated once a week, so it rarely changes. It has a lot of records in it. Each form i use that datatable, i need to do the .fill to get the data. This is slowing down my application and takes awhile to load. Want I want to do is have a class or something where when I open a form that requires the roster data to check a cache, if it exists, then use it, otherwise load it. I have been searching for the right solution for days, and I just cant figure out caching in winforms. I am by no means an expert on programming. I use VB.net.
I have found a few ideas, but do not know how to implement any of them. I know there is the ASP.NET caching method, static method, and system.runtime.caching method.
Can anyone help me put together a solution. Assume I am dumb, because I am. I just make my application work by any means necessary. I dont know what statics are, etc.
Basically I want to load my roster table into cache when it is called the first time, and then be used throughout the application from cache as needed. I want to do the same for lookuptables (that have a lot of records). When the application closes, the cache should be cleared.
-
Mar 18th, 2020, 08:57 AM
#2
Thread Starter
Junior Member
Re: System.Runtime.Caching for a Datatable in a WinForms App
I just did this: will it work?
Code:
Imports System
Imports System.Threading
Imports System.Runtime.Caching
Imports System.Windows.Forms
Public Class TaRRCache
Public Shared Function getrstr() As TaRRDataSet.TSTAFFRSTRDataTable
Dim cache As ObjectCache = MemoryCache.[Default]
If cache.Contains("rstr") Then
Return CType(cache.Get("rstr"), TaRRDataSet.TSTAFFRSTRDataTable)
Else
Dim _rstr As TaRRDataSet.TSTAFFRSTRDataTable
Using ta As New TaRRDataSetTableAdapters.TSTAFFRSTRTableAdapter
_rstr = ta.GetData
End Using
Dim cip As CacheItemPolicy = New CacheItemPolicy
cip.AbsoluteExpiration = DateTime.Now.AddMinutes(1)
cache.Add("rstr", _rstr, cip)
Return _rstr
End If
End Function
End Class
I can just call getrstr from anywhere in my app and it should return the data, correct?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|