Data caching, supported by ASP.NET, allows you to store full-fledged object in the cache. You can use data caching through the Cache object which is available through the Context.Cache property in your web service code. With this object you can temporarily store information that is expensive to create that the web method can reuse it for other calls by other clients.
You can create a BooksService that provides two GetBooks() methods, one of which accepts an author parameter. To ensure best possible performance but cut down on the amount of data in the cache, you can store in the cache as a single object the full books DataSet. Then, when a client calls the version of GetBooks() that requires an author parameter, you need to filter out the rows for the author the client requested.
The following code shows this approach at work. The first step is to create a private method that uses the cache, called GetBooksDataSet():
Private Function GetBooksDataSet() As DataSet
Dim Ds As DataSet
If Context.Cache(“BooksDataSet”) IsNot Nothing Then
‘ Retrieve it from the cache
Ds = DirectCast(Context.Cache(“BooksDataSet”), DataSet)
Else
‘ Retrieve it from the database.
Dim Sql As String = “SELECT ISBN, Title, AuthorFName, AuthorLName, Price FROM Books”
Dim Con As New SqlConnection(connectionString)
Dim Da As New SqlDataAdapter(Sql, Con)
Ds = New DataSet()
Da.Fill(Ds, “Books”)
‘ Track when the DataSet was created. You can retrieve this information in your client
‘ to test that caching is working.
Ds.ExtendedProperties.Add(“CreatedDate”, DateTime.Now)
‘ Store it in the cache for ten minutes.
Context.Cache.Insert(“BooksDataSet”, Ds, Nothing, DateTime.Now.AddMinutes(10), TimeSpan.Zero)
End If
Return Ds
End Function
Both the GetBooks() and GetBooksByAuthor() methods can use the private GetBooksDataSet() method. The difference is that GetBooksByAuthor () loops through the records and manually removes each record that doesn’t match the author. Here are both versions:
<WebMethod(Description:=”Returns the full list of books.”)> _
Public Function GetBooks() As DataSet
Return GetBooksDataSet()
End Function
<WebMethod(Description:=”Returns the full list of books by author.”)> _
Public Function GetBooksByAuthor(AuthorFName As String, AuthorLName As String) As DataSet
‘ Copy the DataSet.
Dim DsFiltered As DataSet = GetBooksDataSet().Copy()
‘ Remove the rows manually.
‘ In this way you can protect your code to SQL injection attacks,
‘ instead than using the DataTable.Select() method
For Each Row As DataRow In DsFiltered.Tables(0).Rows
‘ Perform a case-insensitive compare.
If ([String].Compare(Row(“AuthorFName”).ToString(), AuthorFName.ToUpper(), True) <> 0) AndAlso ([String].Compare(Row(“AuthorLName”).ToString(), AuthorLName.ToUpper(), True) <> 0) Then
Row.Delete()
End If
Next
‘ Remove these rows permanently.
DsFiltered.AcceptChanges()
Return DsFiltered
End Function
Note: You should specify the amount of time to cache information depending on how long the underlying data will remain valid.