Caching data and resetting it for a Web Part

Daniel Reed asked on July 8, 2015 03:38

I am currently storing a complex object for every instance of a web part and widget that I drop on a page. I am doing this by creating a web part and caching the object for 10min using this.ShortClientID as the key.

Now I don't know if even that part is the best way to do things. But basically I want to be able to invalidate the cache if a user changes a web part property called 'Record Number'.I also have a custom form control for this property, so if there is anything in the save function that I could override, maybe I could do it in there. But I can't find any reference from a form control back to the web part instance.

Does anyone have any good ideas here? Could a cache dependency be used? Am I approaching this in the wrong way? I'm not referring to page output caching here. I'm referring to a custom object.

Recent Answers


Roman Hutnyk answered on July 8, 2015 04:01

Daniel,

I'd recommend to leave custom form control alone and try to invalidate cache in the web part code. I'd do that in a setter method of your property:

public YourObjectCalss RecordNumber
{
    get { return _recordNumber; }
    set
    {
        if (value != _recordNumber)
        {
            //TODO: invalidate cache
        }
        _recordNumber= value;
    }
}

I guess, this is the most convenient place to do this as this.ShortClientID is available there.

I'm not sure there is save event for web part that you could override.

0 votesVote for this answer Mark as a Correct answer

Daniel Reed answered on July 8, 2015 04:07

So if that property is placed on the web part, how would it get called? Do you mean that when I call RecordNumber it should check the previous value? How would it know what the previous value was? How would it know what _recordNumber is?

I have solved my issue for the moment by building a cache string based on the record number (and a few other things). Its not ideal, but its pretty reliable.

0 votesVote for this answer Mark as a Correct answer

Roman Hutnyk answered on July 8, 2015 04:31

How it knows property value now? It should sit in database, so example with private field was not the best...

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.