HttpRuntime.Cache doesn't appear to be working

700 Views Asked by At

I am trying to build a system to list items within my Back office system on eBay via the eBay ASP.NET SDK.

Since loading eBay categories isn't terribly fast, and infrequently changing, I figured I'd cache the responses from the requests I'll make.

I have a function which returns a list of eBay categories as an eBay CategoryTypeCollection and I am trying to Cache it, then check for it and retrieve new if necessary:

Dim CategoryList As New CategoryTypeCollection
If HttpRuntime.Cache.Get("eBayCategories") Is Nothing Then
    CategoryList = Categories.DisplayCategories()
    HttpRuntime.Cache.Insert("eBayCategories", CategoryList, Nothing, DateTime.UtcNow.AddMinutes(30), TimeSpan.FromMinutes(0))
Else
    CategoryList = HttpRuntime.Cache.Get("eBayCategories")
End If

However, it is always getting new data.

This is part of a much bigger project, and caching is working for other things, I can't see what I am doing wrong here?

1

There are 1 best solutions below

3
Kevin Panko On

The cache is expiring immediately because you specify 0 minutes to expire.

Change it to Cache.NoSlidingExpiration instead.

HttpRuntime.Cache.Insert("eBayCategories", connectionString, Nothing, DateTime.UtcNow.AddMinutes(30), Cache.NoSlidingExpiration)

See MSDN Cache.Insert.